11 Replies - 1423 Views - Last Post: 11 February 2009 - 07:08 PM Rate Topic: -----

#1 redbloodcell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 09

Undeclared identifier: remainder

Post icon  Posted 10 February 2009 - 02:24 AM

hi, i continued my code to add division, now i got an undeclared identifier -- remainder. what does this mean?

// classes.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h> //required for fflush()

class math //declare math class, with 2 data members and 3 member functions
{
	public:
	 int num1; //num1 and num2 are members of the math class
	 int num2;
 
	 int addition(math* pointer)
	 {
		 return pointer->num1 + pointer->num2; //returns sum of num1 and num2
	 }

	 int subtraction(math* pointer) //returns difference of num1 and num2
	 {
		 return pointer->num1 - pointer->num2;
	 }

	 int multiplication(math* pointer) //returns product of num1 and num2
	 {
		int ans = 0;
		for (int i=1; i<= pointer->num2; i++) //adds num1 to itself for num2 amount of times
			ans += pointer->num1;

		return ans;
	}

	 int division(math* pointer, int& k) //returns quotient of num1 and num2
	 {
		 int quotient;
		 while (pointer->num1 >= pointer->num2) //loops as long as dividend >= divisor
			{
				++quotient; //increments quotient by 1 each time the divisor is subtracted from dividend
				pointer->num2 -= pointer->num1; //at the end of the while loop, dividend becomes remainder.
			}
		 k = pointer->num2;
			return quotient;
	}

};

void getnumbers (int& i, int& j, int choice) //scans in values for num1 and num2
{
	char* opname[]={"added", "subtracted", "mulitplied", "divided"}; //determines phrasing of second sentence
	printf ("Please enter a number: \n");
	fflush(stdin); //flushes out previous inputs
	scanf ("%d", &i);
	printf ( "Please enter another number to be %s: \n", opname[choice]);
	scanf ("%d", &j);

}

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
{
	math operation;
	math* pointer = &operation; //pointer to address of operation, object of math class
	int& i = operation.num1; //these references will be arguments to the hmm function
	int& j = operation.num2; //and will be modified accordingly

	printf ("\nWelcome to my calculator. Select a number below: \n");
	printf ("0: Addition\n");
	printf ("1: Subtraction\n");
	printf ("2: Multiplication\n");
	printf ("3: Division\n");

	int choice;
	fflush(stdin); //flushes out previous inputs
	scanf ("%d", &choice); 

	getnumbers(i, j, choice);
	
	
	//prints answer according to choice
	if (choice == 0)
		printf ("Answer: %d\n\n", operation.addition(pointer));
	if (choice == 1)
		printf ("Answer: %d\n\n", operation.subtraction(pointer));
	if (choice == 2)
		printf ("Answer: %d\n\n", operation.multiplication(pointer));
	if (choice == 3)
		int remainder;
		int& k = remainder;
		printf ("Quotient: %d\n", operation.division(pointer, k));
		printf ("Remainder: %d\n\n", remainder);

	return 0;
}





EDIT:

i just shifted
int remainder;
int& k = remainder;

to right above main
and it compiles fine now!

this is mysterious!

anyway, while my code compiles fine,
it does not display the quotient and remainder as i desired.

ideas? the division function -- while awkward -- was meant to test my knowledge of while loops. it works, as i extracted it from another function, before i embarked on using classes.

Is This A Good Question/Topic? 0
  • +

Replies To: Undeclared identifier: remainder

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Undeclared identifier: remainder

Posted 10 February 2009 - 02:29 AM

Missing braces: :)
if (choice == 3) {
	int remainder;
	int& k = remainder;
	printf ("Quotient: %d\n", operation.division(pointer, k));
	printf ("Remainder: %d\n\n", remainder);
}


