943,985 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1460
  • C++ RSS
Oct 29th, 2005
0

New to C++ need some help

Expand Post »
The following code works. However i want it so when the user reaches 500 dollars the last cout statement will run. But for some reason i can't get it to work properly. It skips the last cout statment and enters -numbers.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int main ()
  6. {
  7.  
  8. int money = 500;
  9. int moneyspent;
  10.  
  11. do
  12. {
  13.  
  14. cout<<"Please enter how much money you would like to spend"<<endl;
  15. cin >> moneyspent;
  16.  
  17. if(moneyspent < 500)
  18.  
  19. money = money - moneyspent;
  20.  
  21. cout<<"You have "<<money<<" dollars left to spend"<<endl;
  22.  
  23. }
  24. while(moneyspent != 500);
  25.  
  26. cout <<"You are out of cash jarboni"<<endl;
  27.  
  28. return 0;
  29.  
  30. }
Last edited by Dave Sinkula; Oct 29th, 2005 at 6:33 pm. Reason: Added [code][/code] tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simon126 is offline Offline
23 posts
since Mar 2005
Oct 29th, 2005
0

Re: New to C++ need some help

please help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simon126 is offline Offline
23 posts
since Mar 2005
Oct 29th, 2005
0

Re: New to C++ need some help

Pretend you're the computer, and keep track of the values of money and moneyspent given some arbitrary input values to the program. You'll see your problem.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Oct 29th, 2005
0

Re: New to C++ need some help

Hint: You loop while your money is above zero!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

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: queue implementation error
Next Thread in C++ Forum Timeline: Cbuilder Fullscreen form how to ???





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


Follow us on Twitter


© 2011 DaniWeb® LLC