| | |
Wierd error messages with calculator program
![]() |
•
•
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);
} “The essential notion of a socialist society is force.”
— Milton Friedman
— Milton Friedman
•
•
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
- internet explorer error messages (Web Browsers)
- 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)
Other Threads in the C++ Forum
- Previous Thread: Playing cards
- Next Thread: subtraction operator overload
Views: 3456 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm array arrays assignment basic beginner binary c++ c++borland c/c++ calculator char class classes client code compile compiler constructor conversion convert count delete dll dynamic encryption error file files form forms fstream function functions game givemetehcodez graph gui helpwithhomework homework iamthwee input int integer lazy lib link linker list loop loops map math matrix member memory multidimensional network newbie news number object objects opengl output parameter pointer pointers problem program programming project qt random read recursion recursive reference search sort spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual win32 window windows winsock






