Cookie Monster 'Virus'

I've written the Cookie Monster program to test my memory on what I've learnt so far. However, when I run the program, if the user enters several words, the message in the while loop repeats itself for the amount of words are in the string. I can't figure out why. Any ideas? :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;

int main()
{
    const string answer = "cookie";
    string userinput;
    
    while (answer != userinput) {
          cout << "I am the cookie monster, give me cookies!" << endl;
          cin >> userinput;
          }
    if (answer == userinput){
       cout << "Om nom... I love cookies!" << endl;
       system("pause");
       }
    return 0;
}
    



Thanks :)
Using >> on cin reads as far as the next white space or the end of input. This leaves anything after that white space still to be read. Next time, it will again read to the next white space, and so on.
What is the solution? :)

Thanks
Last edited on
Your program seems to work fine to me....i dont see any problem, but if you have some kind of problem you can try cin.ignore() before cin statement in loop, that might help your code!
Ermm ummm maybe ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;

int main()
{
    const string answer = "cookie";
    string userinput;
    
    while (answer != userinput) {
          cout << "I am the cookie monster, give me cookies!" << endl;
          getline(cin,userinput);
          }
    if (answer == userinput){
       cout << "Om nom...  Hell yea I love cookies!" << endl;
       system("pause");
       }
    return 0;
}


Last edited on
Cheers guys, cin.ignore() did not work for me, however getline did! :)

Thanks!
remember no system commands please. system("pause");you can delete that line completely.
Why?
Thank you, ui uiho and firedraco, I wasn't aware. What should I use instead? DOS opens and closes so rapidly and I can't install Linux (my OS of choice) on my machine in the forseeable future.
there are a few ways to do this, one you could make your own wait function to keep the consul open for a second after completion of the program. here is the wait function i use.
1
2
3
4
5
6
int wait ( int seconds ){// include time.h library
    clock_t endwait;
    endwait = clock() + seconds * CLOCKS_PER_SEC ;
    while (clock() < endwait) {}
    return 0;
}

this does the same thing as system("PAUSE"); but it does not call to the OS for the function, you write it yourself in the code, which is safer and faster. another thing that you can do is write your own exit function.
1
2
3
4
5
int exitfunc(){
std::cout << "Press ENTER to exit...";
std::cin.ignore(/*put in limits if you want*/);
return 0; 
}
this will allow you to control the time the program closes.
otherwise std::cin.ignore() will work at the end of your program as long as your cin buffer is empty. i would prefer the wait function because you do not have to worry about the cin buffer and you can use it throughout your program if you ever want to do something like a countdown, but the option is up to you and the one you select should vary depending on the program you are running.
Last edited on
Without adding any extra code the best way to keep your program open is by (instead of just double clicking the .exe) going into command prompt and executing it from there (just type the program's name, for example type "myprog.exe") and you'll be able to see the output.

Topic archived. No new replies allowed.