944,198 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2670
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 20th, 2005
0

Trying to create basic calculator but...Some help please

Expand Post »
:mrgreen: I've just started learning and doing examples out of the book but I wanted to see if I could actually make something besides just copying stuff out of a book. So I tried to make an addition calculator.
This isnt the entire source code but just a few snippets to make sure that im getting the procedure right.


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. cout << "Plase enter 2 numbers to be added ";
  7. int a;
  8. int b;
  9. cin >> a;
  10. cin >> b;
  11. cout << " a + b = " << a + b << endl;
  12.  
  13. return 0;
  14. }

Would this work?

If this goes well I would like to expand it with if statements by asking what function to use with cout having the user enter either a for + b for - c for * and d for /. Then assign a function that if variable "number" is = "(whatever operation sign given)" then do this.

Does this seem logicaly reasonable or have i got the entire procedure wrong on hwo to do it?
Similar Threads
Reputation Points: 14
Solved Threads: 0
Junior Poster
Niklas is offline Offline
104 posts
since Sep 2005
Sep 20th, 2005
0

Re: Trying to create

When I run the program It says "Please enter the 2 numbers that will be added" but when I try to enter a number and press enter it doesnt do anything it just keeps skipping lines. Why is it not working. I declared the variables. Asked the user to enter them. And had it calculate it.
Reputation Points: 14
Solved Threads: 0
Junior Poster
Niklas is offline Offline
104 posts
since Sep 2005
Sep 20th, 2005
0

Re: Trying to create

Quote originally posted by Niklas ...
When I run the program It says "Please enter the 2 numbers that will be added" but when I try to enter a number and press enter it doesnt do anything it just keeps skipping lines. Why is it not working. I declared the variables. Asked the user to enter them. And had it calculate it.
I did tried your code..its working fine
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Sep 20th, 2005
0

Re: Trying to create basic calculator but...Some help please

I ran it and it works ok. In response to the prompt you enter a, then a space, then b, then a carriage return. You can also respond a, carriage return, b, carriage return. Your comments indicate that you expect it to do something after you enter a. What it's doing is waiting for b.

Good Luck
Reputation Points: 21
Solved Threads: 1
Junior Poster in Training
murschech is offline Offline
60 posts
since Dec 2004
Sep 20th, 2005
0

Re: Trying to create

well I ran the program didnt work. I recompiled it and ran it and it still didnt work. My compiler must be screwed up im using DEC C++ that game with "Beginning C++ Game Programming" by Michael Dawson.
Reputation Points: 14
Solved Threads: 0
Junior Poster
Niklas is offline Offline
104 posts
since Sep 2005
Sep 20th, 2005
0

Re: Trying to create

EDIT: changed the source code again to figure out where the problem is. So now I can enter first number and it will display what num1 =. Now when I enter the second number it just exits. Now whats going wrong?



C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. cout << "Please enter 2 numbers to be added ";
  7. int a;
  8. int b;
  9. cin >> a;
  10. cout << "\n num1: " << a << endl;
  11. cin >> b;
  12. cout << " num2: " << b << endl;
  13. cout << " a + b = " << a + b << endl;
  14.  
  15. cout << "Press the enter key to exit";
  16. cin.ignore(cin.rdbuf()->in_avail() + 1);
  17. return 0;
  18. }

Sorry if I just forgot something really obvious.
Reputation Points: 14
Solved Threads: 0
Junior Poster
Niklas is offline Offline
104 posts
since Sep 2005
Sep 21st, 2005
0

Re: Trying to create

actually it worked ok for me
Reputation Points: 10
Solved Threads: 1
Newbie Poster
drock9975 is offline Offline
16 posts
since Jun 2005
Sep 21st, 2005
0

Solved: Trying to create

What compiler are you guys using. Im using DEV C++compiler. For me it works it displys message to type in the 2 numbers. Type in first number press enter and it displays the first number = the variable. Then it ask for second number and you put the number in and it exits for some reason. Could this have to do with memory allocation and im going over the amount of memory I set aside for the answer?

Since it seems like this is just gonna go one for ever saying "it works for me" someone just lock this thread.
Reputation Points: 14
Solved Threads: 0
Junior Poster
Niklas is offline Offline
104 posts
since Sep 2005
Sep 21st, 2005
0

Re: Solved: Trying to create

at first i thought that maybe the new line created by the return might make the second cin just read that, but then i threw it in vs.net 2003 and it worked fine, so i retracted that

not sure what his problem is
Reputation Points: 10
Solved Threads: 1
Newbie Poster
drock9975 is offline Offline
16 posts
since Jun 2005
Sep 21st, 2005
0

Re: Trying to create

>Since it seems like this is just gonna go one for ever saying "it works for me" someone just lock this thread.
Maybe you should be more specific than "it doesn't work". It sounds like you're experiencing the first problem that most Dev-C++ users ask here, where the output window closes when the program terminates, and you don't see the complete output. Try this:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <limits>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. cout << "Please enter 2 numbers to be added ";
  8. int a;
  9. int b;
  10. cin >> a;
  11. cout << "\n num1: " << a << endl;
  12. cin >> b;
  13. cout << " num2: " << b << endl;
  14. cout << " a + b = " << a + b << endl;
  15.  
  16. cout << "Press the enter key to exit";
  17. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  18. cin.get();
  19.  
  20. return 0;
  21. }
>cin.ignore(cin.rdbuf()->in_avail() + 1);
This isn't strictly guaranteed to do what you think it's doing, for several somewhat advanced reasons that I'm not terribly interested in explaining right now. But you can treat it like cin.sync() and avoid it in favor of the above example most of the time.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Program keeps crashing...
Next Thread in C++ Forum Timeline: Multidimesional Arrays in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC