Ok so heres the code Code: #include <iostream> int main() { std::cout << "welcome to my math game type play to start" << std::endl; int iWord; std::cin >> iWord; std::cout << "Good what is 4+4? " << std::endl; int iNumber; std::cin >> iNumber; std::cout << "Great, now what is 3 X 7? " << std::endl; return 0; } I want to make a math game but as soon as i type "play" it shows the rest of the text how can i make it wait until i type 8 ( the answer) and then say line 9 ( im new and i want to make games for a job so im just learning at the moment and how can i make it say that its wrong if some one buts like 15?
iWord is an int, not a string. cin tries to read until it gets a number, and since there isn't one that confuses it. So change int iWord to string iWord and this will fix the problem.
Use an if statement to check the answer. Code: if (iNumber==8) cout << "Correct"; else cout << "Wrong";
Well, you don't need to ask that sort of question. Have a guess where to put it in, then run the program, and if it doesn't behave as you want, have another guess. With programs this small there aren't many different places to put it so you're going to hit on a valid location fairly quickly.
Ok thanks i got it but now i want to make it so if i get it wrong ill go to the start and if i get it right i go to the next line ive made a new code its a text adventure and i need help with the question above. Code: #include <iostream> int main() { std::cout << "Welcome to Adventure Joe Ep1" << std::endl; std::cout << "Use the 1, 2, 3, 4 keys to pick your choice" << std::endl; std::cout << "-------------------------------------------" << std::endl; std::cout << " Hey! Hey you, can you hear me?" << std::endl; std::cout << "1-Yes." << std::endl; std::cout << "2-No." << std::endl; int iNumber; std::cin >> iNumber; if (iNumber==1) std::cout << "Good he's awake!"; else std::cout << "Damm he's down for the count"; return 0; }
OK so now you'll need some mechanism for looping. There are three basic types of loop in C: for, while and do. Look them up in your textbook, or google them. for is used for counting, and while and do are both the same in terms of looping while a condition is true, but with while the condition is at the start (while X is true, do this) and with do the condition is at the end (do this while X is true). The main difference between do and while is that a do loop always runs at least once; both for and while loops can run no times.