Need to initialize quotient :
 int division(math* pointer, int& k) //returns quotient of num1 and num2
 {
	 int quotient = 0;//<<<<<<<<<<<<<<<
	 while (pointer->num1 >= pointer->num2) //loops as long as dividend >= divisor
		{
			++quotient; //increments quotient by 1 each time the divisor is subtracted from dividend
...


Was This Post Helpful? 0
  • +
  • -

#3 redbloodcell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 09

Re: Undeclared identifier: remainder

Posted 10 February 2009 - 02:35 AM

can't believe i mised those.
thanks a million :D
Was This Post Helpful? 0
  • +
  • -

#4 redbloodcell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 09

Re: Undeclared identifier: remainder

Posted 10 February 2009 - 02:41 AM

EDIT:
even after changing those egregious errors,
the division function still doesn't work!
Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 276
  • View blog
  • Posts: 1,780
  • Joined: 20-September 08

Re: Undeclared identifier: remainder

Posted 10 February 2009 - 01:08 PM

View Postredbloodcell, on 10 Feb, 2009 - 01:41 AM, said:

EDIT: even after changing those egregious errors,
the division function still doesn't work!



The division routine was a little tricky to get it just right ... ?

You might like to see these modifications below ...

Shalom,
David

// pointers to classes ...

#include <cstdio>   // re. printf(..)
#include <climits>  // re. INT_MIN, INT_MAX

class Math
{
public:
	//num1, num2 and remainder are PUBLIC members of the Math class
	int num1; 
	int num2;
	int remainder;

	int addition( Math* pMath )
	{
		return pMath->num1 + pMath->num2; //returns sum of num1 and num2
	}

	int subtraction( Math* pMath ) //returns difference of num1 and num2
	{
		return pMath->num1 - pMath->num2;
	}

	int multiplication( Math* pMath ) //returns product of num1 and num2
	{
		int sign = 1;
		if( pMath->num2 < 0 ) { pMath->num2 = -pMath->num2; sign = -1; }
		int ans = 0;
		//adds num1 to itself for num2 amount of times
		for( int i=0; i< pMath->num2; i++ )
			ans += pMath->num1;
		return ans*sign;;
	}

	int division( Math* pMath )
	{
		//get sign of 'botton' and if necessary set bottom to 'positive'
		int signBot = 1;
		if( pMath->num2 < 0 ) { pMath->num2 = -pMath->num2; signBot = -1; }
		
		int quotient = 0; //positive numbers only ...
		remainder = pMath->num1;
		
		//case (1) ... divide by 0 ...
		if( pMath->num2 == 0 )
		{
			printf("\nDivide by zero ERROR!\n");
			remainder = 0;
			return 0;
		}
		
		//case (2) top zero
		if( pMath->num1 == 0 ) { remainder = 0; return 0; }
		
		//case (3) ...
		int signTop = 1;
		if( remainder < 0 ) { remainder = -remainder; signTop = -1; }
		
		//case (3) abs(top) < bottom ... see (6) for top >= bot >0
		if( remainder < pMath->num2 )
		{
			remainder *= signTop;
			return 0;
		}
		
		//else ... case (4) abs(top) >= bottom ... remainder is positive after (3) above
		while( remainder >= pMath->num2 ) //num1/num2 ...
		{
			//increments quotient as the divisor is subtracted from dividend
			++quotient;
			remainder -= pMath->num2;
		}
		remainder *= signTop*signBot;
		return quotient*signTop*signBot;
	}// END DIVISION
};// END CLASS

// gets values for num1 and num2 ...
void getNumbers( int& i, int& j, int index )
{
	int flag = INT_MIN;
	
	// ragged array of names used with 'index'  ... to select 'add', 'sub...' ...
	char* opname[]={"add", "subtract", "multiply", "divide by"};

	for(;; )
	{
		printf ("Please enter a number: ");
		i = flag; // each time ... re-set i, j to flag value ...
		scanf( "%d", &i );
		while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...
		printf ( "Please enter another number to %s: ", opname[index] );
		j = flag;
		scanf( "%d", &j );
		while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...
		if( i == flag || j == flag )
		{
			printf
			(
				"\nEntry error!  Integers only please in the range %d..%d\n",
				INT_MIN+1, INT_MAX
			);
			continue;
		}
		break; // since if we reach here we have 2 good numbers ...
	}
}

int main() /////////////////////// MAIN ////////////////////////////////////////
{
	printf
	(
		"The INT_MIN choice is %d and the INT_MAX choice is %d ...\n\n",
		INT_MIN+1, INT_MAX
	);
	
	Math op;
	
	// pMath to address of op, object of Math class
	Math* pMath = &op;
	
	// these references will be arguments to the getNumbers function
	// and will be modified accordingly
	int& i = op.num1;
	int& j = op.num2;

	for(;; ) // forever loop ... until 'return 0;' called in main ...
	{
		printf
		(
			"Welcome to my calculator. Your choices are ...\n"
			"0: Addition\n"
			"1: Subtraction\n"
			"2: Multiplication\n"
			"3: Division\n"
			"4: Exit\n"
			"Please enter a number in the range 0..4 : "
		);

		int choice = -1;
		scanf( "%d", &choice );
		while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...
		
		if( choice >= 0 && choice <= 3 )
			getNumbers( i, j, choice );	// don't call this if INVALID choice

		// prints answer according to choice
		if( choice == 0 )
			printf( "Answer: %d\n\n", op.addition(pMath) );
		else if( choice == 1 )
			printf( "Answer: %d\n\n", op.subtraction(pMath) );
		else if( choice == 2 )
			printf( "Answer: %d\n\n", op.multiplication(pMath) );
		else if( choice == 3 )
		{
			printf( "Quotient: %d\n", op.division(pMath) );
			printf( "Remainder: %d\n\n", op.remainder );
		}
		else if( choice == 4 )
			return 0;
		else
			printf( "This choice %d is INVALID ... \n\n", choice );
	}
} ///////////////////////////////// END MAIN ///////////////////////////////////



Was This Post Helpful? 0
  • +
  • -

#6 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 276
  • View blog
  • Posts: 1,780
  • Joined: 20-September 08

Re: Undeclared identifier: remainder

Posted 10 February 2009 - 04:56 PM

Or ... a little more classy :D approach ... with still some pointers ...


// still some pointers to classes ...

#include <cstdio>   // re. printf(..)
#include <climits>  // re. INT_MIN, INT_MAX


class Math
{

public:

	Math() {} // default constructor ...
	Math( int );
	
	int get_remainder() { return remainder; }

	int addition( Math* pMath )
	{
		return pMath->num1 + pMath->num2; //returns sum of num1 and num2
	}

	int subtraction( Math* pMath ) //returns difference of num1 and num2
	{
		return pMath->num1 - pMath->num2;
	}
	
	int multiplication( Math* pMath ) //returns product of num1 and num2
	{
		//get sign of 'bottonm' and if necessary set bottom to 'positive'
		int signBot = 1;
		if( pMath->num2 < 0 ) { pMath->num2 = -pMath->num2; signBot = -1; }
		
		int ans = 0;
		
		//adds num1 to itself for num2 amount of times
		for( int i=0; i< pMath->num2; i++ )
			ans += pMath->num1;
		return  ans * signBot; // fix sign if signBot was originally negative
	}

	int division( Math* pMath )
	{
		//get sign of 'bottonm' and if necessary set bottom to 'positive'
		int signBot = 1;
		if( pMath->num2 < 0 ) { pMath->num2 = -pMath->num2; signBot = -1; }

		int quotient = 0; //positive numbers only ...
		remainder = pMath->num1;

		//case (1) ... divide by 0 ...
		if( pMath->num2 == 0 )
		{
			printf("\nDivide by zero ERROR!\n");
			remainder = 0;
			return 0;
		}

		//case (2) top zero
		if( pMath->num1 == 0 ) { remainder = 0; return 0; }

		//re. case (3) & (4) below ...
		int signTop = 1;
		if( remainder < 0 ) { remainder = -remainder; signTop = -1; }

		//case (3) abs(top) < bottom ... see (4) for abs(top) >= bot >0
		if( remainder < pMath->num2 )
		{
			remainder *= signTop;
			return 0;
		}

		//else ... case (4) abs(top) >= bottom ...
		//recall that the remainder is positive after (3) above
		while( remainder >= pMath->num2 ) //num1/num2 ...
		{
			//increments quotient as the divisor is subtracted from dividend
			++quotient;
			remainder -= pMath->num2;
		}
		remainder *= signTop*signBot;
		return quotient*signTop*signBot;
	}// END DIVISION
	
private:

	int num1;
	int num2;
	int remainder;

};// END CLASS


// get and set values for num1 and num2 ... for each choice ...
// of index ... that 'indexes' one of 'add', 'sub', 'mult' or 'div'
Math::Math( int index )
{
	int flag = INT_MIN; // from <climits>
	
	// ragged array of names used with 'index'  ... to select 'add', 'sub...' ...
	char* opname[] = {"add", "subtract", "multiply", "divide by"};

	for(;;)
	{
		printf ("Please enter a number: ");
		int i = flag; // each time ... re-set i, j to flag value ...
		scanf( "%d", &i );
		while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...
		printf( "Please enter another number to %s: ", opname[index] );
		int j = flag;
		scanf( "%d", &j );
		while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...
		if( i == flag || j == flag )
		{
			printf
			(
				"\nEntry error!  Integers only please in the range %d..%d\n",
				INT_MIN+1, INT_MAX
			);
			continue;
		}
		num1 = i; // now set private member variables with this 'good' data ...
		num2 = j;
		break; // since if we reach here we 'have' 2 good numbers ...
	}
}



int main() ///// BEGIN MAIN /////
{
	printf
	(
		"< The INT_MIN choice is %d, the INT_MAX choice is %d >\n\n",
		INT_MIN+1, INT_MAX
	);
	
	for(;;) // forever loop ... until 'return 0;' called in main ...
	{
		printf
		(
			"Welcome to 'class Math'\n"
			"0: Addition\n"
			"1: Subtraction\n"
			"2: Multiplication\n"
			"3: Division\n"
			"4: Exit\n"
			"Please enter a number in the range 0..4 ? "
		);
		
		// get pMath to hold the address of op ... op is an object of class 'Math'
		Math op;
		Math* pMath = &op; // a bit of a 'work-around' here * for below *

		int choice = -1;
		scanf( "%d", &choice );
		while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...
		
		if( choice >= 0 && choice <= 3 )
		{
			Math op2( choice ); // don't call this constructor if INVALID choice
			op = op2; // * now op with scope outside this 'if' has the values *
		}
		// prints answer according to choice
		if( choice == 0 )
			printf( "Answer: %d\n\n", op.addition(pMath) );
		else if( choice == 1 )
			printf( "Answer: %d\n\n", op.subtraction(pMath) );
		else if( choice == 2 )
			printf( "Answer: %d\n\n", op.multiplication(pMath) );
		else if( choice == 3 )
		{
			printf( "Quotient: %d\n", op.division(pMath) );
			printf( "Remainder: %d\n\n", op.get_remainder() );
		}
		else if( choice == 4 )
			return 0;
		else
			printf( "This choice %d is INVALID ... \n\n", choice );
	}
} ///// END MAIN ///// 

Was This Post Helpful? 0
  • +
  • -

#7 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 276
  • View blog
  • Posts: 1,780
  • Joined: 20-September 08

Re: Undeclared identifier: remainder

Posted 10 February 2009 - 05:59 PM

No 'point' here ... all 'class' ... :)

// All Class ...

// Simplified pointers to class Math ... More class ... What pointers?

#include <cstdio>   // re. printf(..)
#include <climits>  // re. INT_MIN, INT_MAX

class Math
{
public:

	Math() {} // default constructor ...
	Math( int );
	
	int get_remainder() { return remainder; }

	int addition()
	{
		return num1 + num2; // returns sum of num1 and num2
	}

	int subtraction() // returns difference of num1 and num2
	{
		return num1 - num2;
	}

	int multiplication() //returns product of num1 and num2
	{
		//get sign of 'bottonm' and if necessary set bottom to 'positive'
		int signBot = 1;
		if( num2 < 0 ) { num2 = -num2; signBot = -1; }

		int ans = 0; // declare and initialize to zero to hold sums ...

		//adds num1 to itself for num2 amount of times
		for( int i = 0; i< num2; ++i )
			ans += num1;
		return  ans * signBot; // fix sign if signBot was originally negative
	}

	int division()
	{
		//get sign of 'bottonm' and if necessary set bottom to 'positive'
		int signBot = 1;
		if( num2 < 0 ) { num2 = -num2; signBot = -1; }

		int quotient = 0; //positive numbers only ...
		remainder = num1;

		//case (1) ... divide by 0 ...
		if( num2 == 0 )
		{
			remainder = 0; // 0 quotient, 0 remainder is ERROR value returned
			return 0; // return from this division function right now ... (1)<--
			printf("\nDivide by zero ERROR!\n");
		}

		//case (2) top zero
		if( num1 == 0 ) { remainder = 0; return 0; } // return (2) <------------

		//re. case (3) & (4) below ...
		int signTop = 1;
		if( remainder < 0 ) { remainder = -remainder; signTop = -1; }

		//case (3) abs(top) < bottom ... see (4) for abs(top) >= bot >0
		if( remainder < num2 )
		{
			remainder *= signTop;
			return 0; // return (3) <-------------------------------------------
		}

		//else ... case (4) abs(top) >= bottom ...
		//recall that the remainder is positive after (3) above
		while( remainder >= num2 ) //num1/num2 ...
		{
			//increments quotient as the divisor is subtracted from dividend
			++quotient;
			remainder -= num2;
		}
		remainder *= signTop*signBot;
		return quotient*signTop*signBot; // return (4) <------------------------
	}// END DIVISION

private:

	int num1;
	int num2;
	int remainder;

};// END CLASS


// get and set values for num1 and num2 ... for each choice ...
// of index ... that 'indexes' one of 'add', 'sub', 'mult' or 'div'
Math::Math( int index )
{
	int flag = INT_MIN; // from <climits>
	
	// ragged array of names used with 'index'  ... to select 'add', 'sub...' ...
	char* opname[]={ "add", "subtract", "multiply", "divide by" };

	for(;;) // loop forever ... until break ...
	{
		printf ("Please enter a number: ");
		int i = flag; // each time ... re-set i, j to flag value ...
		scanf( "%d", &i );
		while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...
		printf ( "Please enter another number to %s: ", opname[index] );
		int j = flag;
		scanf( "%d", &j );
		while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...
		if( i == flag || j == flag )
		{
			printf
			(
				"\nEntry error!  Integers only please in the range %d..%d\n",
				INT_MIN+1, INT_MAX
			);
			continue; // from the top of the forever loop right now ...
		}
		num1 = i;   // now set private member variables with this 'good' data
		num2 = j;
		break;	  // since if we did reach here ... we must have 2 good numbers
	}
}


int main() ///// BEGIN MAIN /////
{
	printf
	(
		"< The INT_MIN choice is %d, the INT_MAX choice is %d >\n\n",
		INT_MIN+1, INT_MAX
	);
	
	for(;;) // forever loop ... until 'return 0;' called in main ...
	{
		printf
		(
			"Welcome to 'class Math'\n"
			"0: Addition\n"
			"1: Subtraction\n"
			"2: Multiplication\n"
			"3: Division\n"
			"4: Exit\n"
			"Please enter a number in the range 0..4 ? "
		);

		int choice = -1;
		scanf( "%d", &choice );
		while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...
		Math op;
		if( choice >= 0 && choice <= 3 )
		{
			// don't call this constructor if choice is INVALID ...
			Math op2( choice ); 
			op = op2; // now op is 'known' outside the inner scope of this 'if'
		}
		
		// prints answer according to 'valid' choice
		if( choice == 0 )
			printf( "Answer: %d\n\n", op.addition() );
		else if( choice == 1 )
			printf( "Answer: %d\n\n", op.subtraction() );
		else if( choice == 2 )
			printf( "Answer: %d\n\n", op.multiplication() );
		else if( choice == 3 )
		{
			printf( "Quotient: %d\n", op.division() );
			printf( "Remainder: %d\n\n", op.get_remainder() );
		}
		else if( choice == 4 )
			return 0;
		else
			printf( "This choice %d is INVALID ... \n\n", choice );
	}
} ///// END MAIN /////  

This post has been edited by David W: 10 February 2009 - 06:00 PM

Was This Post Helpful? 0
  • +
  • -

#8 redbloodcell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 09

Re: Undeclared identifier: remainder

Posted 10 February 2009 - 06:05 PM

View PostDavid W, on 10 Feb, 2009 - 03:56 PM, said:

scanf( "%d", &choice );
while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...


thanks i just have one question:
is the above same as
do scanf( "%d", &choice );
while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...

because that is the only way i can figure that out. is there a rule in C++ that if in a do-while loop, the do only has one statement then you can leave the do out?
Was This Post Helpful? 0
  • +
  • -

#9 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 276
  • View blog
  • Posts: 1,780
  • Joined: 20-September 08

Re: Undeclared identifier: remainder

Posted 10 February 2009 - 06:26 PM

while( 1 ) ; /* <--- this line will loop for ever ...*/

If ...

it is CERTAIN that there is at least 1 '\n' char still in the stdin buffer ...

then ...

while( getchar() != '\n' );

WILL be ok ... and will exit when that '\n' is read ...

The above is a bit of brief coding style that C (or C++) users can use on SOME occasions only.

Shalom,
David

P.S.

If the last char read was in c

int c = getchar();

and if you are NOT sure what it is ... perhaps it is the terminal '\n' char ...

then test like this ...

int cTmp = c;
while( cTmp != '\n' ) cTmp = getchar(); /* flush whatever ... */

This post has been edited by David W: 10 February 2009 - 06:35 PM

Was This Post Helpful? 0
  • +
  • -

#10 redbloodcell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 09

Re: Undeclared identifier: remainder

Posted 11 February 2009 - 05:45 PM

there's something i didn't quite understand in David's second code.

i modified the code slightly to remove the use of op2:
int choice = -1;
		scanf( "%d", &choice );
		while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...
		
		if( choice >= 0 && choice <= 3 )
		{
			Math op(choice);
			Math* pMath = &op;
		}
		// prints answer according to choice
		if( choice == 0 )
			printf( "Answer: %d\n\n", op.addition(pMath) );
		else if( choice == 1 )
			printf( "Answer: %d\n\n", op.subtraction(pMath) );
		else if( choice == 2 )
			printf( "Answer: %d\n\n", op.multiplication(pMath) );
		else if( choice == 3 )
		{
			printf( "Quotient: %d\n", op.division(pMath) );
			printf( "Remainder: %d\n\n", op.get_remainder() );
		}
		else if( choice == 4 )
			return 0;
		else
			printf( "This choice %d is INVALID ... \n\n", choice );
	}



