954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

New to C++ need some help

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.

#include <iostream>
using namespace std;


int main ()
{

	int money = 500;
	int moneyspent;
	
	do
	{
	
	cout<<"Please enter how much money you would like to spend"<<endl;
	cin >> moneyspent;

	if(moneyspent < 500)
		
		money = money - moneyspent;
		
	cout<<"You have "<<money<<" dollars left to spend"<<endl;
	
	}
	while(moneyspent != 500);

	cout <<"You are out of cash jarboni"<<endl;

	return 0;

}
simon126
Newbie Poster
23 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

please help

simon126
Newbie Poster
23 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

Hint: You loop while your money is above zero!

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You