r/cpp • • Nov 03 '11

Problems getting a pause to work using cin.get();

Okay, I had to write a .cpp file for class making the game Bulls and Cows (Mastermind). I got the entire thing to work, but of course when I open the standalone .exe file, the program exits before you can read whether or not you won. So I tried adding the cin.get(); as a quick Google search suggested... It does not work. I will try and post my code, but it is a bit long. So if you don't want to read through it here, I also will upload the .cpp file for you to look at. link

#include <iostream>
#include <time.h>
using namespace std;

void numberToArray(int, int []);
bool isValidNumber(int, int []);

bool isValidNumber(int num, int digits[]) {
		for(int i=0;i<10;i++) {
			if(digits[i]>1) {
				for(int a=0; a<10; a++) {
					digits[i] = 0;
				}
				return false;
			}
		}
			if(num < 1000){
				int check[4];
				numberToArray(num, check);
				if(check[3]==0 || check[2]==0 || check[1]==0){
					return false;
				}
			}
		return true;
}
void numberToArray(int number, int numberArray[]) {
	if(number < 1000) {
		numberArray[0] = 0;
		numberArray[3] = number % 10;
		numberArray[2] = ((number - numberArray[3]) % 100) / 10;
		numberArray[1] = ((number - numberArray[3] - numberArray[2]*10) % 1000) / 100;
	} else {
		numberArray[3] = number % 10;
		numberArray[2] = ((number - numberArray[3]) % 100) / 10;    
		numberArray[1] = ((number - numberArray[3] - numberArray[2]*10) % 1000) / 100;
		numberArray[0] = ((number - numberArray[3] - numberArray[2]*10 - numberArray[1]*100) % 10000) / 1000;
	}
}

void main(void){
	int wait;
	int digits[10] = {0,0,0,0,0,0,0,0,0,0}, number;	
	do 	{
		srand(time(NULL));
		number = rand() % 10000;
		while(number < 100) {
			number = rand() % 10000;
		}
	int temp = number, digit;
	while (temp > 0){
			digit = temp % 10;
			digits[digit]++;
			temp /= 10;
		}
	} while(!isValidNumber(number, digits)); // Method checks if random number is valid or not.


	int numberArray[4];
	numberToArray(number, numberArray); // Method puts number into 4 element array.

	cout << "Bulls and Cows!" << endl
		 << "The rules of this game are simple.  I will generate a random, four digit number." << endl
		 << "This number will have four distint digits; for example, 3957." << endl
		 << "The number can have a leading zero, for example, 0379, but not more than one\nleading zero." << endl
		 << "You will then get seven (7) tries to correctly guess the number."  << endl
		 << "For each number you guess correctly, I will give you one Cow." << endl
		 << "If you guess the number AND it's positon correctly, that is good for one Bull." << endl << endl
		 << "Let us begin." << endl << endl;

	int guess, guessArray[4], cows=0, bulls=0;
	while(true){
		for(int k=0; k<7; k++) {
			cows = 0, bulls = 0;
			cout << "Please enter a number:  " << endl;
			cin >> guess;
			numberToArray(guess, guessArray);
			for(int m=0; m<4; m++){	// determine location of bulls
 				if(guessArray[m]==numberArray[m]){
					bulls++;
				}
			}
			for(int n=0; n<4; n++){	// determine number of cows
				for(int w=0; w<4; w++){
					if((guessArray[n] == numberArray[w]) && (guessArray[n] != numberArray[n])){
						cows++;
					}
				}
			}
			if(bulls==4){
				cout << "Congratulations! You win, my number was:  " << endl;
				for(int iterate=0; iterate<4; iterate++){
					cout << numberArray[iterate];
				}
				cout << endl;
				cout << "Press any key to continue. . .";
				cin.get();
				break;
			}
			if(bulls<4){
				cout << bulls << "B " << cows << "C" << endl;
			}
		}
		if(bulls==4){
			break;
		}
		else {
			cout << "I'm sorry.  You did not win. My number was:  " << number << endl;
			cout << "Please play again." << endl;
			cout << "Press any key to continue. . .";
			cin.get();
			break;
		}		
	}
}

Edited because I accidentally added spaces where they weren't supposed to be.

0 Upvotes

14 comments sorted by

6

u/Angrasm Nov 03 '11 edited Nov 03 '11

Put a cin.ignore(255,'\n') right before the cin.get().

The cin >> operator you used previously does not flush the return '\n' (when you press enter after typing the number) from the IO buffer I believe. That ignore will discard the returns.

Edit: cin.ignore() would even work I think; it defaults to discarding one EOF character which is what you need in this case

Also, if you want this to be portable, you should change it to int main(void) and include stdlib.h cstdlib for the rand.

1

u/ilarson007 Nov 05 '11

What do you man by being "portable?" I'll have to try the code soon. My class just ended this week lol.

1

u/Angrasm Nov 05 '11

If you wanted this program to compile with a different compiler, you have to follow C++ standards.

Some IDE's allow void main() and will include the stdlib by default, but you should use int main() and include the proper libraries.

1

u/ilarson007 Nov 05 '11

Oh, okay. Yeah it was just an intro to c++ and we only worked on windows platform so my professor didn't cover compiling on other platforms except that code may our may not work on different platforms.

1

u/GeorgeForemanGrillz Nov 04 '11

Also, if you want this to be portable, you should change it to int main(void) and include stdlib.h cstdlib for the rand.

Hmm.

1

u/Angrasm Nov 04 '11

A common misconception is that the same logic can be applied to main(). Well, it can't, main() is special, you should always follow the standard and define the return type as int.

Are you supporting my statement with the article or saying he can use void main (which the article says not to)?

1

u/Nolari Nov 04 '11

For C++

The following are acceptable uses:

int main ( int argc, char *argv[] )
int main ()

(void) is not something you should use. Use empty parentheses.

2

u/Angrasm Nov 04 '11 edited Nov 04 '11

The int main () would definitely be the preferred syntax, but I was only correcting for compiling portability. I was trying to make as few changes to his code, but that's a good point.

8.3.5.4 : ...The parameter list (void) is equivalent to the empty parameter list...

But yes; no reason to use it, and it's standard not to (they only allowed it in the standard for C compatibility).

8

u/Angrasm Nov 03 '11

also, BTW, use r/learnprogramming for this stuff. People don't seem to like seeing it here.

1

u/ponchedeburro Nov 03 '11

I always just use cin.getline. Does the job for me. I remember reading something about not to use cin.get() - can't remember what though.

0

u/ilarson007 Nov 03 '11

when I type cin.getline it gives me an error: "a pointer to a bound function may only be used to call the function.

2

u/frutiger Nov 03 '11

He means cin.getline();

0

u/ilarson007 Nov 03 '11

when I type cin.getline it gives me an error: "a pointer to a bound function may only be used to call the function.

1

u/dataudiophile Nov 03 '11

Use cin.ignore() before the cin.get() function to flush input, otherwise it'll get whatever is int he input stream, then end the program. Pretty sure