How to use If Else statements whith char

Oct 25, 2011 at 6:09pm
I'm trying to make my program output what your weight would be on a plant or the moon depending on what letter the user inputs. So far it's not working, if i use int values it works fine but as soon as i try using char values it won't work.I really need help on this.


heres the program


#include <iostream> //Header
#include <string>

//Constants

const double e = 0.8975; //Venus coefficient
const double a = 1.0; //Earth coefficient
const double o = 0.166; //Moon coefficient
const double u = 2.5374; //Jupiter coefficient
const double t = 1.0677; //Saturn coefficient



using namespace std;
int main( )

{

double weight;
char plantLet;
double weightOV, weightOE, weightOM, weightOJ, weightOS;
char V;
char E;
char M;
char J;
char S;


cout << "please enter your weight" << endl << endl;
cin >> weight;

cout << "Now please enter one of the numbers that corresponds" << endl;
cout << "to one of the following plants or moon:" <<endl << endl;
cout << "Venus=1, Earth=2, Moon=3, Jupiter=4, Saturn=5."<<endl << endl;
cin >> plantLet;



if (plantLet <= V)
{
weightOV = weight * e;
cout << endl;
cout << "your weight on Venus is; " << weightOV << endl;
}

else
if (plantLet == E)
{
weightOE = weight * a;
cout << endl;
cout << "your weight on Earth is; " << weightOE << endl;
}

else
if (plantLet <= M)
{
weightOM = weight * o;
cout << endl;
cout << "your weight on the Moon is; " << weightOM << endl;
}

else
if (plantLet == J)
{
weightOJ = weight * u;
cout << endl;
cout << "your weight on Jupiter is; " << weightOJ << endl;
}

else
if (plantLet == S)
{
weightOS = weight * t;
cout << endl;
cout <<"your weight on Saturn is; " << weightOS << endl;
}

else
{
if (plantLet!= V && E && M && J && S)
cout << "Error: Number entered was incorrect; please input one of the following numbers, " << endl;
cout << "1, 2, 3, 4, 5." << endl;
}



return 0;
}
Last edited on Oct 25, 2011 at 6:09pm
Oct 25, 2011 at 6:16pm
1
2
3
char V;
...
if (plantLet <= V)


This is comparing your variable plantLet to a variable named V. You never gave V a value, so this is meaningless. Just because you named the variable V does not mean it represents the character V. You could just as easily name it WorchestershireSauce -- the names of variables do not matter.


If you want to compare against the actual letter 'V', then you need to use a character literal, not a variable. Do this by putting the character inside single quotes:

1
2
3
// char V; // <- get rid of all of these

if(plantLet == 'V')  // use 'V', not V.  Also I don't know why you had <= 



Also:
 
if (plantLet!= V && E && M && J && S) 


This code does not do what you expect. There is no need for it anyway, since the 'else' chain ensures that plantLet is not any one of the previously checked options.
Oct 25, 2011 at 6:23pm
That did not work, I changed the if (plantLet == "V") and got rid of the char V; stuff but the == not has an error that says "operand types are incompatible (char and const char)"

what am I doing worng?


Update:

Oh, I found, thanks so much for the help!!!
Last edited on Oct 25, 2011 at 6:28pm
Oct 25, 2011 at 6:29pm
It's 'V', not "V"
Oct 25, 2011 at 6:36pm
Wht not? "V" works
Oct 25, 2011 at 7:45pm
'V' and "V" aren't the same thing...one is a single character, and the other is a C string with one character in it.
Oct 25, 2011 at 7:48pm
A suggestion: Use the toupper function on your "if" statements. Input is case-sensitive, and users are more likely to use lower-case input than upper-case. That will help avoid bad data.

Oh, incidentally, your instructions and your programming logic don't really match, since if the user enters a one, it'll drop down to your "else." You might want to revise them to say:

1
2
cout << "Now please enter one of the following:" << endl << endl;
cout << "(V)enus, (E)arth, (M)oon, (J)upiter, (S)aturn"<< endl << endl;
Last edited on Oct 25, 2011 at 7:49pm
Topic archived. No new replies allowed.