1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to make the console wait until i type somting?

Discussion in 'C++' started by itlahic, Apr 28, 2011.

  1. itlahic

    itlahic New Member

    Joined:
    Apr 28, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Use an if statement to check the answer.
    Code:
    if (iNumber==8)
      cout << "Correct";
    else
      cout << "Wrong";
    
     
  4. itlahic

    itlahic New Member

    Joined:
    Apr 28, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Ok where would i add that in?
    like under where it asks what is 4+4?
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  6. itlahic

    itlahic New Member

    Joined:
    Apr 28, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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;
    
    }
    
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page