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.
#include <iostream>
#include <ctime>
usingnamespace 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((unsignedint)time(NULL));
quiz();
system("pause");
return 0;
}
You are trying to call generateQuestionAndAnswer and answerCorrect before they are declared.
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.
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... ;-)
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;
}
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 (==).
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.