| | |
C++ - Need help solving a problem with my Calculator Script.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 19
Reputation:
Solved Threads: 0
Hello, this is Metalcrunch. I've been learning C++ over the past days and I thought I'd make a calculator. So I looked over the existing code on the internet on how to make a calculator, understood it, then made my own from the very scratch.
However, I'm having a few problems with it - Hope you guys can help me out!
main_calculator.cpp
header_calculator.h
The build log.
I can't honestly figure it out ... I'd really, really like some help!
metalcrunch
However, I'm having a few problems with it - Hope you guys can help me out!

main_calculator.cpp
c++ Syntax (Toggle Plain Text)
//Calculator Script by Sass. //main_calculator.cpp //Default include's. #include <iostream> #include <windows.h> using namespace std; //Custom header file. #include "header_calculator.h" //Main() int main() { //One-time welcome greeting: SetConsoleTitle("Calculator v1.1"); cout << "Kalkulaator v1.1 by MetalCrunch." << endl; cout << "---------------------------------------" << endl << endl; //Main program: for (;;) { do { cout << "1 - Addition." << endl; cout << "2 - Subtraction." << endl; cout << "3 - Multiplication." << endl; cout << "4 - Division." << endl; cout << "5 - Abimees." << endl; cin >> choice; } switch (choice) { //Addition. case 1: cout << "Enter the first number: "; cin >> num1; cout << "\nEnter the second number: "; cin >> num2; result = num1 + num2; cout << "Answer: "; cout << result; break; //Subtraction. case 2: cout << "Enter the first number: "; cin >> num1; cout << "\nEnter the second number: "; cin >> num2; result = num1 - num2; cout << "\nAnswer: "; cout << result; break; //Multiplication. case 3: cout << "Enter the first number: "; cin >> num1; cout << "\nEnter the second number: "; cin >> num2; result = num1 * num2; cout << "\nAnswer: "; cout << result; break; //Division. case 4: cout << "Enter the first number: "; cin >> num1; cout << "\n Enter the second number: "; cin >> num2; result = num1 / num2; cout << "\nAnswer: "; cout << result; break; //"Information". case 5: cout << "Calculator v1.1 by Metalcrunch" << endl; cout << "Enter the number 1-5, then answer the questions..." << endl; cout << "The program will solve it all." << endl; cout << "But please don't enter a value over 9 numbers, the program will crash!" << endl; break; default: cout << "test"; break; } } return 0; }
header_calculator.h
c++ Syntax (Toggle Plain Text)
//Calculator Script //header_calculator.h #ifndef HEADER_CALCULATOR_H #define HEADER_CALCULATOR_H //Main int's, for calculation. int num1, num2; int result; //The "choice" int. int choice; #endif
The build log.
C++ Syntax (Toggle Plain Text)
Compiling... main_calculator.cpp c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(31) : error C2059: syntax error : 'switch' c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(31) : error C2143: syntax error : missing ';' before '{' c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(33) : error C2046: illegal case c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(44) : error C2046: illegal case c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(55) : error C2046: illegal case c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(66) : error C2046: illegal case c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(77) : error C2046: illegal case c:\documents and settings\*********\my documents\visual studio 2008\projects\project1\calculator\calculator\main_calculator.cpp(84) : error C2047: illegal default
I can't honestly figure it out ... I'd really, really like some help!
metalcrunch
•
•
Join Date: Jul 2009
Posts: 3
Reputation:
Solved Threads: 1
C++ Syntax (Toggle Plain Text)
#include <iostream> double a = 0; double b = 0; double c = 0; double d = 0; double add = 0; double sub = 0; double mul = 0; double f = 0; int main () { std::cout << "Enter any one of the following " << std::endl; std::cout << "1 for ADDITION " << std::endl; std::cout << "2 for SUBTRACTOIN " << std::endl; std::cout << "3 for MULTIPLICATOIN " << std::endl; std::cout << "4 for DIVISION " << std::endl; std::cin >> a; std::cout << ' ' << std::endl; std::cout << "Enter first number: " <<std::endl; std::cin >> b; std::cout<< ' ' << std::endl; std::cout << "Enter second number: " << std::endl; std::cin >> c; std::cout<< ' ' << std::endl; if ( a == 1) { add = b + c; std::cout << add << std::endl; }else if ( a == 2){ sub = b - c; std::cout << sub << std::endl; }else if (a == 3) { mul = b * c; std::cout << mul << std::endl; }else if (a == 4){ f = b / c; std::cout << f << std::endl; } }
Last edited by Ancient Dragon; Jul 21st, 2009 at 10:47 am. Reason: correct code tags
ALI INAM, you should urgently stop posting such rubbish solutions, BTW: why making every variable global?
Ever heard of local variables?
Do you really think the OP will learn from such solutions?
By just giving code to the OP, you won't help him, remember that.
It's against Daniweb's homework policy.
Links for (I hope) a permanent reminder:
http://www.daniweb.com/forums/announcement118-2.html
http://www.daniweb.com/forums/announcement118-3.html
Ever heard of local variables?
Do you really think the OP will learn from such solutions?
By just giving code to the OP, you won't help him, remember that.
It's against Daniweb's homework policy.
Links for (I hope) a permanent reminder:
http://www.daniweb.com/forums/announcement118-2.html
http://www.daniweb.com/forums/announcement118-3.html
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Jul 2009
Posts: 19
Reputation:
Solved Threads: 0
Hi, me again. Well, first of all, big thanks to AceOfSpade19 for clearing that up to me.
Second of all, thank you ALI INAM for trying to help me, however next time try putting the [.code=c++][./code] tags.
And third of all, I'm still stuck. Here's how the Main Program part looks like currently.
Now, that was the default Main Code. If I add the "while" command to it like this,
Then it will get me this error.
If I follow the error, and put it like this,
It will compile "succesfully" but the script is broken, if I enter any number/letter and press Enter it will exit immediately... Right..
I also tried to make it like this.
However, that didn't help much, because I get this error.
Well, I'm stuck... Again.. Would be nice if anyone would told me what to do... Well, would be even better, if I would be told what to put in the "while" statement...
Second of all, thank you ALI INAM for trying to help me, however next time try putting the [.code=c++][./code] tags.
And third of all, I'm still stuck. Here's how the Main Program part looks like currently.
c++ Syntax (Toggle Plain Text)
//Main program: for (;;) { do { cout << "1 - Addition." << endl; cout << "2 - Subtraction." << endl; cout << "3 - Multiplication." << endl; cout << "4 - Division." << endl; cout << "5 - Abimees." << endl; cin >> choice; } switch (choice) { //Addition.
Now, that was the default Main Code. If I add the "while" command to it like this,
c++ Syntax (Toggle Plain Text)
//Main program: for (;;) { do { cout << "1 - Addition." << endl; cout << "2 - Subtraction." << endl; cout << "3 - Multiplication." << endl; cout << "4 - Division." << endl; cout << "5 - Abimees." << endl; cin >> choice; } while (choice < 1 || choice > 5) { break; } switch (choice) { //Addition.
C++ Syntax (Toggle Plain Text)
error C2143: syntax error : missing ';' before '{'
If I follow the error, and put it like this,
c++ Syntax (Toggle Plain Text)
//Main program: for (;;) { do { cout << "1 - Addition." << endl; cout << "2 - Subtraction." << endl; cout << "3 - Multiplication." << endl; cout << "4 - Division." << endl; cout << "5 - Abimees." << endl; cin >> choice; } while (choice < 1 || choice > 5); { break; } switch (choice) { //Addition.
It will compile "succesfully" but the script is broken, if I enter any number/letter and press Enter it will exit immediately... Right..
I also tried to make it like this.
c++ Syntax (Toggle Plain Text)
//Main program: for (;;) { do { cout << "1 - Addition." << endl; cout << "2 - Subtraction." << endl; cout << "3 - Multiplication." << endl; cout << "4 - Division." << endl; cout << "5 - Abimees." << endl; cin >> choice; } while (choice < 1 || choice > 5) break; switch (choice) { //Addition.
C++ Syntax (Toggle Plain Text)
syntax error : missing ';' before 'break'
Well, I'm stuck... Again.. Would be nice if anyone would told me what to do... Well, would be even better, if I would be told what to put in the "while" statement...
the syntax for a do-while loop is this:
and maybe consider changing your 'switch' statement to a series of if-else since there's more than 1 or 2 lines of code for each option. I think that's more of a style issue.
Edit: one more thing I noticed
you have your 'for(;
' but once you run through your do-while loop, you have a break, exiting the for loop. So you only run through the for loop once, maybe look at why you have a break there
Hope that's helpful
c++ Syntax (Toggle Plain Text)
do { //stuff to loop through } while(conditions); //note the colon
and maybe consider changing your 'switch' statement to a series of if-else since there's more than 1 or 2 lines of code for each option. I think that's more of a style issue.
Edit: one more thing I noticed
you have your 'for(;
' but once you run through your do-while loop, you have a break, exiting the for loop. So you only run through the for loop once, maybe look at why you have a break thereHope that's helpful
Last edited by jesseb07; Jul 21st, 2009 at 9:03 am.
Ps. 121
Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html
AJAX, PHP, C#, C++, JAVA
Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html
AJAX, PHP, C#, C++, JAVA
•
•
Join Date: Jul 2009
Posts: 19
Reputation:
Solved Threads: 0
Well, thanks, mate... This is what I got, and it works..
Although, I have to admit... I don't fully understand why do I need this.
If I change it to like, "choice > 1 || choice < 5" then it stops working. But that seems more logical, doesn't it?
I won't mark it as solved, yet, because I'd like to know why do I need the "while" statement to be like that (shown in the code tags, previously). Is it because if you put a number below 1 and over 5 it will "re-do" the Do function? Seems to function like so...
Thank you all, though.
metalclunch
c++ Syntax (Toggle Plain Text)
//Main program: for (;;) { do { cout << "1 - Addition." << endl; cout << "2 - Subtraction." << endl; cout << "3 - Multiplication." << endl; cout << "4 - Divide." << endl; cout << "5 - Help." << endl; cin >> choice; } while (choice < 1 || choice > 5); switch (choice) { default: cout << "If you see me, report the 'default' bug!"; //Liitmine.
Although, I have to admit... I don't fully understand why do I need this.
c++ Syntax (Toggle Plain Text)
while (choice < 1 || choice > 5);
I won't mark it as solved, yet, because I'd like to know why do I need the "while" statement to be like that (shown in the code tags, previously). Is it because if you put a number below 1 and over 5 it will "re-do" the Do function? Seems to function like so...
Thank you all, though.
metalclunch
Last edited by metalclunch; Jul 21st, 2009 at 9:52 am.
•
•
•
•
Although, I have to admit... I don't fully understand why do I need this.
If I change it to like, "choice > 1 || choice < 5" then it stops working. But that seems more logical, doesn't it?c++ Syntax (Toggle Plain Text)
while (choice < 1 || choice > 5);
c++ Syntax (Toggle Plain Text)
while (choice < 1 || choice > 5);
>If I change it to like, "choice > 1 || choice < 5" then it stops working. But that seems more logical, doesn't it?
I know what you mean, if you want to have it work in that way, then you have to replace the logical OR by a logical AND, so that it becomes:
choice > 0 && choice < 5 (I also changed the one to a zero here).(Because the number has to be higher than zero AND lower than five, it's not enough that just one of these conditions is true, they must be both true).
Last edited by tux4life; Jul 21st, 2009 at 9:59 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
![]() |
Similar Threads
- how to solve this (IT Professionals' Lounge)
- Problem to find smallest no divisible by all of the first 20 natural numbers (C++)
- Delete columns (Perl)
- Help solving this problem (C++)
- isFixReg() ??? (Java)
- Problem in Korn shell script (Shell Scripting)
- problem about the codding (C)
Other Threads in the C++ Forum
- Previous Thread: AfxBeginThread Memory Leak?
- Next Thread: I really don't know how to make programs....please Solve this for me
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






