I am trying to make a simple program that can do some basic algebra equations in a command prompt. The problem is no matter what I type into the command prompt at the beginning it always displays A = Solve A in A+B=C even when I type in A it still gives me the results of H. Can anyone tell me why this is happening?
#include <iostream>
usingnamespace std;
int main()
{
char problem;
cout<<"Enter The Letter Of The Problem You Want To Solve (Type H For a List): ";
cin>>problem;
if (problem='H') {
cout<<"A = Solve A in A+B=C \n";
cin.get(); }
elseif (problem='A') {
float C;
float B;
cout<<"We Are Going To Calculate The Value Of A In The Equation A+B=C \n";
cout<<"Enter B's Value: ";
cin>>B;
cout<<"Enter C's Value: ";
cin>>C;
float A=C-B;
cout<<"A Equals: "<<A;
cin.get();
return 0; }
else
cout<<"Invalid Entry";
cin.get();
return 0;
}
(problem='H') does not check whether problem is equal to 'H', it ASSIGNS the value 'H' to problem. By definition any nonzero value is assumed true, therefore the first if is executed.
To be honest, I find your code to be a little sloppy. You should drop that first if statement:
if (problem ='H')
and just tell the user what the menu options are. After that you can use your problem variable to store the users input from the menu options. The logic should then flow into a switch case. Which can be learned here : http://www.cplusplus.com/doc/tutorial/control/
Essentially you want your various algebraic expressions to be there own cases. Your default will be your error output.
Another thing, initialize your variables. You only initialize your (char) problem variable. You need to initialize (int) A, B, and C. Like so:
int A, B, C;
You can not use variables that you have yet to create.
I'm no pro at coding - having just figured out pointers after a week of studying - so I may not be the best suited to aide you; these are only a few suggestions from one rookie programmer to another. Hope it helps.
thanks Osor77 you were right. Somehow I forgot about that.
@Fallen Elijah
I would just tell the user what the options are but the thing is i plan to add alot more equations to it and then the command prompt would end up looking sloppy. Also I am just going to be initializing A,B, and C depending on the equation so there values could be different. For example when you enter A's value in the example above is C-B and that might be different in another equation so i can't really initialize it above.
Sorry if that didn't really make sense im not very good at explaining things.