I can't use cin.get() in my game

I can't use the cin.get(); in my game. It isn't translated, or I don't know....
Please help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  #include <iostream>
#include <cstdlib>
#include <time.h>


int main() {
	std::string go;
	typedef long long int large;
	int r;
	int i = 0, j = 0;	
	large nums;
	large answ;
	int compare;
	bool repeat = false;
	int life = 3;
	time_t t;
	srand((unsigned)time(&t));
	std::cout <<"\n" <<"Hi! This is a memory tester program. \nYou should remember numbers, and repeat it all. \nIf you scroll back, you cheat! If you'r ready, type: 'go'. Good luck!" <<"\n";
	do {
		std::cin >> go;
		i++;
		if ((i % 3) == 0){
			std::cout <<"You don't want to start the game or you can't type 'go'?" << std::endl;
		}
	}while(go != "go");
	do {
		r = ( 1 + rand()%9);
		
		if(!repeat){
			nums = (nums*10)+r;
		}
		
		std::cout <<"Remember this: " << nums <<"\n";
		sleep(3);
		std::cout << std::string(200, '\n');
		std::cout <<"Okay, then type the number/numbers: ";
		std::cin >> answ;
		
		if (answ == nums){
			std::cout <<"You got it! Next!" <<"\n";		
			++j;
			repeat = false;
		}
		
		else{
			std::cout <<"You missed! Try again!" <<"\n";
			repeat = true;
			life -= 1;
			std::cout <<"Your lives: " << life <<"\n" <<"Press ENTER";
			std::cin.get();
		}	
		
		if(life == 0){
			std::cout <<"Hey, out of your lives! Sorry, Game Over!" <<"\n" <<"Your score: " << j <<" point." <<std::endl;
			return 0;
		}	
	}while(j != 10);
	std::cout <<"Congratulations! You scored full point!" <<std::endl;
	return 0;
}
You need to include <string> to use std::string

There is no sleep function in C++ so use
1
2
3
#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));
The cin input stream is buffered. After entering each value such as go or answ, there will remain at least a trailing newline character '\n' in the buffer.

If you clear the buffer after the cin, then the next input operation becomes more guaranteed to behave in a predictable way.

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

int main() 
{
    std::string go;
    std::cout << "enter a string\n";
    std::cin  >> go;
    std::cin.ignore(1000, '\n');      // empty input buffer


    long long answ;    
    std::cout << "enter a number\n";
    std::cin  >> answ;
    std::cin.ignore(1000, '\n');      // empty input buffer

    std::cout << "Press ENTER\n";
    std::cin.get();
    
    
    std::cout << "Done\n";
}


http://www.cplusplus.com/reference/istream/istream/ignore/
Thank you but don't you know how to stop the program in the "else"? The cin.get not works...
I thought I explained it, by way of a working example. Once you understand the example I gave, you should be equipped to apply that knowledge to other situations.

I also explained it in more detail in this thread:
http://www.cplusplus.com/forum/beginner/205575/#msg973473
Sorry I din't see YOUR answer, just the first... my computer was slow or I don't know... sorry, your answer is very helpful :D Thank you! :D
Thank you, it works :) I tried it.
Last edited on
The only question I have, why I have to press 2 enters?
Nothing, it works fine :D I just put the ignore after the enter pressing cout... now everything is fine! Many thanks! :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cstdlib>
#include <time.h>


int main() {
	std::string go;
	typedef long long int large;
	int r;
	int i = 0, j = 0;	
	large nums;
	large answ;
	int compare;
	bool repeat = false;
	int life = 3;
	time_t t;
	srand((unsigned)time(&t));
	std::cout <<"\n" <<"Hi! This is a memory tester program. \nYou should remember numbers, and repeat it all. \nIf you scroll back, you cheat! If you'r ready, type: 'go'. Good luck!" <<"\n";
	do {
		std::cin >> go;
		std::cin.ignore(1000, '\n');  
		i++;
		if ((i % 3) == 0){
			std::cout <<"You don't want to start the game or you can't type 'go'?" << std::endl;
		}
	}while(go != "go");
	do {
		r = ( 1 + rand()%9);
		
		if(!repeat){
			nums = (nums*10)+r;
		}
		
		std::cout <<"Remember this: " << nums <<"\n";
		sleep(3);
		std::cout << std::string(200, '\n');
		std::cout <<"Okay, then type the number/numbers: ";
		std::cin >> answ;
		std::cin.ignore(1000, '\n');  
		
		if (answ == nums){
			std::cout <<"You got it! Next!" <<"\n";		
			++j;
			repeat = false;
		}
		
		else{
			std::cout <<"You missed! Try again!" <<"\n";
			repeat = true;
			life -= 1;
			std::cout <<"Your lives: " << life <<"\n" <<"Press ENTER";
			std::cin.get();
		}	
		
		if(life == 0){
			std::cout <<"Hey, your lives over! Sorry, Game Over!" <<"\n" <<"Your score: " << j <<" point." <<std::endl;
			return 0;
		}	
	}while(j != 10);
	std::cout <<"Congratulations! You scored full point!" <<std::endl;
	return 0;
}
Topic archived. No new replies allowed.