4 Replies - 1358 Views - Last Post: 04 March 2011 - 10:32 AM Rate Topic: -----

#1 programmingBeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-March 11

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?


#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;
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Computer Assisted Instruction Undeclared identifier issues

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

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
Was This Post Helpful? 0
  • +
  • -

#3 programmingBeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-March 11

Re: Computer Assisted Instruction Undeclared identifier issues

Posted 04 March 2011 - 10:22 AM

View Postjimblumberg, 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


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
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

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?

This post has been edited by r.stiltskin: 04 March 2011 - 10:33 AM

Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

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
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1