c++ - While loop keeps printing out and not reading the next line -
this first simple program. keeps printing out guess is. non-stop , doesn't ask user input. (the next line of code.) mistake?
#include <iostream> #include <string> using namespace std; int main() { string username; cout << "hello there.\n"; cout << "my name tars. \n"; cout << "what name? \n"; getline(std::cin, username); cout << username << ", let's play game.\n"; int secretnum; secretnum = rand() % 20 + 1; cout << "i'm thinking of number between 1-20.\n"; int guess; bool conti = true; while (conti) cout << "guess is. \n"; cin >> guess; if (guess == secretnum) { cout << "you read mind!"; conti = false; } if (guess < secretnum) { cout << "that low."; } if (guess > secretnum) { cout << "that high."; } return 0; }
you need use brackets
#include <iostream> #include <string> using namespace std; int main() { string username; cout << "hello there.\n"; cout << "my name tars. \n"; cout << "what name? \n"; getline(std::cin, username); cout << username << ", let's play game.\n"; int secretnum; secretnum = rand() % 20 + 1; cout << "i'm thinking of number between 1-20.\n"; int guess; bool conti = true; while (conti) { cout << "guess is. \n"; cin >> guess; if (guess == secretnum) { cout << "you read mind!"; conti = false; } if (guess < secretnum) { cout << "that low."; } if (guess > secretnum) { cout << "that high."; } } return 0; } by default if don't use them next line considered part of while loop in case:
while (conti) cout << "guess is. \n";
Comments
Post a Comment