• 0

[C++] Only allow numbers to be entered


Question

I have to ask how many months an apartment is going to be rented for and I want it to be so that only numbers can be put in.

The program also asks if it's a one or two bedroom apartment and when you put a lettter in it just goes crazy.

Link to comment
Share on other sites

16 answers to this question

Recommended Posts

  • 0
I have to ask how many months an apartment is going to be rented for and I want it to be so that only numbers can be put in.

The program also asks if it's a one or two bedroom apartment and when you put a lettter in it just goes crazy.

586604749[/snapback]

It goes crazy because a letter is really just a number value to a computer. Do some error checking. If the input isn't a good value, tell the user the input wasn't valid and have them try again.

Link to comment
Share on other sites

  • 0

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	//declaration
	int nBedrooms, nMonths, AptPrice;
	float TotalPayment;
	char ChoiceFurnished, ChoiceRefrigerator;	

	//display appartment prices
	cout<<"---------------Riviera Apartments---------------\n"
  <<"One bedroom\n"
  <<"\tFurnished\n"
  <<"\t\tWith refrigerator.........950.00\n"
  <<"\t\tWithout refrigerator......935.00\n"
  <<"\tUnfurnished.......................890.00\n"
  <<"Two bedrooms\n"
  <<"\tFurnished\n"
  <<"\t\tWith refrigerator........1150.00\n"
  <<"\t\tWithout refrigerator.....1135.00\n"
  <<"\tUnfurnished......................1000.00\n";

	//ask number of bedrooms, verify 1/2 is entered
	cout<<"Enter the number of bedrooms(1/2): "; cin>>nBedrooms;
	while(nBedrooms!=1&&nBedrooms!=2)
	{
  cout<<"Enter the number of bedrooms(1/2): "; cin>>nBedrooms;
	}

	//use this for one bedroom apartments
	if(nBedrooms==1)
	{
  cout<<"\tWould you like furnished(f) or unfurnished(u)? "; cin>>ChoiceFurnished;
  while(ChoiceFurnished!='f'&&ChoiceFurnished!='F'&&ChoiceFurnished!='u'&&ChoiceFurnished!='U')
  {
  	cout<<"\tWould you like furnished(f) or unfurnished(u)? "; cin>>ChoiceFurnished;
  }
  	if(ChoiceFurnished=='f'||ChoiceFurnished=='F')
    ChoiceFurnished='F';
  	else
    ChoiceFurnished='U';

  	if(ChoiceFurnished=='F')
  	{  	
    {
    	cout<<"\tWould you like a refrigerator(y/n)? "; cin>>ChoiceRefrigerator;
    	while(ChoiceRefrigerator!='y'&&ChoiceRefrigerator!='Y'&&ChoiceRefrigerator!='n'&&ChoiceRefrigerator!='N')
    	{
      cout<<"\tWould you like a refrigerator(y/n)? "; cin>>ChoiceRefrigerator;
    	}
    }
  	
  	
  	if(ChoiceRefrigerator=='y'||ChoiceRefrigerator=='Y')
    ChoiceRefrigerator='Y';
  	else
    ChoiceRefrigerator='N';
  	}
  	

  cout<<"\tHow long would you like to keep the apartment (in months)? "; cin>>nMonths;
  while(nMonths<1)
  {
  	cout<<"\tHow long would you like to keep the apartment (in months)? "; cin>>nMonths;
  }

  //set prices for one bedroom apartment
  if(ChoiceFurnished=='F'&&ChoiceRefrigerator=='Y')
  	AptPrice=950.00;
  if(ChoiceFurnished=='F'&&ChoiceRefrigerator=='N')
  	AptPrice=935.00;
  if(ChoiceFurnished=='U')
  	AptPrice=890.00;	
	}

	//use this for two bedroom apartments
	if(nBedrooms==2)
	{
  cout<<"\tWould you like furnished(f) or unfurnished(u)? "; cin>>ChoiceFurnished;
  while(ChoiceFurnished!='f'&&ChoiceFurnished!='F'&&ChoiceFurnished!='u'&&ChoiceFurnished!='U')
  {
  	cout<<"\tWould you like furnished(f) or unfurnished(u)? "; cin>>ChoiceFurnished;
  }
  	if(ChoiceFurnished=='f'||ChoiceFurnished=='F')
    ChoiceFurnished='F';
  	else
    ChoiceFurnished='U';

  	if(ChoiceFurnished=='F')
  	{  	
    {
    	cout<<"\tWould you like a refrigerator(y/n)? "; cin>>ChoiceRefrigerator;
    	while(ChoiceRefrigerator!='y'&&ChoiceRefrigerator!='Y'&&ChoiceRefrigerator!='n'&&ChoiceRefrigerator!='N')
    	{
      cout<<"\tWould you like a refrigerator(y/n)? "; cin>>ChoiceRefrigerator;
    	}
    }
  	
  	
  	if(ChoiceRefrigerator=='y'||ChoiceRefrigerator=='Y')
    ChoiceRefrigerator='Y';
  	else
    ChoiceRefrigerator='N';
  	}

  cout<<"\tHow long would you like to keep the apartment (in months)? "; cin>>nMonths;
  while(nMonths<1)
  {
  	cout<<"\tHow long would you like to keep the apartment (in months)? "; cin>>nMonths;
  }

  //set prices for one bedroom apartment
  if(ChoiceFurnished=='F'&&ChoiceRefrigerator=='Y')
  	AptPrice=1150.00;
  if(ChoiceFurnished=='F'&&ChoiceRefrigerator=='N')
  	AptPrice=1135.00;
  if(ChoiceFurnished=='U')
  	AptPrice=1000.00;	
	}



	//find total using apartment price and number of months
	TotalPayment=nMonths*AptPrice;

	//manipulate ouput to show to show two digits after decimal point
	cout<<fixed<<showpoint<<setprecision(2);

	//display total payment
	cout<<"\t\t\tYour  total payment is: "<<TotalPayment<<endl;

	return 0;
}

Everything works fine right now as long as you don't input letters

Link to comment
Share on other sites

  • 0

use that isnum() function to do a check on your nBedrooms ... also I would work on the formatting of your code a little more so it is more readable try using some spaces.

Link to comment
Share on other sites

  • 0

try it as IsNum() ... I just did a google search and saw a function called isnum() ... I would try google ... it is everyones best friend.

Link to comment
Share on other sites

  • 0
You could read in the months as a char* then use atoi. Do some error checking between those.

586605005[/snapback]

thats a good way to do it

using isnum would look like this:

if (!isnum(x)) cout << "this is not a number";

Link to comment
Share on other sites

  • 0
There is no isnum() in standard C++ iirc.

the prefered way is to do:

int number(0);
if(std::wcin >> number)
{
 ? ?// good input
}
else
{
 ? // bad input
 ? // clear the buffer
 ?std::wcin.clear(); // clear the failbits
 ?std::wcin.ignore(std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
}

586605393[/snapback]

i didnt verify, im at work so i just gave him a sample code by what other ppl said about this fun:pion :p

Link to comment
Share on other sites

  • 0

There is no isnum() in standard C++ iirc.

the prefered way is to do:

int number(0);
if(std::wcin >> number)
{
    // good input
}
else
{
   // bad input
   // clear the buffer
  std::wcin.clear(); // clear the failbits
  std::wcin.ignore(std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
}

Link to comment
Share on other sites

  • 0
#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

//change display

system("TITLE Thang.Arsenal Home Letting Agency");

system("COLOR 2");

//Declare

float TotalPayment;

int nMonth;

int nRoom;

int Price;

char ChoiceFurnished;

char ChoiceBill;

char cDoagain;

do

{

system("CLS");

//Display the choices

cout << "\t----------Thang.Arsenal Home Letting Agency----------" << endl;

cout << "1 Bedroom Apartment" << endl;

cout << "\tFurnished: " << endl;

cout << "\t\tWith Bill Included......: " << "$1000" << endl << endl;

cout << "\t\tWithout Bill Included...: " << "$900" << endl << endl;

cout << "\tUnfurnished.....................: " << "$500" << endl;

cout << "2 Bedrooms Apartment" << endl;

cout << "\tFurnished: " << endl;

cout << "\t\tWith Bill Included......: " << "$1500" << endl << endl;

cout << "\t\tWithout Bill Included...: " << "$1200" << endl << endl;

cout << "\tUnfurnished.....................: " << "$900" << endl << endl;

cout << "Enter number of room <1/2>: " << endl;

cin >> nRoom;

while(nRoom!=1&&nRoom!=2)

{

cout << "Enter number of room <1/2>: ";

cin >> nRoom;

}

if(nRoom == 1) //this is very different with if(nRoom = 1)

{

cout << "Would you like Furnished (f) or Unfurnished (u) ? " << endl;

cin >> ChoiceFurnished;

while(ChoiceFurnished!='f'&&ChoiceFurnished!='F'&&ChoiceFurnished!='u'&&ChoiceFurnished!='U')

{

cout << "Would you like Furnished (f) or Unfurnished (u) ? " << endl;

cin >> ChoiceFurnished;

}

if(ChoiceFurnished=='f'||ChoiceFurnished=='F')

{

ChoiceFurnished = 'F';

}else{

ChoiceFurnished = 'U';

}

/*

this bit here...

there are 2 condition for ChoiceFurnished to follow

as ChoiceFurnished can only be 'f' 'F' 'u' 'U' from while(statement)

ChoiceFurnished is also set to be 'F' whenever the input is 'f' 'F' ... other than that will be 'U'

*/

if(ChoiceFurnished == 'F')

{

cout << "Would you like you bill included?" << "(y or n)" << endl << endl;

cin >> ChoiceBill;

while(ChoiceBill!='y'&&ChoiceBill!='Y'&&ChoiceBill!='n'&&ChoiceBill!='N')

{

cout << "Would you like you bill included?" << "(y or n)" << endl << endl;

cin >> ChoiceBill;

}

}

if(ChoiceFurnished == 'y' || ChoiceFurnished == 'Y')

{

ChoiceFurnished = 'Y';

}else{

ChoiceFurnished = 'N';

}

cout << "How long are you going to sign a contact for this property (in months) ?" << endl;

cin >> nMonth;

if(nMonth <1)

{

cout << "How long are you going to sign a contact for this property (in months) ?" << endl;

cin >> nMonth;

}

// set price for 1 bedroom apartment with choices...

if(ChoiceFurnished=='F'&&ChoiceBill=='Y')

Price=1000;

if(ChoiceFurnished=='F'&&ChoiceBill=='N')

Price=900;

if(ChoiceFurnished=='U')

Price=500;

}

if(nRoom == 2) //this is different with if(nRoom = 2)

{

cout << "Would you like Furnished (f) or Unfurnished (u) ? " << endl;

cin >> ChoiceFurnished;

while(ChoiceFurnished!='f'&&ChoiceFurnished!='F'&&ChoiceFurnished!='u'&&ChoiceFurnished!='U')

{

cout << "Would you like Furnished (f) or Unfurnished (u) ? " << endl;

cin >> ChoiceFurnished;

}

if(ChoiceFurnished == 'f' || ChoiceFurnished == 'F')

{

ChoiceFurnished = 'F';

}else{

ChoiceFurnished = 'U';

}

if(ChoiceFurnished == 'F')

{

cout << "Would you like you bill included?" << "(y or n)" << endl << endl;

cin >> ChoiceBill;

while(ChoiceBill!='y'&&ChoiceBill!='Y'&&ChoiceBill!='n'&&ChoiceBill!='N')

{

cout << "Would you like you bill included?" << "(y or n)" << endl << endl;

cin >> ChoiceBill;

}

}

if(ChoiceFurnished == 'y' || ChoiceFurnished == 'Y')

{

ChoiceFurnished = 'Y';

}else{

ChoiceFurnished = 'N';

}

cout << "How long are you going to sign a contact for this property (in months) ?" << endl;

cin >> nMonth;

if(nMonth <1)

{

cout << "How long are you going to sign a contact for this property (in months) ?" << endl;

cin >> nMonth;

}

// set price for 1 bedroom apartment with choices...

if(ChoiceFurnished=='F'&&ChoiceBill=='Y')

Price=1500;

if(ChoiceFurnished=='F'&&ChoiceBill=='N')

Price=1200;

if(ChoiceFurnished=='U')

Price=900;

}

//Calculate the total payment

TotalPayment = Price * nMonth;

cout<<fixed<<showpoint<<setprecision(3);

cout<<"\t\t\tYour total payment is: "<<TotalPayment<<endl;

cout << "Would you like to choose another options?" << "(y or n)" << endl;

cin >> cDoagain;

if(cDoagain=='y'||cDoagain=='Y')

{

cDoagain = 'Y';

}else{

cDoagain = 'N';

}

}

while (cDoagain == 'Y' || cDoagain == 'y');

system("PAUSE");

return 0;

}

This is what I have done ...

But I have no idea how this thing turned out to be wrong in the final calculation...

instead of taking the Price variable into calculation ... it set that to be 40 somehow

I have no idea

Link to comment
Share on other sites

  • 0

Holy thread bumping batman :p If you still need help you're better off creating a new thread as this one will probably be locked soon.

Also if you create a new thread please indent your code. It makes it a lot easier to others to understand the code and it may help you to find the problem also :)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.