First, you wouldn't need all the characters you declared.
You probably wanted to use "+" where you wrote
1 2
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
1 2 3 4 5 6 7
if (choice=='y')
//your code
elseif(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:
#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?
@ 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 elseif, 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.
#include <iostream>
#include <string>
#include <sstream>
usingnamespace 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";
}
elseif ( str == b )
{
cout << " I am pleasant as always. \n";
}
return 0;
}