it didn't work. is it because op must be defined right at the very start instead of within the conditional

This post has been edited by redbloodcell: 11 February 2009 - 05:52 PM

Was This Post Helpful? 0
  • +
  • -

#11 redbloodcell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 09

Re: Undeclared identifier: remainder

Posted 11 February 2009 - 06:06 PM

actually i think i understand now. it's got to do with the scope of the variables, if defined at the very start, the op has scope for everything within the main brace, and this includes within and otuside the conditional.
op2 only exists in the scope of the conditional.

here is my second question.
i tried changing the block of if and else if statements to a switch statement,

from
int choice = -1;
		scanf( "%d", &choice );
		while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...
		
		if( choice >= 0 && choice <= 3 )
			getNumbers( i, j, choice );	// don't call this if INVALID choice

		// prints answer according to choice
		if( choice == 0 )
			printf( "Answer: %d\n\n", op.addition(pMath) );
		else if( choice == 1 )
			printf( "Answer: %d\n\n", op.subtraction(pMath) );
		else if( choice == 2 )
			printf( "Answer: %d\n\n", op.multiplication(pMath) );
		else if( choice == 3 )
		{
			printf( "Quotient: %d\n", op.division(pMath) );
			printf( "Remainder: %d\n\n", op.remainder );
		}
		else if( choice == 4 )
			return 0;
		else
			printf( "This choice %d is INVALID ... \n\n", choice );



