| | |
Calculator help no output
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 28
Reputation:
Solved Threads: 0
Hi. I'm having trouble making a simple calculator.
The code I have so far compiles, but it does not run.
Right now I only have addition in because I wanted to make sure I was going in the right direction before I added anything else.
I have to use a Do...while statement in this code.
Can anyone tell me when the program doesn't run???
Here is what I have so far:
Thanks in advance.
The code I have so far compiles, but it does not run.
Right now I only have addition in because I wanted to make sure I was going in the right direction before I added anything else.
I have to use a Do...while statement in this code.
Can anyone tell me when the program doesn't run???
Here is what I have so far:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main () { char sign; string input; string total=0; cout << "Current total is 0" << endl; do { cout << "Enter an operation: + - * / (or enter X to exit):"; cin >> sign; if (sign = '+') cout <<"Enter a number: "; cin >> input; cout <<"Current total is " << input + total; } while (input != "X"); }
Thanks in advance.
•
•
Join Date: Jan 2009
Posts: 151
Reputation:
Solved Threads: 3
0
#2 Oct 19th, 2009
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main () { char sign; string input; int total=0; // No need for a string, is there? cout << "Current total is 0" << endl; do { cout << "Enter an operation: + - * / (or enter X to exit):"; cin >> sign; if (sign == '+') { cout <<"Enter a number: "; cin >> input; cout <<"Current total is " << input + total; } }while (input != "X"); }
I wouldn't use string unless you know how to use it. Use char arrays.
Last edited by Phil++; Oct 19th, 2009 at 7:07 pm.
If you ask me questions through Private messaging I won't reply.
1
#3 Oct 19th, 2009
Some starters: Sort out your confusion about strings and numbers. Assignment is
= , comparison is == . Multiline blocks are not determined by indentation in this curly-brace language. Last edited by Dave Sinkula; Oct 19th, 2009 at 7:08 pm. Reason: D'oh! Pokey again.
"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 2009
Posts: 28
Reputation:
Solved Threads: 0
0
#5 Oct 19th, 2009
I was trying to use double to identify the input..but I can't use "X" because it is not a number.
•
•
Join Date: Jan 2009
Posts: 151
Reputation:
Solved Threads: 3
0
#6 Oct 19th, 2009
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main () { char sign; int input; int total=0; // No need for a string, is there? cout << "Current total is 0" << endl; do { cout << "Enter an operation: + - * / (or enter X to exit):"; cin >> sign; if (sign == '+') { cout <<"Enter a number: "; cin >> input; cout <<"Current total is " << input + total; } }while (input != "X"); }
Actually, there is no need to use string since you're only inputting a number. Char array:
C++ Syntax (Toggle Plain Text)
char name[255];
C++ Syntax (Toggle Plain Text)
string sentence = "Hello my name is Phil and I hate C++";
Last edited by Phil++; Oct 19th, 2009 at 7:13 pm.
If you ask me questions through Private messaging I won't reply.
0
#7 Oct 19th, 2009
Last edited by Dave Sinkula; Oct 19th, 2009 at 7:14 pm. Reason: Slow on the draw again; added quote.
"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 2009
Posts: 28
Reputation:
Solved Threads: 0
0
#8 Oct 19th, 2009
•
•
•
•
So take input as a string, and if it's not "X", then convert it to a number.
Getting 10 errors in the build current. I think it's because of the input problem with the while statement.
Last edited by infern0; Oct 19th, 2009 at 7:24 pm.
0
#10 Oct 19th, 2009
Converting a string to a number is just about the most frequently asked question. Surely even a rudimentary search would give you a hint. (Part of learning to code is learning how to find answers. This is a good time to start, I'd say.)
"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
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: How do you add the same members of a do while loop?
- Next Thread: reading in info from a file and sorting it
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






