### if else problems

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` ``````#include "stdafx.h" #include #include #include using namespace std; int main() { char choice; char a; char b; char m; char d; char r; char q; int n1,n2,answer,useranswer; bool quitnow = false; srand(static_cast(time(NULL))); n1 = rand()%20+1; n2 = rand()%20+1; answer = n1 = n2; cout << "a.Find the sum of two numbers" << a << endl; cout << "b.Subtract two numbers" << b << endl; cout << "m.find the product of two numbers" << m << endl; cout << "d.Divide two numbers" << d << endl; cout << "r. find the Modulus of two numbers" << r << endl; cout << "q.Quit the program" << q << endl; cin >> choice; if(choice == a) { cout << "what is" << n1 << "+" << n2 << "?"; cin >> useranswer; } else(choice == b); { cout << "what is" << n1 << "-" << n2 << "?"; cin >> useranswer; } else(choice == m) { cout << "what is" << n1 << "*" << n2 << "?"; cin >> useranswer; } else(choice == d) { cout << "what is" << n1 << "/" << n2 << "?"; cin >> useranswer; } else(choice == r) { cout << "what is" << n1 << "%" << n2 << "?"; cin >> useranswer; } else(choice == q) { quitnow = true; } return 0; }``````

what is wrong with my program?
It should go something like this:
 ``1234567891011`` `````` if (choice == 'a') { // do something } else if (choice == 'b') { // do something } // etc. ``````
First, you wouldn't need all the characters you declared.
You probably wanted to use "+" where you wrote
 ``12`` `````` answer=n1=n2; //it should be answer=n1+n2; ``````

Then all your cout-s are all faulty.
All the variables declared are not initialized, yet you wanna display their content(which would be garbage). In your if statements, all other else would be else if but with the exception of the last else. Such that you would have
 ``1234567`` ``````if (choice=='y') //your code else if(choice=='h') //do this //continue the else if until the last else which would be else //blah blah blah ``````
Hey guys I'm new. I tried looking all over but I can't grasp the idea. I downloaded C4droid on my nexus phone and am trying to make conditional responses. Here's what I have so far:

#include <iostream>
#include <string>
#include <sstream>

#define a "What is your name?"
#define b "How are you today?"

int main ( )
{
string str;
cout << "How may I assist you? forward slash n";
getline ( cin, str );
if ( str == a )
{
cout << " My name is Phi. forward slash n";
}
else ( str == b )
{
cout << " I am pleasant as always. forward slash n"
}
return 0;
}

now the problem is that whenever I type in "what is your name?" I get both responses: "My name is Phi. I am pleasant as always."

I want it to only say its name when I ask for its name. What am I doing wrong?

I apologize for not copy and pasting, I'm writing this on my laptop.

Okay nvm, I solved the issue... but now, how do I make it so that the program isn't so picky? Instead of having to put "How are you today?" I would like to put "how have you today" and still get the same result, or do I just have to make it so that when I define it, that's part of the definition?
Last edited on
@ tmalcuit90 This would be better as a new thread, but I'll try to answer here
This line has three problems:
`else ( cin == b );`

First, it should read `else if `, not just `else`.
Secondly, the line ends with a semicolon, which finishes that if-else structure. That means the following lines are always executed.
Third, the stream `cin` cannot be compared with a string like that.

In C++, it is preferred to use a `const` declaration instead of #define.
 ``12345678910111213141516171819202122232425`` ``````#include #include #include using namespace std; const string a = "What is your name?"; const string b = "How are you today?"; int main ( ) { string str; cout << "How may I assist you? \n"; getline ( cin, str ); if ( str == a ) { cout << " My name is Phi. \n"; } else if ( str == b ) { cout << " I am pleasant as always. \n"; } return 0; }``````
Topic archived. No new replies allowed.