to
int choice = -1;
		scanf( "%d", &choice );
		while( getchar() != '\n' ); // flush stdin as we go to keep it flushed ...
		
		if( choice >= 0 && choice <= 3 )
		{
			Math op2(choice);
			op = op2;
		}
		
		switch (choice) //executes a function below based on user input 
								//then prompts the user to either quit or return to main menu
			{
				case '0':
						printf( "Answer: %d\n\n", op.addition(pMath) );
						break;
				case '1':
						printf( "Answer: %d\n\n", op.subtraction(pMath) );
						break;
				case '2':
						printf( "Answer: %d\n\n", op.multiplication(pMath) );
						break;
				case '3':
						printf( "Quotient: %d\n", op.division(pMath) );
						printf( "Remainder: %d\n\n", op.get_remainder() );
						break;
				case '4':
						return 0;
				default:
						printf( "This choice %d is INVALID ... \n\n", choice1);
		}   



and i got:
error C2065: 'choice' : undeclared identifier

hmmmmmm. any idea why? what is the correct way to implement such a switch statement?
Was This Post Helpful? 0
  • +
  • -

#12 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 276
  • View blog
  • Posts: 1,780
  • Joined: 20-September 08

Re: Undeclared identifier: remainder

Posted 11 February 2009 - 07:08 PM

int choice;
scanf( "%d", &choice );

means ...
choice is an integer here then ....

you have ...
case '0':

try using
case 0:
etc ...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1