If statement prob.

Im a beginner as you can see below have written the following code :

#include <iostream>
#include <string>
#include <stdlib.h>


int main () {
using namespace std;


float monthmoney;
float usedmoney;
char a,y,n;

cout<<"Give me your month money"<<endl;
cin >> monthmoney;

cout << "Do you want to purchase something? Y/N" << endl;
cin >> a;

if (a==y)
{
cout << "Type the amount you want to spend";
cin >> usedmoney;
monthmoney-=usedmoney;
cout << monthmoney;



}
else
{

cout << "Goodbye";

}




return 0;
}



The program runs but when i type y (i want to make a purchase ) it just goes to the else statement:

[Switching to process 1808]
Running…
Give me your month money
700
Do you want to purchase something? Y/N
y
Goodbye
Debugger stopped.
Program exited with status value:0.

I modified the program like this to see if the problem was the characters and it worked :
cout << "Do you want to purchase something? Y=1/N=2" << endl;
cin >> a;

if (a==1) {...........}

But i want the user to give me Y/N not a number can someone post the solution please?
Thanks
I don´t know why y/n are 1/2.. But you should do if(y=='y') //note the '' around y
When you use a==1 it's a good expression, since you don't need to use the '' there...

Just remember: for char evaluations, use ", for numeric evaluations don't..

Experts will explain more ;)

also please put your code in [code] tags :)

Cheers!
Last edited on
Topic archived. No new replies allowed.