Question about an int checker

I've been scratching my head all day today trying to figure out how to make an "int checker". Essentially, I want my program to ignore "guesses" that are not numbers, cout an error message, and continue with the, "((wrong < MAXMISTAKES && guess != iSecret))" game loop . Also, my code is divided into two files; Source.cpp and test.cpp. Is it counter intuitive to write code this way?


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
//test.cpp
#include "test.h"
#include <iostream>
#include <string>
#include <stdio.h> 
#include <stdlib.h>    
#include <time.h>


using namespace std;
test::test()
{


	int guess(0);
	int iSecret;
	int wrong(0);
	int pause(0);
	const int MAXMISTAKES = 5;
	char ans;

	srand(time(NULL));	//initilizes random number
	iSecret = rand() % 10 + 1; //defines secret number

	printf("Guess a number between (1-10):\n");


	while ((wrong < MAXMISTAKES && guess != iSecret)){
		cin >> guess;


		if (guess < iSecret) {
			cout << "Your guess was low" << endl;
			++wrong;
		}

		if (guess > iSecret) {
			cout << "Your guess was too high" << endl;
			++wrong;
		}


		if (guess == iSecret) {
			cout << "Congrats, you're a winner" << endl;
			cout << "Would you like to try again?" << endl;
			cout << "Please enter y/n" << endl;

		}

		if (wrong == MAXMISTAKES) {

			cout << "Sorry, you're a loser?" << endl;
			cout << "Would you like to try again?" << endl;
			cout << "Please enter y/n" << endl;


		}
	}
}


and the main file

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
//Source.cpp
#include <test.h>
#include <iostream> 
#include <windows.h>
using namespace std;

int main() {

	HANDLE outcon = GetStdHandle(STD_OUTPUT_HANDLE);

	CONSOLE_FONT_INFOEX font;
	GetCurrentConsoleFontEx(outcon, false, &font);
	font.dwFontSize.X = 7;
	font.dwFontSize.Y = 12;
	SetCurrentConsoleFontEx(outcon, false, &font);
	SetConsoleTextAttribute(outcon, 0X03); //changes consol color


	char ans;

	do {
			test bo;
			cin >> ans;
		} while ((ans == 'y') || (ans == 'Y'));



		return 0;
	}

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
#include <iostream>

int get_checked_int( int min_value, int max_value )
{
    std::cout << "enter an integer in [" << min_value << '-' << max_value << "]: " ;

    int value ;
    if( std::cin >> value )
    {
        if( value >= min_value && value <= max_value ) return value ; // ok, return checked value

        else std::cerr << "value is out of range\n" ;
    }

    else
    {
        std::cerr << "invalid input\n" ;

        // http://www.cplusplus.com/reference/ios/ios/clear/
        std::cin.clear() ; // clear the failed state

        // http://www.cplusplus.com/reference/istream/istream/ignore/
        std::cin.ignore( 1000, '\n' ) ; // extract and discard invalid characters
    }

    return get_checked_int( min_value, max_value ) ; // try again
}


> my code is divided into two files; Source.cpp and test.cpp. Is it counter intuitive to write code this way?

No; in general, it is a good idea to write the code this way (split the code into multiple physical components.)
Last edited on
Thank you for your help! After an hour or two of tweaking my code I finally have my program working how I wanted it.

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
63
#include "test.h"
#include <iostream>
#include <string>
#include <stdio.h> 
#include <stdlib.h>    
#include <time.h>
#include <Checker.h>


using namespace std;
test::test()
{
	int guess(0);
	int iSecret;
	int wrong(0);
	int pause(0);
	const int MAXMISTAKES = 5;
	char ans;
	int min_value = 1;
	int max_value = 10;

	srand(time(NULL));	//initilizes random number
	iSecret = rand() % 10 + 1; //defines secret number

	printf("Guess a number between (1-10):\n");

	while ((wrong < MAXMISTAKES && guess != iSecret)) {
		cin >> guess;

		if ((guess >= min_value) && (guess <= max_value));

		else {
			cout << guess;
			Checker bo;
			cin.clear();
			cin.ignore(1000, '\n');
		}


		if (guess < iSecret & guess >= 1) {
			cout << "Your guess was low" << endl;
			++wrong;
		}

		if (guess > iSecret) {
			cout << "Your guess was too high" << endl;
			++wrong;
		}


		if (guess == iSecret) {
			cout << "Congrats, you're a winner" << endl;
			cout << "Would you like to try again?" << endl;

		}

		if (wrong == MAXMISTAKES) {

			cout << "Sorry, you're a loser?" << endl;
			cout << "Would you like to try again?" << endl;
		}
	}
}
Topic archived. No new replies allowed.