Join 136,059 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,558 people online right now. Registration is fast and FREE... Join Now!
Please help, it keeps giving me errors when I compile it, it says the error is where it says getline (firstquestion); Please repost it with this problem fixed, thank you in advance,
CODE
#include<iostream> #include<string> using namespace std;
int main(void) { beginning: system("CLS"); system("TITLE Play Game or Load Game?"); string question1; string question2; string question3; string question4; string question5; string firstquestion; double dpassword; system("CLS"); system("TITLE Test Game"); cout << "Play Game" << endl; cout << "Load Game" << endl; cout << "Answer: "; getline (firstquestion); if(playgameorloadgame == Play Game || Play game || play game){ goto question1; }else if(playgameorloadgame == Load Game || Load game || load game){ goto loadgame; }else{ cout << "That is invalid" << endl; system("PAUSE"); goto beginning; } loadgame: system("CLS"); system("TITLE Please enter the password"); cout << "Please enter the password: " << endl; cin >> dpassword; if(dpassword == password1){ goto question1; }else if(dpassword == password2){ goto question2 }else if(dpassword == password3){ goto question3 }else if(dpassword == password4){ goto question4 }else if(dpassword == password5){ goto question5 }else{ system("CLS"); system("TITLE Wrong Password"); cout << "There are only 5 levels.....for now." << endl; system("PAUSE"); goto beginning; } question1: system("CLS"); system("TITLE Question 1"); cout << "What does GPU stand for?" << endl; getline (question1); if(question1 == Graphics Processing Unit || Graphics Processing unit || Graphics processing unit || graphics processing unit){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password1" << endl; cout << "That is not a L that is a one." << endl; system("PAUSE"); goto question2; }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); goto beginning; } question2: system("CLS"); system("TITLE Question 2"); cout << "What does CPU stand for?" << endl; getline (question2); if(question2 == Central Processing Unit || Central Processing unit || Central processing unit || central processing unit){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password2" << endl; system("PAUSE"); goto question3; }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); goto beginning; } question3: system("CLS"); system("TITLE Question 3" << endl; cout << "Can your computer run without a graphics card?" << endl; getline (question3); if(question3 == yes || Yes){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password3" << endl; system("CLS"); goto question4; }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); goto beginning; } question4: system("CLS"); system("TITLE Question 4"); cout << "Does a computer need a processor?" << endl; getline (question4); if(question4 == yes || Yes){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password4" << endl; system("PAUSE"); goto question5; }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); goto beginning; } question5: system("CLS"); system("TITLE Question 5"); cout << "Can you use your T.V. as a monitor?" << endl; getline (question5); if(question5 == yes || Yes){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password5" << endl; system("PAUSE"); system("CLS"); system("TITLE YOUR FINISHED!!!"); cout << "That is the end of the game, more to come!" << endl; system)"PAUSE"); goto beginning; }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); goto beginning; } return 0; }
Please help, it keeps giving me errors when I compile it, it says the error is where it says getline (firstquestion); Please repost it with this problem fixed, thank you in advance,
CODE
#include<iostream> #include<string> using namespace std;
int main(void) { beginning: system("CLS"); system("TITLE Play Game or Load Game?"); string question1; string question2; string question3; string question4; string question5; string firstquestion; double dpassword; system("CLS"); system("TITLE Test Game"); cout << "Play Game" << endl; cout << "Load Game" << endl; cout << "Answer: "; getline (firstquestion); if(playgameorloadgame == Play Game || Play game || play game){ goto question1; }else if(playgameorloadgame == Load Game || Load game || load game){ goto loadgame; }else{ cout << "That is invalid" << endl; system("PAUSE"); goto beginning; } loadgame: system("CLS"); system("TITLE Please enter the password"); cout << "Please enter the password: " << endl; cin >> dpassword; if(dpassword == password1){ goto question1; }else if(dpassword == password2){ goto question2 }else if(dpassword == password3){ goto question3 }else if(dpassword == password4){ goto question4 }else if(dpassword == password5){ goto question5 }else{ system("CLS"); system("TITLE Wrong Password"); cout << "There are only 5 levels.....for now." << endl; system("PAUSE"); goto beginning; } question1: system("CLS"); system("TITLE Question 1"); cout << "What does GPU stand for?" << endl; getline (question1); if(question1 == Graphics Processing Unit || Graphics Processing unit || Graphics processing unit || graphics processing unit){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password1" << endl; cout << "That is not a L that is a one." << endl; system("PAUSE"); goto question2; }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); goto beginning; } question2: system("CLS"); system("TITLE Question 2"); cout << "What does CPU stand for?" << endl; getline (question2); if(question2 == Central Processing Unit || Central Processing unit || Central processing unit || central processing unit){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password2" << endl; system("PAUSE"); goto question3; }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); goto beginning; } question3: system("CLS"); system("TITLE Question 3" << endl; cout << "Can your computer run without a graphics card?" << endl; getline (question3); if(question3 == yes || Yes){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password3" << endl; system("CLS"); goto question4; }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); goto beginning; } question4: system("CLS"); system("TITLE Question 4"); cout << "Does a computer need a processor?" << endl; getline (question4); if(question4 == yes || Yes){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password4" << endl; system("PAUSE"); goto question5; }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); goto beginning; } question5: system("CLS"); system("TITLE Question 5"); cout << "Can you use your T.V. as a monitor?" << endl; getline (question5); if(question5 == yes || Yes){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password5" << endl; system("PAUSE"); system("CLS"); system("TITLE YOUR FINISHED!!!"); cout << "That is the end of the game, more to come!" << endl; system)"PAUSE"); goto beginning; }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); goto beginning; } return 0; }
- C++ Programmer
getline(cin, firstquestion);
Also, don't use goto's. Use functions or loops or something.
Next you need to make sure you define a variable before you try to use it.
Then look at how to compare the values of strings using the == operator.
You really should not use goto... It works, but you will find you code hard to maintain and keep track of. You end up with spaghetti code if you use goto too much. There are control structures (if, switch, while, do-while, for) that do a wonderful job of avoiding the use of goto.
if(playgameorloadgame == Play Game || Play game || play game){ qestion1(); }else if(playgameorloadgame == Load Game || Load game || load game){ loadgame(); }else{ cout << "That is invalid" << endl; system("PAUSE"); main(); }
void loadgame() { system("CLS"); system("TITLE Please enter the password"); cout << "Please enter the password: " << endl; cin >> dpassword;
if(dpassword == password1){ question1(); }else if(dpassword == password2){ question2(); }else if(dpassword == password3){ question3(); }else if(dpassword == password4){ question4(); }else if(dpassword == password5){ question5(); }else{ system("CLS"); system("TITLE Wrong Password"); cout << "There are only 5 levels.....for now." << endl; system("PAUSE"); main(); } } void question1() { system("CLS"); system("TITLE Question 1"); cout << "What does GPU stand for?" << endl; getline (question1); if(question1 == Graphics Processing Unit || Graphics Processing unit || Graphics processing unit || graphics processing unit){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password1" << endl; cout << "That is not a L that is a one." << endl; system("PAUSE"); question2(); }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); main(); } }
void question2() { system("CLS"); system("TITLE Question 2"); cout << "What does CPU stand for?" << endl; getline (question2); if(question2 == Central Processing Unit || Central Processing unit || Central processing unit || central processing unit){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password2" << endl; system("PAUSE"); question3(); }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); main(); } } void question3() { system("CLS"); system("TITLE Question 3" << endl; cout << "Can your computer run without a graphics card?" << endl; getline (question3); if(question3 == yes || Yes){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password3" << endl; system("CLS"); question4(); }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); main(); } } void question4() { system("CLS"); system("TITLE Question 4"); cout << "Does a computer need a processor?" << endl; getline (question4); if(question4 == yes || Yes){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password4" << endl; system("PAUSE"); question5(); }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); main(); } } void question5() { system("CLS"); system("TITLE Question 5"); cout << "Can you use your T.V. as a monitor?" << endl; getline (question5); if(question5 == yes || Yes){ system("CLS"); system("TITLE Correct!"); cout << "That is correct, here is the password: password5" << endl; system("PAUSE"); system("CLS"); system("TITLE YOUR FINISHED!!!"); cout << "That is the end of the game, more to come!" << endl; system)"PAUSE"); main(); }else{ system("CLS"); system("TITLE Wrong Answer"); cout << "Sorry, that is incorrect." << endl; system("PAUSE"); main(); } }
see the diffrence without goto i hope this run coz idont have a tc compiler and a ide i never compiled it