= is for assignment, == for comparison
= is for assignment, == for comparison
I wrote the program and it works perfectly, but there's a catch.
Code:#include <iostream.h> #include <string> using namespace std; int main() { string date; char answer; //either yes or no bool check; do{cout << "\nplease enter the date in dd/mm/yyyy: "; cin >> date; cout << "\nIs this the correct date ? press y/n \n" << date << "\n\n"; cin >> answer; if (answer == 'y'){ cout << "\nThank you!!\n"; break; } else if (answer != 'n' && answer != 'y') { cout << "invalid input\n"; } } while (answer == 'n' && answer != 'n' || answer != 'y'); system ("pause"); return 0; }
i got it to loop and put it in my program, (right at the end. last 7 lines)...
but, i have a problem. When a non integer is inputted, the program crashes.
how do i get around this ?
Code:#include <iostream.h> #include <string> #include <conio.h> main() { int salescode, bikeprice, modelcode, quantity; string date; char answer; //either yes or no do //the instructions to enter the i.d. code will be repeated (do...while //loop) as long as the salescode is less than 1 or greater than 20. { cout << "\n\nPlease enter your identifiction code "; cin >> salescode; //the condition is tested if (salescode < 1 || salescode > 20) { cout << ("\nTHIS I.D. CODE IS NOT VALID. TRY AGAIN \n"); } } //the instructions to enter the i.d. code will be repeated (do...while //loop) as long as the salescode is less than 1 or greater than 20. while (1 > salescode || salescode > 20); //another do...while loop do { cout << "\nPlease enter the bike price (one unit = 1 Euro) "; cin >> bikeprice; if (bikeprice < 1 || bikeprice > 500) { cout << ("\nINVALID PRICE. Bike prices cannot be 0 or more than 500. TRY AGAIN \n"); } } while (bikeprice < 1 || bikeprice > 500); //another do...while loop do { cout << "\nPlease enter the 3 digit model code "; cin >> modelcode; if (modelcode < 0 || modelcode > 999) { cout << ("\nINVALID MODEL CODE. TRY AGAIN \n"); } } while (modelcode < 0 || modelcode > 999); //another do...while loop do { cout << ("\nPlease enter the quantity sold "); cin >> quantity; if (quantity < 1 || quantity > 10) { cout << ("\nINVALID QUANTITY. TRY A NUMBER FROM 1-10 \n"); } } while (quantity < 1 || quantity > 10); do { cout << "\nPlease enter the date in this format dd/mm/yyyy: "; cin >> date; cout << "\nIs this the correct date ?\t" << date << "\tpress y/n: \n\n"; cin >> answer; //gets answer from user switch (answer) { case 'y': cout << "\n\n\nThank You!\n\n"; break; } }while (answer != 'y'); }
Shouldn't those be AND operators? saying OR means that only on side has to be true for it to continue.
As to the actual question you asked, have you read the FAQ yet, particularly Cprogramming.com FAQ > Convert a string to a int (C++) ?
Which part doesn't it recognize?
You can't use >> by definition since that will not read in a character.
Info :Compiling E:\program\noname00.cpp
Error: noname00.cpp(1,2):Unable to open include file 'SSTREAM.h' ??????????????
Warn : STRING.h(549,3):Functions containing for are not expanded inline
Warn : STRING.h(557,3):Functions containing while are not expanded inline
Warn : STRING.h(563,3):Functions containing for are not expanded inline
Warn : STRING.h(575,3):Functions containing for are not expanded inline
Warn : string.cc(686,32):Comparing signed and unsigned values
Error: noname00.cpp(37,25):Undefined symbol 'istringstream' ??????????
Error: noname00.cpp(37,25):Statement missing ; ?????????????
Error: noname00.cpp(39,17):Undefined symbol 'myStream' ???????
Warn : noname00.cpp(43,2):Parameter 's' is never used
Warn : noname00.cpp(43,2):Parameter 'i' is never used
are you trying to accomplish something like this
breakup code however needed,Code:#include <string> #include <iostream> #include "input.h" //function prototype of double readDouble(string prompt, bool allowNegatives) using namespace std; int main() { double month;//use whatever u want here double day; double year; char resp; char no='n'; yes='y'; do{ double month=readDouble("Enter a month ",false); double day=readDouble("Enter a day ",false); double cin>>year=readDouble("Enter a year ",false); cout<<"Is this date correct "<<month<<"/"<<day<<"/"<<year<<endl; cin>>resp; }while(resp=='n'||resp==no); //while(resp=='y'||resp==yes) //put whatever u want here } double readDouble(string prompt, bool allowNegatives) { double rv=0.0; bool done=false; do { cout<<prompt; cin>>rv; while(cin.fail()!=0) { cerr<<"Cannot read data!"<<endl; cin.clear(); cin.ignore(256,'\n'); cout<<prompt; cin>>rv; } if (allowNegatives==false && rv<0) { cerr<<"Input positive numbers!"<<endl; cin.clear(); cin.ignore(256,'\n'); cin>>rv; } else { done=true; } return rv; } while(done==false); }
now that im looking at this the char no='n' and char yes='y' are not needed
Last edited by Creatlv3; 05-04-2010 at 01:37 AM. Reason: took a closer look at my program, fixed mistakes made by tiredness