Call Function Inside of IF Statment

Im struggling to call the function inside of an if statement in the main(). How can I fix this? Also should I convert the int to string since I'm trying to get the first digit of the random number and the entered number?

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

int checkDigit(int * secretCode, int * guess) {
	if (guess[0] == secretCode[0]) {
		cout << "Your 1st Digit is Correct, Keep Trying!" << endl;
	}
}

int main()
{
	int cd;
	int secretCode = 0;
	int guess = 0;
	int guessCount = 0;
	int guessLimit = 8;
	bool outOfGuesses = false;
	srand(time(0));
	secretCode = rand() % 9000 + 1000;
	cout << "Code Breaker Game" << endl;
	cout << secretCode << endl;

	while (guess != secretCode && !outOfGuesses) {
		if (guessCount < guessLimit) {
			cout << "Please Enter a 4 Digit Code: " << endl;
			cin >> guess;
			guessCount++;
			checkDigit();
		}
		else {
			outOfGuesses = true;
		}
	}



	char input;
	if (outOfGuesses) {
		cout << "You Lose! Would you like to try again? [y/n]" << endl;
		cin >> input;
		if (input == 'y') {
			main();
		}
		else {
			cout << "Thanks for Playing" << endl;
		}
	}
	else {
		cout << "You win!" << endl;
	}
	return 0;
}


Im trying to call the checkDigit function inside the while and if statement.
Last edited on
Hello mahoward,

The function call on line 31 needs to match the function definition. The only difference is the function call only needs the variable names being sent. Not the type of variable.

Also in the function definition I would remove the '*'s, They are not needed.

Andy
Hello mahoward,

Sorry the call of nature was strong.

After looking at your code there are a few things I noticed.

The function int checkDigit(int * secretCode, int * guess) promises to return an int, but does not. Then in "main" the function call does nothing to capture the returned value.

In the function the if statement is trying to access the int as if it is an array. It is not an array just a number. To access the way that you are yould need to change the "int" into a "std::string" first.

In "main" you have initialized all your variables except "cd". Not sure what you intended to use it for, but it would be a good idea to initialize it to (0) zero.

Your "srand" will work, but the more up to date way is srand(static_cast<size_t>(time(nullptr)));.

On line 45 you call "main". You never call "main" in your program. This if for the operating to use when the program first runs. If you want to keep the program running use a do/while or while loop. For this I usually use a do/while loop.

Not at a point where I can run and test the program yet, but if I find anything else I will let you know.

Andy
Here's start - it checks the first number.

The challenge now is how you handle the other 3. You can stay with the integer :), you can create an array of integers :| or you can input the number as a string :( but it's up to you of course.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

void checkDigit(int &secretCode, int &guess) { // <--
    if (guess % 10 == secretCode % 10) { // <--
        cout << "Your 1st Digit is Correct, Keep Trying!" << endl;
    }
}

int main()
{
    int cd;
    int secretCode = 0;
    int guess = 0;
    int guessCount = 0;
    int guessLimit = 8;
    bool outOfGuesses = false;
    srand(time(0));
    secretCode = rand() % 9000 + 1000;
    cout << "Code Breaker Game" << endl;
    cout << secretCode << endl; 
    
    while (guess != secretCode && !outOfGuesses) {
        if (guessCount < guessLimit) {
            cout << "Please Enter a 4 Digit Code: " << endl;
            cin >> guess;
            guessCount++;
            checkDigit(secretCode, guess); // <--
        }
        else {
            outOfGuesses = true;
        }
    }
    
    
    
    char input;
    if (outOfGuesses) {
        cout << "You Lose! Would you like to try again? [y/n]" << endl;
        cin >> input;
        if (input == 'y') {
            main();
        }
        else {
            cout << "Thanks for Playing" << endl;
        }
    }
    else {
        cout << "You win!" << endl;
    }
    return 0;
}
Topic archived. No new replies allowed.