Calculator help no output

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 28
Reputation: infern0 is an unknown quantity at this point 
Solved Threads: 0
infern0 infern0 is offline Offline
Light Poster

Calculator help no output

 
0
  #1
Oct 19th, 2009
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:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7. char sign;
  8. string input;
  9. string total=0;
  10. cout << "Current total is 0" << endl;
  11. do
  12. { cout << "Enter an operation: + - * / (or enter X to exit):";
  13. cin >> sign;
  14. if (sign = '+')
  15. cout <<"Enter a number: ";
  16. cin >> input;
  17. cout <<"Current total is " << input + total;
  18. }
  19. while (input != "X");
  20. }

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 151
Reputation: Phil++ is an unknown quantity at this point 
Solved Threads: 3
Phil++ Phil++ is offline Offline
Junior Poster
 
0
  #2
Oct 19th, 2009
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7. char sign;
  8. string input;
  9. int total=0; // No need for a string, is there?
  10. cout << "Current total is 0" << endl;
  11. do
  12. { cout << "Enter an operation: + - * / (or enter X to exit):";
  13. cin >> sign;
  14. if (sign == '+')
  15. {
  16. cout <<"Enter a number: ";
  17. cin >> input;
  18. cout <<"Current total is " << input + total;
  19. }
  20. }while (input != "X");
  21. }

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,400
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 28
Reputation: infern0 is an unknown quantity at this point 
Solved Threads: 0
infern0 infern0 is offline Offline
Light Poster
 
0
  #4
Oct 19th, 2009
I don't know how to use arrarys yet
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 28
Reputation: infern0 is an unknown quantity at this point 
Solved Threads: 0
infern0 infern0 is offline Offline
Light Poster
 
0
  #5
Oct 19th, 2009
Originally Posted by Dave Sinkula View Post
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.
I was trying to use double to identify the input..but I can't use "X" because it is not a number.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 151
Reputation: Phil++ is an unknown quantity at this point 
Solved Threads: 3
Phil++ Phil++ is offline Offline
Junior Poster
 
0
  #6
Oct 19th, 2009
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7. char sign;
  8. int input;
  9. int total=0; // No need for a string, is there?
  10. cout << "Current total is 0" << endl;
  11. do
  12. { cout << "Enter an operation: + - * / (or enter X to exit):";
  13. cin >> sign;
  14. if (sign == '+')
  15. {
  16. cout <<"Enter a number: ";
  17. cin >> input;
  18. cout <<"Current total is " << input + total;
  19. }
  20. }while (input != "X");
  21. }

Actually, there is no need to use string since you're only inputting a number. Char array:
  1. char name[255];
Strings:
  1. string sentence = "Hello my name is Phil and I hate C++";
where as using single char, would just print out "H"
Last edited by Phil++; Oct 19th, 2009 at 7:13 pm.
If you ask me questions through Private messaging I won't reply.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,400
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #7
Oct 19th, 2009
Originally Posted by infern0 View Post
I was trying to use double to identify the input..but I can't use "X" because it is not a number.
So take input as a string, and if it's not "X", then convert it to a number.
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 28
Reputation: infern0 is an unknown quantity at this point 
Solved Threads: 0
infern0 infern0 is offline Offline
Light Poster
 
0
  #8
Oct 19th, 2009
Originally Posted by Dave Sinkula View Post
So take input as a string, and if it's not "X", then convert it to a number.
I don't know how to convert a string to a number.. I tried looking it up but it looks complicated.

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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 151
Reputation: Phil++ is an unknown quantity at this point 
Solved Threads: 3
Phil++ Phil++ is offline Offline
Junior Poster
 
0
  #9
Oct 19th, 2009
What errors?
If you ask me questions through Private messaging I won't reply.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,400
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #10
Oct 19th, 2009
Originally Posted by infern0 View Post
I don't know how to convert a string to a number.. I tried looking it up but it looks complicated.

Getting 10 errors in the build current. I think it's because of the input problem with the while statement.
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC