higueraalfredo Posted October 1, 2005 Share Posted October 1, 2005 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 More sharing options...
0 GoogleNinja Posted October 1, 2005 Share Posted October 1, 2005 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 More sharing options...
0 higueraalfredo Posted October 1, 2005 Author Share Posted October 1, 2005 #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 More sharing options...
0 Dan.Varga Posted October 1, 2005 Share Posted October 1, 2005 you can use the function isnum(variable) to check and see if it is a number or not. Link to comment Share on other sites More sharing options...
0 Dan.Varga Posted October 1, 2005 Share Posted October 1, 2005 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 More sharing options...
0 higueraalfredo Posted October 1, 2005 Author Share Posted October 1, 2005 I guess the tabs and stuff that Visual C++ adds don't show up for some reason but here it looks fine to me. Link to comment Share on other sites More sharing options...
0 Dan.Varga Posted October 1, 2005 Share Posted October 1, 2005 Basically you would do isnum(variable) ... it will return a value true/false on whether or not it is a number. Link to comment Share on other sites More sharing options...
0 higueraalfredo Posted October 1, 2005 Author Share Posted October 1, 2005 Can you explain the isnum thing cause I can't really find anything on google Link to comment Share on other sites More sharing options...
0 higueraalfredo Posted October 1, 2005 Author Share Posted October 1, 2005 What library do I use? It says undefined identifier Link to comment Share on other sites More sharing options...
0 Dan.Varga Posted October 1, 2005 Share Posted October 1, 2005 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 More sharing options...
0 masterren Posted October 1, 2005 Share Posted October 1, 2005 You could read in the months as a char* then use atoi. Do some error checking between those. Link to comment Share on other sites More sharing options...
0 Rudy Posted October 1, 2005 Share Posted October 1, 2005 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 More sharing options...
0 Rudy Posted October 1, 2005 Share Posted October 1, 2005 (edited) or even better do {cin >> x;} while (!isnum(x)); Edited October 1, 2005 by Rudy Link to comment Share on other sites More sharing options...
0 Rudy Posted October 1, 2005 Share Posted October 1, 2005 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 More sharing options...
0 AndreasV Posted October 1, 2005 Share Posted October 1, 2005 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 More sharing options...
0 Thang.Arsenal Posted October 10, 2009 Share Posted October 10, 2009 #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 More sharing options...
0 ViZioN Posted October 10, 2009 Share Posted October 10, 2009 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 More sharing options...
Question
higueraalfredo
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