| youngcoder13 (21) | |
|
Hello, I created a text game creator but it does not seem to work. Can somebody explain what is going wrong and any tips? // // main.cpp // Text creator 3 // // Created by Olivier on 9/30/12. // Copyright (c) 2012 Olivier. All rights reserved. // #include <iostream> using namespace std; void gamemiddle1(); void gamemiddle2(); string nameofgame; string name; string start; string startsolution1; string startsolution2; string middle1; string middle2; string middle1solution; string middle2solution; string middle1solution1; string middle1solution2; string middle2solution1; string middle2solution2; void gamestart() { cout << "Welcome to " << nameofgame; cout << " by " << name; cout << ",\n" << start; cout << " :"; char startsolution[10]; cin >> startsolution; if (startsolution == startsolution1) { gamemiddle1(); } if (startsolution == startsolution2) { gamemiddle2(); } } int main(int argc, const char * argv[]) { cout << "Name of game: "; getline(cin,nameofgame); cout << "Name of Author: "; getline(cin,name); cout << "Enter beginning of Text adventure: "; getline(cin,start); cout << "One answer to " << start; cout << ": "; getline(cin,startsolution1); cout << "Another answer to " << start; cout << ": "; getline(cin,startsolution2); cout << "Enter middle of the Text adventure if player types in " << startsolution1; cout << ": "; getline(cin,middle1); cout << "Enter middle of Text adventure if player types " << startsolution2; cout << ": "; getline(cin,middle2); cout << "One answer to " << middle1; getline(cin,middle1solution1); cout << "Another answer to " << middle1; getline(cin,middle1solution2); gamestart(); } void gamemiddle1() { cout << middle1; getline (cin,middle1solution); } void gamemiddle2() { cout << middle2; getline (cin,middle2solution); } | |
|
|
|
| AleaIactaEst (99) | |
| Looks fine to me, what part isn't working? | |
|
|
|
| youngcoder13 (21) | |
| It automatically ends program once it displays the middle, it doesnt wait for getline. Do you have any tips or suggestions? This is my third try for this program and I dont want to mess up. Thanks a lot though! | |
|
|
|
| BlakeK (5) | |
|
I would bet there is still a '\n' in the stream. It gets thrown into getline without you seeing it, and your program closes. Before getline, you need to clear that out... std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );and #include <limits> | |
|
|
|
| youngcoder13 (21) | |
|
It worked, just need to clear a bit up. // // main.cpp // Text creator 3 // // Created by Olivier on 9/30/12. // Copyright (c) 2012 Olivier. All rights reserved. // #include <iostream> #include <limits> using namespace std; void gamemiddle1(); void gamemiddle2(); void gameend1(); void gameend2(); string nameofgame; string name; string start; string startsolution1; string startsolution2; string middle1; string middle2; string middle1solution; string middle2solution; string middle1solution1; string middle1solution2; string middle2solution1; string middle2solution2; string end1; string end2; void gamestart() { cout << "Welcome to " << nameofgame; cout << " by " << name; cout << ",\n" << start; cout << " :"; char startsolution[10]; cin >> startsolution; if (startsolution == startsolution1) { gamemiddle1(); } if (startsolution == startsolution2) { gamemiddle2(); } } int main(int argc, const char * argv[]) { cout << "Name of game: "; getline(cin,nameofgame); cout << "Name of Author: "; getline(cin,name); cout << "Enter beginning of Text adventure: "; getline(cin,start); cout << "One answer to " << start; cout << ": "; getline(cin,startsolution1); cout << "Another answer to " << start; cout << ": "; getline(cin,startsolution2); cout << "Enter middle of the Text adventure if player types in " << startsolution1; cout << ": "; getline(cin,middle1); cout << "Enter middle of Text adventure if player types " << startsolution2; cout << ": "; getline(cin,middle2); cout << "One answer to " << middle1; getline(cin,middle1solution1); cout << "Another answer to " << middle1; getline(cin,middle1solution2); cout << "Enter the end of the game if player types " << middle1solution1; cout << ": "; getline(cin,end1); cout << "Enter the end of the game if player types " << middle1solution2; cout << ": "; getline(cin,end2); gamestart(); } void gamemiddle1() { cout << middle1; std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); getline(cin,middle1solution); if (middle1solution == end1) { gameend1(); } } void gamemiddle2() { cout << middle2; std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); getline(cin,middle2solution); if (middle2solution == end2) { gameend2(); } } void gameend1() { cout << end1; } void gameend2() { cout << end2; } | |
|
Last edited on
|
|