how to call out the function. C++ keeps saying identifier not found

I am sorry. I cant call out the function. C++ keeps saying identifier not found.
Thank you so much!

Last edited on
Original OP:
I am sorry. I cant call out the function. C++ keeps saying identifier not found.
Thank you so much!


Task:
1. Create a New Project and give your project the name Lab3b2.
2. Add a source file to your project, called RemainderTest.cpp.
3. Write a remainder test, which performs the following:
Computers are playing an increasing role in education. Write a program that will help an
elementary school students learn remainder calculation.
A) Use srand and rand to produce two positive one-digit integers in range 1 to 9.
B) The program should then prompt the user with a question, such as:
How much is the remainder of 6 divided by 7?
C) Now, ask the student to input the answer.
D) Next, the program checks the student’s answer. If it is correct, display the message “Very
Good!” and ask another remainder question. If the answer is wrong, display the message
“No. Please try again.” and let the student try the same question repeatedly until the
student finally gets it right.
E) A sentinel value -1 is used to indicate the end of the program.
(Note: Separate functions should be used to generate each new one-digit integers and
perform the checking. These functions should be called once when the application begins
execution.)
A sample screen display when the method is called is given below:
How much is the remainder of 1 divided by 8?
Enter your answer (-1 to exit): 1
Very Good!
How much is the remainder of 3 divided by 9?
Enter your answer (-1 to exit): 2
No. Please try again!
Enter your answer (-1 to exit): 3
Very Good!
How much is the remainder of 8 divided by 2?
Enter your answer (-1 to exit): -1
The input which is underlined is the user’s input to a question.
4. Compile your program and test it by executing your program.

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 <ctime>
using namespace std;

void quiz()
{
	
	int guess;
	int number1, number2, answer;
	int counter = 1;
	bool flag = true;


	do	
	{
		if(flag){
			// Pass two variables, number1 and number2, to function "generateQuestionAndAnswer"
			// The function will return back the answer of the remainder.
			// Hint: The variables should be PASSED BY REFERENCE
			answer = generateQuestionAndAnswer(number1, number2);
			cout << "How much is the remainder of " << number1 << " divided by " << number2 << "?" << endl;
		}
		cout << "Enter your answer (-1 to exit): ";
		cin >> guess;
		if( guess == -1 )
			break;

		// Pass two variables, guess and answer, to function "answerCorrect".
		// The function will return a boolean value true if guess is equal to answer,
		// otherwise, a boolean value false should be returned
		flag = answerCorrect( guess, answer );
		if(!flag)
			cout << "No. Please try again." << endl;
		else
			cout << "Very good!" << endl;
	}while( guess != -1 );
}

int generateQuestionAndAnswer( int a, int b ) { /* PASS-BY-REFERENCE */
    a % b ;
}

int answerCorrect( int c, int d ) { /* PASS-BY-REFERENCE */
    return (c = d) ? true : false;
}



int main()
{
	srand((unsigned int)time(NULL));
	quiz();
	system("pause");
	return 0;
}


You are trying to call generateQuestionAndAnswer and answerCorrect before they are declared.
Last edited on
you should include prototypes:

1
2
3
int answerCorrect( int c, int d );
int generateQuestionAndAnswer( int a, int b );
void quiz();

just after
using namespace std;

another way would be to rearrange your functions so that void quiz(){...} is between main() and answerCorrect( int c, int d )

Thanks so much you guys!!
I am such a fool.
But now it says my
uninitialized local variable 'number2' used and uninitialized local variable 'number1' used.
Why it says so ? I've opened number1 and number2 in the void (quiz) already.
I changed the type of answerCorrect from int to bool and add a return before a % b.

Last edited on
int number1, number2, answer;

you should assign a value to those variables. I think what you want to do here can be found at http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
Your number1 and number2 variables are uninitialised when you use them.

They're declared, but they don't store any values (or, perhaps more accurately, they're likely to be storing junk values).

So when you call generateQuestionAndAnswer passing in those variables, the compiler is warning you that you're using these variables but you haven't actually assigned anything to them.

EDIT: Also, I'm not sure if you're aware or not, but your comments in the functions denote that the parameters are passed by reference. Currently they're not; they're passed by value.

EDIT 2: And that ternary operation is the answerCorrect function is going to end up stinging you... ;-)
Last edited on
Firstly, thanks again!
Does it mean I need to assign values inside the number1, number2?
Is there other method instead of assign values?
It is because my teacher told us don't change his skeleton code.( my teacher didn't assign values)
Yeah, you need to assign values to number1 and number2.

No, there's no way to avoid this; if you're using values that you've not assigned anything to, then you don't know what they're doing or what the outcome is going to be. This is why you're getting compile time warnings.

Did your teacher tell you which bits of code you could and couldn't change? Because right now, those number variables are, functionally speaking, doing nothing.
Thanks.
except these few lines, all the other codes are skeleton code.
1
2
3
4
5
6
7
int generateQuestionAndAnswer( int a, int b ) { /* PASS-BY-REFERENCE */
    return a % b ;
     }

bool answerCorrect( int c, int d ) { /* PASS-BY-REFERENCE */
    return (c = d) ? true : false;
     }

the two functions are doing nothing??
No, they're doing something, just not something functionally relevant to your program requirements.

Take generateQuestionAndAnswer. You're passing in two values and returning the result of a modulus operation on those values. Yet you don't ever tell the program what the values are, so how can you possibly know what it's returning?

? % ? = ???????

answerCorrect isn't going to do what you want it to either. Remember the difference between assignments (=) and comparisons (==).
generateQuestionAndAnswer returns "a modulo b" for some reason, and answerCorrect returns true if d is not 0. I doubt that was what's intended.
Thank you!
o god.You are so right. These two mistakes are so dumb!
I added these code in the void so that it can function correctly.
But when my answer are correct, it keeps asking me the same question
(for example, How much is the remainder of 1 divided by 8?
Enter your answer (-1 to exit): 1
Very Good!
How much is the remainder of 1 divided by 8?)

Am I suppose to add a loop? Which loop is the best? And where should I put it?
I am kinda puzzled in these functions.


1
2
3
4
5
6
7
8
9
10
11
12
for(counter=1; counter<3; counter++)
		switch(counter)
	{
		case 1:
            number1 = rand() % 9 + 1;
			break;
		case 2:
            number2 = rand() % 9 + 1;
			if (number2 == number1){
			counter=1;
			continue;}
	}
I can fix it now!!Thank you all of you for paying so many attention to my dumb mistakes!
especially iHutch105!!
Again, thanks!
No problem.

Don't worry about mistakes, they'll happen to anyone, no matter what level the programmer. Sometimes the best debugger is another set of eyes.
Topic archived. No new replies allowed.