Hi, it's been awhile since I've been here and the site has changed quite a bit, I like it. Anyway, I attempted to make a basic calculator program, but I'm getting some weird error messages. The error messages tell me that there are syntax errors, but I've looked through the code and I can't find any. Here is the code, and below that are the error messages:

/*
 * BCalc:
 * A calculator program with add, subtract, multiply, and divide functions.
 *
 * Programmer: 
 */
#include <iostream>
using namespace std;

float number = 0;

int get_choice(int &choice);
void handle_choice(int choice);
float add_num(float number);
float subtract_num(float number);
float multiply_num(float number);
float divide_num(float number);

 
int main()
{
	int choice;

	while(get_choice(choice)); //while get_choice returns 1 call handle_choice function.
	{
		handle_choice(choice);
	}
	
	return 0;  //end of program
}


int get_choice(int &choice)
{
	do
	{
		cout << "Select operation: \n";
		cout << "1 ~ addition.\n";
		cout << "2 ~ subtraction.\n";
		cout << "3 ~ multiplication.\n";
		cout << "4 ~ division.\n";
		cout << "5 ~ clear\n";
		cout << "6 ~ exit program.\n";
		cin >> choice;
		
		if ((choice < 7 ) || (choice > 0))
		{
			cout << "invalid entry." << endl;
		}

		if (choice == 6)  //if choice equals 6 exit return 0 and exit program 
		{
			return 0;
		}
	}while((choice < 7) || (choice > 0));
	return 1;
}


void handle_choice(int choice)
{
	switch(choice)
		{
		case 1:
			number = add_num(number); //assign value returned by function to global variable number
			break;

		case 2:
			number = subtract_num(number);
			break;

		case 3:
			number = multiply_num(number);
			break;

		case 4:
			number = divide_num(number);
			break;

		case 5:
			number = 0;   //if 5 is entered set number variable to 0
			break;
		
		default:
			cout << "invalid entry" << endl;
			break;
		}
}


float add_num(float number)
{
	float i = 0;
	float j = 0;
	char return_main = 'C';

	do
	{
		if(number == 0)             //if number is 0 prompt user for two numbers to add.
		{
			cout << "enter first number to add ";
			cin >> i;
			cout << "Enter second number to add ";
			cin >> j;
			number = i + j;
			cout << number;
		}

		else                    //if number is not 0 prompt user for the number to be added
		{
			cout << "Enter number to add ";
			cin >> i;
			number += i;
			cout << number;
		}

		cout << "enter R to return, C to continue addition ";
		cin >> return_main;

	}while(return_main != 'r');    //while return_main is not r loop through the function. 

	return (number);            //when return_main is r return the sum.
}


float subtract_num(float number)
{
	float i, j;
	char return_main;

	do
	{
		if(number == 0)
		{
			cout << "Enter number to subtract from ";
			cin >> i;
			cout << "Enter amount to subtract ";
			cin >> j;
			number = i - j;
			cout << number;
		}
		
		else
		{
			cout << "Enter amount to subtract ";
			cin >> i;
			number -= i;
			cout << number;
		}

		cout << "Enter R to return, C to continue subtraction ";
		cin >> return_main;
	}while(return_main != 'r');

	return (number);
}


float multiply_num(float number)
{
	float i, j;
	char return_main;

	if(number == 0)
	{
		cout << "Enter number to multiply ";
		cin >> i;
		cout << " times ";
		cin >> j;
		number = i * j;
		cout << number;
	}

	else
	{
		cout << number << " times ";
		cin >> i;
		number *= i;
		cout << number << endl;
	}
		
	cout << "Enter R to return to main menu\n";
	cin >> return_main;
}while(return_main != 'r');

	return (number);
}


float divide_num(float number)
{
	float i, j;
	char return_main;
	
	do
	{
		if(number == 0)
		{
			cout << "Enter number to be divided ";
			cin >> i;
			cout << "divided by ";
			cin >> j;
			number = i / j;
			cout << number;
		}
	
		else
		{
			cout << number << " divided by ";
			cin >> i;
			number /= i;
			cout << number << endl;
		}

		cout << "Enter r to return, c to continue ";
		cin >> return_main;

	}while(return_main != 'r');
	
	return (number);
}
BCalc.cpp
C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(184) : error C2143: syntax error : missing ';' before 'while'
C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(186) : error C2143: syntax error : missing ';' before 'return'
C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(187) : error C2143: syntax error : missing ';' before '}'
C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(187) : error C2143: syntax error : missing ';' before '}'
C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(187) : error C2143: syntax error : missing ';' before '}'
C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(191) : error C2143: syntax error : missing ';' before '{'
C:\Program Files\Microsoft Visual Studio\VC98\BCalc.cpp(191) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.

Match each { to its }. Are you trying to do a do...while loop without the do?

float multiply_num(float number)
{
	float i, j;
	char return_main;

	if(number == 0)
	{
		cout << "Enter number to multiply ";
		cin >> i;
		cout << " times ";
		cin >> j;
		number = i * j;
		cout << number;
	}

	else
	{
		cout << number << " times ";
		cin >> i;
		number *= i;
		cout << number << endl;
	}
		
	cout << "Enter R to return to main menu\n";
	cin >> return_main;
}while(return_main != 'r');

	return (number);
}

Match each { to its }. Are you trying to do a do...while loop without the do?

}while(return_main != 'r');

	return (number);
}

:o Wow, that little bit made me feel kind of dumb, it helped out alot though, thanks. :D

This article has been dead for over six months. Start a new discussion instead.