| | |
Fraction calculator
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
hey, this program is supposed to calculate fractions, and I feel that I have most of the code down, but I can't get past the
fractions.cpp:50: error: expected primary-expression before 'char'
error, its probably something misicule, but I can't get it for the life of me. The error is in the main function. heres my code. THANK YOU SO MUCH IN ADVANCE!!!
fractions.cpp:50: error: expected primary-expression before 'char'
error, its probably something misicule, but I can't get it for the life of me. The error is in the main function. heres my code. THANK YOU SO MUCH IN ADVANCE!!!
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; void menu (char symbol); void addFractions(int& a,int& b,int& c,int& d,int& numerator,int& denominator); void subtractFractions(int& a,int& b,int& c,int& d,int& numerator,int& denominator); void multiplyFractions (int& a,int& b,int& c,int& d,int& numerator,int& denominator); void divideFractions (int& a,int& b,int& c,int& d,int& numerator,int& denominator); int main() { int a, b, c, d; int numerator; int denominator; char symbol; cout << "first, we will need your numerators/denominators." << endl; cout << "What is your first fractions numerator?" << endl; cin>> a; cout << "What is your first fractions denominator?" << endl; cin >> b; cout << "What is your second fractions numerator?" << endl; cin >> c; cout << "What is your second fractions denominator?" << endl; cin >> d; while (d == 0 || b == 0) { if (d == 0) { cout << "Your second fraction has a 0 in the denominator, what would" << " you like to change it to?" << endl; cin >> d; } if (b == 0) { cout << "Your first fraction has a 0 in the denominator, what would" << " you like to change it to?" << endl; cin >> b; } } menu (char symbol); return 0; } void menu(char symbol) { int a, b, c, d; int numerator, denominator; cout << "Have fractions that need solving??" << endl; cout << "Well, luckily for you, you chanced upon a fractions calculator" << endl; cout << "First, we will need the operation you wish to perform." << endl; cout << "choose one of the following: + (addition), - " << "(subtraction), * (multiplication), and finally / (division)." << endl; cin >> symbol; cout << endl; switch (symbol) { case '*': multiplyFractions(a,b,c,d,numerator, denominator); cout << "Answer is " << numerator << "/" << denominator << endl; break; case '/': divideFractions(a,b,c,d,numerator, denominator); cout << "Answer is " << numerator << "/" << denominator << endl; break; case '+': addFractions(a,b,c,d,numerator, denominator); cout << "Answer is " << numerator << "/" << denominator << endl; break; case '-': subtractFractions(a,b,c,d,numerator, denominator); cout << "Answer is " << numerator << "/" << denominator << endl; break; default: cout << "Invalid input, pay better attention next time." << endl; } } void addFractions (int& a,int& b,int& c,int& d,int& numerator,int& denominator) { int temp; temp = b*d; numerator = (temp/b*a)+(temp/d*c); denominator = temp; } void subtractFractions (int& a,int& b,int& c,int& d,int& numerator,int& denominator) { int temp; temp = b*d; numerator = (temp/b*a)-(temp/d*c); denominator = temp; } void multiplyFractions(int& a,int& b,int& c,int& d,int& numerator,int& denominator) { numerator = (a*c); denominator = (b*d); } void divideFractions(int& a,int& b,int& c,int& d,int& numerator,int& denominator) { numerator = (a*d); denominator = (b*c); }
•
•
Join Date: Oct 2008
Posts: 32
Reputation:
Solved Threads: 4
You were also missing a bracket here and there
You also didn't need addresses when passing the values of a, b, c and d
You might not want the code but I did it anyway cos it's fun.
Regards..
P.S. Try to tab your statements so it's easier to catch when you're missing brackets.
You also didn't need addresses when passing the values of a, b, c and d
You might not want the code but I did it anyway cos it's fun.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; void menu(int a, int b, int c, int d, char symbol); void addFractions(int a,int b, int c, int d, int &numerator, int &denominator); void subtractFractions(int a, int b, int c, int d, int &numerator, int &denominator); void multiplyFractions (int a, int b, int c, int d, int &numerator, int &denominator); void divideFractions (int a, int b, int c, int d, int &numerator, int &denominator); //------------------------------------------------------------------------- int main() { int a, b, c, d; int numerator; int denominator; char symbol; cout << "first, we will need your numerators/denominators." << endl; cout << "What is your first fractions numerator?" << endl; cin>> a; cout << "What is your first fractions denominator?" << endl; cin >> b; cout << "What is your second fractions numerator?" << endl; cin >> c; cout << "What is your second fractions denominator?" << endl; cin >> d; while (d == 0 || b == 0) { if (d == 0) { cout << "Your second fraction has a 0 in the denominator, what would" << " you like to change it to?" << endl; cin >> d; } if (b == 0) { cout << "Your first fraction has a 0 in the denominator, what would" << " you like to change it to?" << endl; cin >> b; } } menu(a, b, c, d, symbol); return 0; } //-------------------------------------------------------------------- void menu(int a, int b, int c, int d, char symbol) { int numerator, denominator; cout << "Have fractions that need solving??" << endl; cout << "Well, luckily for you, you chanced upon a fractions calculator" << endl; cout << "First, we will need the operation you wish to perform." << endl; cout << "choose one of the following: + (addition), - " << "(subtraction), * (multiplication), and finally / (division)." << endl; cin >> symbol; cout << endl; switch (symbol) { case '*': multiplyFractions(a,b,c,d,numerator, denominator); cout << "Answer is " << numerator << "/" << denominator << endl; system("pause>nul"); break; case '/': divideFractions(a,b,c,d,numerator, denominator); cout << "Answer is " << numerator << "/" << denominator << endl; system("pause>nul"); break; case '+': addFractions(a, b, c, d, numerator, denominator); cout << "Answer is " << numerator << "/" << denominator << endl; system("pause>nul"); break; case '-': subtractFractions(a,b,c,d,numerator, denominator); cout << "Answer is " << numerator << "/" << denominator << endl; system("pause>nul"); break; default: cout << "Invalid input, pay better attention next time." << endl; } } void addFractions (int a, int b, int c, int d, int &numerator, int &denominator) { numerator = (a*d) + (b*c); denominator = (b*d); } void subtractFractions (int a, int b, int c, int d, int &numerator, int &denominator) { numerator = (a*d)-(b*c); denominator = (b*d); } void multiplyFractions(int a, int b, int c, int d, int &numerator, int&denominator) { numerator = (a*c); denominator = (b*d); } void divideFractions(int a, int b, int c, int d, int &numerator, int &denominator) { numerator = (a*d); denominator = (b*c); }
P.S. Try to tab your statements so it's easier to catch when you're missing brackets.
![]() |
Similar Threads
- Starting Python (Python)
- class within a class (Java)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Console commands (C++)
- Help with GUI (Java)
- Fraction Calculator (Java)
- Needing some help (C++)
- Complex Number Calculator in C++ (C++)
Other Threads in the C++ Forum
- Previous Thread: Borland form help [2]
- Next Thread: Help with code please
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile 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 iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





