Ok, so I am trying to write a program that will solve simple math and physics problems, and am trying to use strings and words more, so my question is:
is it possible to use a char in an if statement? I'm new to this so yeah. Here is what I tried and my compiler said "NO" so does anyone know a way to get it to work?

Here's the code I have (just the part with the if statement):

cout << "Please select a type of problem: ";
cout << "\n\nAlgebra";
cout << "\n Physics";
char type[10];
cin >> type;

if (type=algebra)
       {

and so on and so on but you prolly get the idea

Thanks in advance everyone!

Ok, so I am trying to write a program that will solve simple math and physics problems, and am trying to use strings and words more, so my question is:
is it possible to use a char in an if statement? I'm new to this so yeah. Here is what I tried and my compiler said "NO" so does anyone know a way to get it to work?

Here's the code I have (just the part with the if statement):

cout << "Please select a type of problem: ";
cout << "\n\nAlgebra";
cout << "\n Physics";
char type[10];
cin >> type;

if (type=algebra)
       {

and so on and so on but you prolly get the idea

Thanks in advance everyone!

I'm guessing your intent is to compare to the string literal "algebra" , but what you wrote is actually attempting to store the value stored in the variable called algebra to the variable called type . You need to add the quotes to make the compiler interpret it as a string literal and you need to use a double equal ( == ) to execute an equality comparison, single equal is assignment. Even though, you can't compare a string literal to a character array.

I'd suggest converting it to an integer-driven menu rather than a string-driven one:

cout << "Please select a type of problem: " << endl << endl;
cout << "1.)\tAlgebra" << endl;
cout << "2.)\tPhysics" << endl;
int type = 0;
cin >> type;

if (type == 1) //algebra selected
       {...

My C++ is a little rusty, and FBody said (but might have got lost in there) is you probably meant to write this:

if (type=="algebra")

The string you are comparing it to needs to be in quotes, and "==" means "equals" and "=" means "assignment". IIRC

if (type="algebra")

would always return true (unless you set something = 0 ... maybe.)

Thanks everyone for your posts! I've decided to take Fbody's advice and to just switch it to an integer driven menu. Thanks everyone for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.