Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   How to make the console wait until i type somting? (http://www.go4expert.com/forums/console-wait-i-type-somting-t25655/)

itlahic 28Apr2011 06:25

How to make the console wait until i type somting?
 
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?

xpi0t0s 28Apr2011 13:43

Re: How to make the console wait until i type somting?
 
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.

xpi0t0s 28Apr2011 13:44

Re: How to make the console wait until i type somting?
 
Use an if statement to check the answer.
Code:

if (iNumber==8)
  cout << "Correct";
else
  cout << "Wrong";


itlahic 28Apr2011 17:24

Re: How to make the console wait until i type somting?
 
Ok where would i add that in?
like under where it asks what is 4+4?

xpi0t0s 29Apr2011 03:37

Re: How to make the console wait until i type somting?
 
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.

itlahic 29Apr2011 07:38

Re: How to make the console wait until i type somting?
 
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;

}


xpi0t0s 29Apr2011 14:44

Re: How to make the console wait until i type somting?
 
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.


All times are GMT +5.5. The time now is 02:00.