I'm trying to self-teach (myself) visual C++, and i'm starting with a simple calculator. i'm trying to ask the user to input how to calculate an answer, but i'm having trouble with the if statements. heres my source code for that part:
cout %26lt;%26lt; "Ok,\n Would you like to Add, Subtract, Multiply or Divide?\n\n";
int type = 0;
int add = ;
int subtract = ;
cin %26gt;%26gt; type;
if ( type == add ) {
cout %26lt;%26lt; TotalPlus;
}
elseif ( type == subtract ) {
cout %26lt;%26lt; TotalMinus
}
I have no idea what to put in the int add and int subtract. any help? I have defined all the intigers and totals exc. above, i didnt include that code for the sake of character space on the question's body paragraph.
"if" statements in Visual C++ (Microsoft VC++ 2008)?
What does the user enter to say they want to add? You didn't tell them what to enter. As a result, there's no way to say what the value of "add" or "subtract" should be.
If you prompt with "Enter 1 to add or 2 to subtract" then you could say add = 1 and subtract = 2.
Otherwise I don't know what to tell you.
Reply:What you want to do is use integers to get a function. You would half to use a enum. Dont do that.
#include %26lt;string%26gt;
void main(){
string function;
cout %26lt;%26lt; "What function?";
cin %26gt;%26gt; function;
if (function == "add")
...
elseif
...
}
YOU CAN ALSO DO THIS
cout %26lt;%26lt; "1 - add, 2 - subtract";
int func = 0;
if (func == 1)
...
elseif (func == 2)
http://iamtruancy.co.nr/
Navigate to C++
This is my site, ask if you have any problems
Reply:dude, i noticed an "elseif" statement in your code. That is not valid, you must be a vb programmer lolz... here's the syntax of an if statement:
if (expression)
{
//do this
}
else if (expression)
{
//do that..
}
else
{
// fallback codes..
}
You can also use the so called ternary operator when your condition is limited to two options, example:
(expression)?(do this if true):(or this if false);
Hope this one helps.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment