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;
	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;


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.

Hint: You loop while your money is above zero!