#include<iostream>
#include<cstdlib>
using namespace std;
//function prototypes
void problemGeneration(int, int, int);
void checkAnswer(int&, int, bool);
//declare global variables to be passed into functions
int main()
{
int num1 = rand() % 10;//random number between 0 and 9
int num2 = rand() % 10;//random number between 0 and 9
int answer = 0;
int product = num1 * num2;
bool isCorrect = false;
problemGeneration(num1, num2, answer);
checkAnswer(answer, product, isCorrect);
}
void problemGeneration(int num1, int num2, int answer)
{
cout <<"How much is" << " "<< num1 <<" " <<"times" << num2 << "?" << endl;
cin >> answer;
}
void checkAnswer(int &answer, int product, bool isCorrect)
{
if(answer == product)
{
isCorrect = true;
if(isCorrect = true)
{
cout << "Very Good!"<< endl;
problemGeneration(num1, num2, answer);
}
}
else
{
isCorrect = false;
cout << "No. Please Try Again. " << endl;
cin>> answer;
}
}
4 Replies - 830 Views - Last Post: 04 March 2011 - 10:32 AM
#1
Computer Assisted Instruction Undeclared identifier issues
Posted 04 March 2011 - 10:14 AM
I am working on a computer assisted instruction assignment. I've got the code written but every time I compile I get errors telling me that num1 and num2 on line 36 are undeclared identifiers. I can't figure out why they are undeclared when I declared them at the beginning of main. Any thoughts?
Replies To: Computer Assisted Instruction Undeclared identifier issues
#2
Re: Computer Assisted Instruction Undeclared identifier issues
Posted 04 March 2011 - 10:19 AM
Please post the exact and complete error messages. These messages have information to aid it deducing the problem.
Jim
Jim
#3
Re: Computer Assisted Instruction Undeclared identifier issues
Posted 04 March 2011 - 10:22 AM
jimblumberg, on 04 March 2011 - 10:19 AM, said:
Please post the exact and complete error messages. These messages have information to aid it deducing the problem.
Jim
Jim
The error messages that I got are:
1>c:\users\jason\documents\visual studio 2008\projects\computer-aided instruction\computer-aided instruction\computer-aided instruction.cpp(36) : error C2065: 'num1' : undeclared identifier
1>c:\users\jason\documents\visual studio 2008\projects\computer-aided instruction\computer-aided instruction\computer-aided instruction.cpp(36) : error C2065: 'num2' : undeclared identifier
#4
Re: Computer Assisted Instruction Undeclared identifier issues
Posted 04 March 2011 - 10:31 AM
You have declared the variables num1 and num2 in your main function and in problemGeneration. They exist only within those functions; they are not accessible by checkAnswer.
If you want to use them in checkAnswer, you have to send them to it as parameters. But do you really want to use them in that function?
In other words, should checkAnswer be calling problemGeneration, or should it return control to main and let main take care of calling the other functions?
If you want to use them in checkAnswer, you have to send them to it as parameters. But do you really want to use them in that function?
In other words, should checkAnswer be calling problemGeneration, or should it return control to main and let main take care of calling the other functions?
This post has been edited by r.stiltskin: 04 March 2011 - 10:33 AM
#5
Re: Computer Assisted Instruction Undeclared identifier issues
Posted 04 March 2011 - 10:32 AM
Quote
1>c:\users\jason\documents\visual studio 2008\projects\computer-aided instruction\computer-aided instruction\computer-aided instruction.cpp(36) : error C2065: 'num1' : undeclared identifier
Looking at this error message notice the line number where the error is detected is line 36. And line 36 is included in this snippet:
void checkAnswer(int &answer, int product, bool isCorrect)
{
if(answer == product)
{
isCorrect = true;
if(isCorrect = true)
{
cout << "Very Good!"<< endl;
problemGeneration(num1, num2, answer); //THIS IS LINE 36
}
}
else
{
isCorrect = false;
cout << "No. Please Try Again. " << endl;
cin>> answer;
}
}
So in the above snippet where are you defining the variables num1 and num2?
You might want to look at this.
Jim
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|