Tuxiscool Posted September 15, 2003 Report Share Posted September 15, 2003 Ok, i have *tried* to make a calculator for one of my first programs and i get a few errors on compile, can anyone help me fix these? Here's my code #include <iostream.h> struct answer{ int multiply; int add; int divide; int subtract; } { main() int Choice; int mulnumone; int mulnumtwo; int addnumone; int addnumtwo; int divnumone; int divnumtwo; int subnumone; int subnumtwo; while (1) { // Menu Heading cout << "ttt//////////////////////////n"; cout << "ttt/////// CALCULATOR ///////n"; cout << "ttt//////////////////////////n"; //Menu Choices cout << "nttt1) Multiply Numbers n"; cout << "ttt2) Add Numbers n"; cout << "ttt3) Divide Numbers n"; cout << "ttt4) Subtract Numbers n"; cout << "ttt5) Exit the calculator n"; cout << "ntttPlease Enter your choice: "; // Find users choice cin >> Choice; // Accordng to choice Calculate users input // Multiplication if (Choice == 1) cout << "ntttYou chose to Multiply Numbers.nttt"; cout << "ntttEnter the numbers that you want to Multiply, after you type each nimber, press enternttt"; cout << "ntttEnter First Number: nttt"; cin >> mulnumone; cout << "ntttEnter Second Number: nttt"; cin >> mulnumtwo; // Calculate the Multiplication Equation answer.multiply = mulnumone * mulnumtwo; cout << mulnumone << " times " << mulnumtwo << " equals " << answer.multiply << "n"; // Addition else if (Choice == 2) cout << "ntttYou chose to Add Numbers.nttt"; cout << "ntttEnter the numbers that you want to Add, after you type each nimber, press enternttt"; cout << "ntttEnter First Number: nttt"; cin >> addnumone; cout << "ntttEnter Second Number: nttt"; cin >> addnumtwo; // Calculate the Addition Equation answer.add = mulnumone + mulnumtwo; cout << addnumone << " plus " << addnumtwo << " equals " << answer.add << "n"; // Division else if (Choice == 3) cout << "ntttYou chose to Divide Numbers.nttt"; cout << "ntttEnter the numbers that you want to Divide, after you type each nimber, press enternttt"; cout << "ntttEnter First Number: nttt"; cin >> divnumone; cout << "ntttEnter Second Number: nttt"; cin >> divnumtwo; // Calculate the Division Equation answer.divide = divnumone / divnumtwo; cout << divnumone << " divided by " << divnumtwo << " equals " << answer.add << "n"; // Subtraction else if (Choice == 4) cout << "ntttYou chose to Subtract Numbers.nttt"; cout << "ntttEnter the numbers that you want to Subtract, after you type each nimber, press enternttt"; cout << "ntttEnter First Number: nttt"; cin >> subnumone; cout << "ntttEnter Second Number: nttt"; cin >> subnumtwo; //Calculate the Subtraction Equation answer.subtract = subnumone / subnumtwo; cout << subnumone << " minus " << subnumtwo << " equals " << answer.subtract << "n"; // If users choice was to exit the calculator, exit else if (Choice == 5) { cout << "ntttYou chose to Exit the Calculator. Bye.nttt"; break; } // If users Choice is not valid, must enter again else { cout << "ntttYour choice is NOT valid!n"; cout << "tttPlease enter a CORRECT choice!nnttt"; } } return 0; } and here is g++'s output (through emacs) g++ -O2 calculator.cpp -o calculator -Wno-deprecated calculator.cpp:13: parse error before `{' token calculator.cpp:26: parse error before `while' calculator.cpp:31: syntax error before `<<' token calculator.cpp:32: syntax error before `<<' token calculator.cpp:36: syntax error before `<<' token calculator.cpp:37: syntax error before `<<' token calculator.cpp:38: syntax error before `<<' token calculator.cpp:39: syntax error before `<<' token calculator.cpp:40: syntax error before `<<' token calculator.cpp:41: syntax error before `<<' token calculator.cpp:45: syntax error before `>>' token calculator.cpp:53: syntax error before `<<' token calculator.cpp:54: syntax error before `<<' token calculator.cpp:55: syntax error before `>>' token calculator.cpp:56: syntax error before `<<' token calculator.cpp:57: syntax error before `>>' token calculator.cpp:62: syntax error before `<<' token calculator.cpp:68: syntax error before `<<' token calculator.cpp:69: syntax error before `<<' token calculator.cpp:70: syntax error before `>>' token calculator.cpp:71: syntax error before `<<' token calculator.cpp:72: syntax error before `>>' token calculator.cpp:77: syntax error before `<<' token calculator.cpp:83: syntax error before `<<' token calculator.cpp:84: syntax error before `<<' token calculator.cpp:85: syntax error before `>>' token calculator.cpp:86: syntax error before `<<' token calculator.cpp:87: syntax error before `>>' token calculator.cpp:92: syntax error before `<<' token calculator.cpp:98: syntax error before `<<' token calculator.cpp:99: syntax error before `<<' token calculator.cpp:100: syntax error before `>>' token calculator.cpp:101: syntax error before `<<' token calculator.cpp:102: syntax error before `>>' token calculator.cpp:107: syntax error before `<<' token calculator.cpp:122: syntax error before `<<' token Compilation exited abnormally with code 1 at Mon Sep 15 23:09:19 Help would be much appreciated Thanks in advance. Link to comment Share on other sites More sharing options...
Glitz Posted September 15, 2003 Report Share Posted September 15, 2003 For starters, main() should come before the {. Fix that and compile again. That may get rid of most of your errors. Glitz. Link to comment Share on other sites More sharing options...
johnnyv Posted September 15, 2003 Report Share Posted September 15, 2003 Okay firstly your structure definition needs to end with a semicolon }; you must create an instance of your structure before you can use it "answer answer;"(created an instance of the answer structure named answer, it could be done when you make the structure). you had { before main() not after as was mentioned you needed to declare what type main() is eg "int main()" so g++ will know what main will return you could use void main() but thats not recommended. enclose your else if statment code in brackets if there is a fair bit of it. that was about it, here is a version that compiles #include <iostream.h> struct answer{ int multiply; int add; int divide; int subtract; }; int main() { int Choice; int mulnumone; int mulnumtwo; int addnumone; int addnumtwo; int divnumone; int divnumtwo; int subnumone; int subnumtwo; answer answer; while (1) { // Menu Heading cout << "ttt//////////////////////////n"; cout << "ttt/////// CALCULATOR ///////n"; cout << "ttt//////////////////////////n"; //Menu Choices cout << "nttt1) Multiply Numbers n"; cout << "ttt2) Add Numbers n"; cout << "ttt3) Divide Numbers n"; cout << "ttt4) Subtract Numbers n"; cout << "ttt5) Exit the calculator n"; cout << "ntttPlease Enter your choice: "; // Find users choice cin >> Choice; // Accordng to choice Calculate users input // Multiplication if (Choice == 1) { cout << "ntttYou chose to Multiply Numbers.nttt"; cout << "ntttEnter the numbers that you want to Multiply, after you type each nimber, press enternttt"; cout << "ntttEnter First Number: nttt"; cin >> mulnumone; cout << "ntttEnter Second Number: nttt"; cin >> mulnumtwo; // Calculate the Multiplication Equation answer.multiply = mulnumone * mulnumtwo; cout << mulnumone << " times " << mulnumtwo << " equals " << answer.multiply << "n"; } // Addition else if (Choice == 2) { cout << "ntttYou chose to Add Numbers.nttt"; cout << "ntttEnter the numbers that you want to Add, after you type each nimber, press enternttt"; cout << "ntttEnter First Number: nttt"; cin >> addnumone; cout << "ntttEnter Second Number: nttt"; cin >> addnumtwo; // Calculate the Addition Equation answer.add = mulnumone + mulnumtwo; cout << addnumone << " plus " << addnumtwo << " equals " << answer.add << "n"; } // Division else if (Choice == 3) { cout << "ntttYou chose to Divide Numbers.nttt"; cout << "ntttEnter the numbers that you want to Divide, after you type each nimber, press enternttt"; cout << "ntttEnter First Number: nttt"; cin >> divnumone; cout << "ntttEnter Second Number: nttt"; cin >> divnumtwo; // Calculate the Division Equation answer.divide = divnumone / divnumtwo; cout << divnumone << " divided by " << divnumtwo << " equals " << answer.add << "n"; } // Subtraction else if (Choice == 4) { cout << "ntttYou chose to Subtract Numbers.nttt"; cout << "ntttEnter the numbers that you want to Subtract, after you type each nimber, press enternttt"; cout << "ntttEnter First Number: nttt"; cin >> subnumone; cout << "ntttEnter Second Number: nttt"; cin >> subnumtwo; //Calculate the Subtraction Equation answer.subtract = subnumone / subnumtwo; cout << subnumone << " minus " << subnumtwo << " equals " << answer.subtract << "n"; } // If users choice was to exit the calculator, exit else if (Choice == 5) { cout << "ntttYou chose to Exit the Calculator. Bye.nttt"; break; } // If users Choice is not valid, must enter again else { cout << "ntttYour choice is NOT valid!n"; cout << "tttPlease enter a CORRECT choice!nnttt"; } } return 0; } Link to comment Share on other sites More sharing options...
Tuxiscool Posted September 16, 2003 Author Report Share Posted September 16, 2003 Thank you all for your help, it's much appreciated Anyway, about this line answer answer; Can someone please explain in more depth what this does? because i don't really understand it, thanks. And also, im going to try and add a few new features, like Squaring etc. Would it be done like this? (apart from adding it as one of my choices and declaring variables) else if (Choice == umm whatever) { cout << "ntttYou chose to Square a Number.nttt"; cout << "ntttEnter the numbers that you want to Square, after you type the number, press enternttt"; cout << "ntttEnter Number: ttt"; cin >> squarenum; answer.square = squarenum ^2; cout << "nttt" << squarenum << " squared " << " equals " << answer.square << "n"; } That's just a guess but is that how you do squaring in c++? if not, could someone please tell me how? thanks. Link to comment Share on other sites More sharing options...
Cannonfodder Posted September 16, 2003 Report Share Posted September 16, 2003 You always have ... x2 = x * x Link to comment Share on other sites More sharing options...
johnnyv Posted September 16, 2003 Report Share Posted September 16, 2003 In regards to "answer answer;" you are declaring an instance of the structure answer and calling it answer. it's like int X; you could have called it answer bob; then you would call bob.multiply , bob.add etc.... The structure definition below is just that, it's a definition, the stuct name(in this case answer) becomes a sort of new variable type. struct answer{ int multiply; int add; int divide; int subtract; }; To make use of this new variable type you need to create in instance of it, hence "answer answer;" . You can actually define instances of a stucture when you define the structure as follows struct answer{ int multiply; int add; int divide; int subtract; } answer, bob; Here two instances of the struct answer have been defined, answer and bob which can be used later in the program. I hope that explains it a bit better. You could look at this page for some more examples. http://www.cplusplus.com/doc/tutorial/tut3-5.html Oh and another thing, there is an error in your code in the addition part "answer.add = mulnumone + mulnumtwo;" should be addnumone + addnumtwo;. Once you understand this then you should improve you program by adding protections like not allowing a divide by zero error to occur, and preventing users from entering other than ints, eg currently adding a number and letter together gives a rather bad result. Link to comment Share on other sites More sharing options...
johnnyv Posted September 23, 2003 Report Share Posted September 23, 2003 Oh for square root in c++ you need to include the math header file #include <math.h> y = sqrt(x); ref: http://www.cplusplus.com/ref/cmath/sqrt.html Link to comment Share on other sites More sharing options...
ramfree17 Posted September 23, 2003 Report Share Posted September 23, 2003 and dont forget to explicitly link the math libraries during compile time. i think its -lm but it might be -lmath. i remember this issue from a newbie post in the local mailing list. ciao! Link to comment Share on other sites More sharing options...
Tuxiscool Posted September 26, 2003 Author Report Share Posted September 26, 2003 Yeah, thanks for that, i had done that already though over a week ago, now it does a few more things aswell. Now im trying to figure out how i can find the size of an angle when i have the value CosA (where A is the angle) and also tanA and sinA. Here's my code so far #include <iostream.h> #include <math.h> struct answer{ double multiply, add, divide, subtract, square, sqrt, tanA, cosine, sine, circumference, semiCircumference; }; int main() { int Choice, choiceCircumference; double mulnumone, mulnumtwo, addnumone, addnumtwo, divnumone, divnumtwo, subnumone, subnumtwo, squarenum, sqrtnum, tannumopposite, tannumadjacent, cosnumadjacent, cosnumhypotenuse, sinnumopposite, sinnumhypotenuse, circleDiameter, semiCircleDiameter; answer answer; while (1) { // Menu Heading cout << "ttt##########################n"; cout << "ttt####### CALCULATOR #######n"; cout << "ttt##########################n"; //Menu Choices cout << "nttt1) Multiply Numbers n"; cout << "ttt2) Add Numbers n"; cout << "ttt3) Divide Numbers n"; cout << "ttt4) Subtract Numbers n"; cout << "ttt5) Square Numbers n"; cout << "ttt6) Find Square Root of number n"; cout << "ttt7) Find cosA n"; cout << "ttt8) Find sinA n"; cout << "ttt9) Find tanA n"; cout << "ttt10) Find Circumference n"; cout << "ttt11) Exit the calculator n"; cout << "ntttPlease Enter your choice: "; // Find users choice cin >> Choice; // Accordng to choice Calculate users input // Multiplication if (Choice == 1) { cout << "ntttYou chose to Multiply Numbers.nttt"; cout << "ntttEnter the numbers that you want to Multiply, after you type each number, press enternttt"; cout << "ntttEnter First Number: ttt"; cin >> mulnumone; cout << "ntttEnter Second Number: ttt"; cin >> mulnumtwo; // Calculate the Multiplication Equation answer.multiply = mulnumone * mulnumtwo; cout << "nttt" << mulnumone << " times " << mulnumtwo << " equals " << answer.multiply << "nn"; } // Addition else if (Choice == 2) { cout << "ntttYou chose to Add Numbers.nttt"; cout << "ntttEnter the numbers that you want to Add, after you type each number, press enternttt"; cout << "ntttEnter First Number: ttt"; cin >> addnumone; cout << "ntttEnter Second Number: ttt"; cin >> addnumtwo; // Calculate the Addition Equation answer.add = addnumone + addnumtwo; cout << "nttt" << addnumone << " plus " << addnumtwo << " equals " << answer.add << "nn"; } // Division else if (Choice == 3) { cout << "ntttYou chose to Divide Numbers.nttt"; cout << "ntttEnter the numbers that you want to Divide, after you type each number, press enternttt"; cout << "ntttEnter First Number: ttt"; cin >> divnumone; cout << "ntttEnter Second Number: ttt"; cin >> divnumtwo; // If divnumtwo equals 0, don't try to divide if (divnumtwo == 0) { cout << "ntttSorry, you can not divide by Zero, Please try something else.nn"; } // Calculate the Division Equation else { answer.divide = divnumone / divnumtwo; cout << "nttt" << divnumone << " divided by " << divnumtwo << " equals " << answer.divide << "nn"; } } // Subtraction else if (Choice == 4) { cout << "ntttYou chose to Subtract Numbers.nttt"; cout << "ntttEnter the numbers that you want to Subtract, after you type each number, press enternttt"; cout << "ntttEnter First Number: ttt"; cin >> subnumone; cout << "ntttEnter Second Number: ttt"; cin >> subnumtwo; //Calculate the Subtraction Equation answer.subtract = subnumone - subnumtwo; cout << "nttt" << subnumone << " minus " << subnumtwo << " equals " << answer.subtract << "nn"; } // Squaring else if (Choice == 5) { cout << "ntttYou chose to Square a Number.nttt"; cout << "ntttEnter the number you want to Square: ttt"; cin >> squarenum; // Calculate the Squaring Equation answer.square = squarenum * squarenum; cout << "nttt" << squarenum << " squared equals " << answer.square << "nn"; } // Square Rooting else if (Choice == 6) { cout << "ntttYou chose to find the Square Root of a number.nttt"; cout << "ntttEnter the number you want the Square Root of: ttt"; cin >> sqrtnum; //Calculate Squaring Equation answer.sqrt = sqrt (sqrtnum); cout << "nttt" << "The square root of " << sqrtnum << " is " << answer.sqrt << "nn"; } // Finding cosA else if (Choice == 7) { cout << "ntttYou chose to find cosA.nttt"; cout << "ntttEnter the size of the adjacent side: ttt"; cin >> cosnumadjacent; cout << "ntttEnter the size of the hypotenuse: ttt"; cin >> cosnumhypotenuse; // Find cosA answer.cosine = cosnumadjacent / cosnumhypotenuse; cout << "nttt" << "cosA equals " << answer.cosine << "nn"; } // Finding sinA else if (Choice == 8) { cout << "ntttYou chose to find sinA.nttt"; cout << "ntttEnter the size of the opposite side: ttt"; cin >> sinnumopposite; cout << "ntttEnter the size of the hypotenuse: ttt"; cin >> sinnumhypotenuse; // Find sinA answer.sine = sinnumopposite / sinnumhypotenuse; cout << "nttt" << "sinA equals " << answer.sine << "nn"; } // Finding tanA else if (Choice == 9) { cout << "ntttYou chose to find tanA.nttt"; cout << "ntttEnter the size of the opposite side: ttt"; cin >> tannumopposite; cout << "ntttEnter the size of the adjacent side: ttt"; cin >> tannumadjacent; // Find tanA answer.tanA = tannumopposite / tannumadjacent; cout << "nttt" << "tanA equals " << answer.tanA << "nn"; } // Finding Circumference else if (Choice == 10) { cout << "ntttWhat do you want to find the circumference of? n"; cout << "nttt1) Circle. n"; cout << "ttt2) Semicircle. n"; cout << "tttWhat is your choice? "; cin >> choiceCircumference; if (choiceCircumference == 1) { cout << "ntttYou chose to find the circumfefence of a circle.nttt"; cout << "ntttEnter the diameter of the circle: ttt"; cin >> circleDiameter; // Find Circumference of circle answer.circumference = circleDiameter * 3.14159; cout << "nttt" << "The circumference of the circle with the diameter " << circleDiameter << " is " << answer.circumference << "n"; } else if (choiceCircumference == 2) { cout << "ntttYou chose to find the circumference of a Semicircle.nttt"; cout << "ntttEnter the diameter of the Semicircle: ttt"; cin >> semiCircleDiameter; // Find Circumferene of semicircle answer.semiCircumference = (semiCircleDiameter * 3.14159) * 0.5; cout << "nttt" << "The circumference of the semicircle with the diameter " << semiCircleDiameter << " is " << answer.semiCircumference << "n"; } } // If users choice was to exit the calculator, exit else if (Choice == 11) { cout << "ntttYou chose to Exit the Calculator. Bye.nttt"; break; } // If users Choice is not valid, must enter again else { cout << "ntttYour choice is NOT valid!n"; cout << "tttPlease enter a CORRECT choice!nn"; } } return 0; } [/code] Link to comment Share on other sites More sharing options...
Glitz Posted September 26, 2003 Report Share Posted September 26, 2003 try these functions: ACOS() ASIN() ATAN() The return values are in radians. Glitz. Link to comment Share on other sites More sharing options...
Dr Thrall Posted September 26, 2003 Report Share Posted September 26, 2003 btw why don't u use-switch case in place of if-else so far i know, switch-case use for known selection, & if for comparison, an unknown condition ( like if user hit the key or what ... ) . using switch-case, ur code become more attractive & clean ... & use getchar in place on cin, for getting choice from user. as if user keep pressing enter key, cin still waits for input & ur display screen may disappear ( untill u use method of istream not to do this ) & u can't c the choice menu untill u press wrong key, or return from ur calculation Link to comment Share on other sites More sharing options...
Tuxiscool Posted September 27, 2003 Author Report Share Posted September 27, 2003 Thank you very much for all of your help and Dr Thrall, i will try out your advice when i can be bothered ;), but for a start, trying ACOS() , ASIN(), and ATAN() produces the following errors. calculator.cpp: In function `int main()': calculator.cpp:177: `ACOS' undeclared (first use this function) calculator.cpp:177: (Each undeclared identifier is reported only once for each function it appears in.) calculator.cpp:197: `ASIN' undeclared (first use this function) calculator.cpp:217: `ATAN' undeclared (first use this function) do i need to add an extra header file or is it something different i do? Thanks in Advance Link to comment Share on other sites More sharing options...
Cannonfodder Posted September 27, 2003 Report Share Posted September 27, 2003 When something is generating an undefined error then the compilier can't find a reference to it. If it can't find a reference to it then it can't find the code for it. You need to make sure the 3 functions or the header file for the functions are declared. One good way to go about figuring this out is to do a google search on it.. E.g. acos c++ and look for how to reference them. Of course you can always read your manual too :) Link to comment Share on other sites More sharing options...
Glitz Posted September 29, 2003 Report Share Posted September 29, 2003 Oops! I didn't mean for them to be in uppercase. Try asin() acos() atan() Sorry for the confusion. Glitz. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now