Trying to create

Thread Solved
Reply

Join Date: Sep 2005
Posts: 102
Reputation: Niklas is an unknown quantity at this point 
Solved Threads: 0
Niklas's Avatar
Niklas Niklas is offline Offline
Junior Poster

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

 
0
  #1
Sep 20th, 2005
: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.


  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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 102
Reputation: Niklas is an unknown quantity at this point 
Solved Threads: 0
Niklas's Avatar
Niklas Niklas is offline Offline
Junior Poster

Re: Trying to create

 
0
  #2
Sep 20th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Trying to create

 
0
  #3
Sep 20th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

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

 
0
  #4
Sep 20th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 102
Reputation: Niklas is an unknown quantity at this point 
Solved Threads: 0
Niklas's Avatar
Niklas Niklas is offline Offline
Junior Poster

Re: Trying to create

 
0
  #5
Sep 20th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 102
Reputation: Niklas is an unknown quantity at this point 
Solved Threads: 0
Niklas's Avatar
Niklas Niklas is offline Offline
Junior Poster

Re: Trying to create

 
0
  #6
Sep 20th, 2005
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?



  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 16
Reputation: drock9975 is an unknown quantity at this point 
Solved Threads: 1
drock9975 drock9975 is offline Offline
Newbie Poster

Re: Trying to create

 
0
  #7
Sep 21st, 2005
actually it worked ok for me
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 102
Reputation: Niklas is an unknown quantity at this point 
Solved Threads: 0
Niklas's Avatar
Niklas Niklas is offline Offline
Junior Poster

Solved: Trying to create

 
0
  #8
Sep 21st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 16
Reputation: drock9975 is an unknown quantity at this point 
Solved Threads: 1
drock9975 drock9975 is offline Offline
Newbie Poster

Re: Solved: Trying to create

 
0
  #9
Sep 21st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Trying to create

 
0
  #10
Sep 21st, 2005
>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:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC