| | |
Wierd error messages with calculator program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2004
Posts: 42
Reputation:
Solved Threads: 0
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:
C++ Syntax (Toggle Plain Text)
/* * 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); }
C++ Syntax (Toggle Plain Text)
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);
} "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Sep 2004
Posts: 42
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Dave Sinkula
Match each { to its }. Are you trying to do a do...while loop without the do?
}while(return_main != 'r'); return (number); }
![]() |
Similar Threads
- Error messages on startup XP (Windows NT / 2000 / XP)
- Error messages aplenty!!!!! (C++)
- Microsoft Error Messages (IT Professionals' Lounge)
- program w/ switch AND nested if. six error messages (C++)
- Error loading C:\WINNT\Downloaded Program Files\bridge.dll (Viruses, Spyware and other Nasties)
- internet explorer error messages (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: Playing cards
- Next Thread: subtraction operator overload
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






