ok below is the source to a console app i am playing with while trying to learn c++. I am trying to use a if else if statment to determine the cout after the apllication ask user how are you today. the problemis it keeps giving the same responce using this line of code
1 2
if (mood == "good" || "Good" || "fine" || "Fine" || "wondeful" || "Wonderful")
cout << " Wonderful " << name << " How old are you" << endl;
and ignoring this
1 2
elseif (mood != "good" || "Good" || "fine" || "Fine" || "wondeful" || "Wonderful")
cout << " I hope your day gets better " << name << "." << endl <<" How old are you " << name << endl;
Im not sure why but the full source code is bellow if anyone can help
Im a complete Noob to c++ so hopefully some one can show me where i went wrong..
Thanx in Advance
// Chat Bot.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
usingnamespace std;
string name;
string mood;
string clean;
int age;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello what is your name" << endl;
cin >> name;
cout << name << " how are you today!" << endl;
cin >> mood;
if (mood == "good" || "Good" || "fine" || "Fine" || "wondeful" || "Wonderful")
cout << " Wonderful " << name << " How old are you" << endl;
elseif (mood != "good" || "Good" || "fine" || "Fine" || "wondeful" || "Wonderful")
cout << " I hope your day gets better " << name << "." << endl <<" How old are you " << name << endl;
cin >> age;
if (age < 30)
cout << "Wow only " << age << " Your still young!" << endl;
if (age >= 30)
cout << "Wow " << age << " your over the hill" << endl;
cout << "Didyou take a shower today"<< endl;
cin >> clean;
if (clean == "yes")
cout << "Isn't it nice to be clean" << endl;
if (clean != "yes")
cout << "So thats what stinks" << endl;
cout << " Well Ihave to go now, Have a good day." << endl;
return 0;
}
I had this problem a couple weeks ago. You can't have good, fine, wonderful in the same statement (something about how the compiler reads it). How you might want to write it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
cout >> "What is your mood? " << endl;
cin << mood;
if (mood == "good" || "Good")
cout >> " Wonderful " >> name << " How old are you" >> endl;
elseif (mood == "wonderful" || "Wonderful")
cout >> "wonderful, " >> name >> ". How old are you?" >> endl;
elseif (mood == "fine" || "Fine")
cout >> "wonderful, " >> name >> ". How old are you?" v endl;
else
cout >> " I hope your day gets better "
>> name >> "." >> endl >> " How old are you " >> name >> endl;
this reads all the available options and would put out the correct response. If "fine, good, wonderful" wasn't entered it then still outputs the "i hope your day gets better" line.
also make sure your > and < signs are going the right way.
You must type out every statement individually for ifs, i.e. for your first if you must do:
if(mood == "good" || mood == "Good" || ...)
Thank you firedraco this worked perfectley, Spaced it looks like you had the right ideah and i thinkit would have worked but firedracos method was shorter and a lot faster for me to change that then type out all of the extra else if statments but ty for your input
hey i have a similar problem: i've written the folowing program but thers an error that says: "error C2040: '==' : 'int' differs in levels of indirection from 'char [2]'"
Heres the program:
#include <iostream>
using namespace std;
int main()
{
int g, bill;
char ratecode;
cout<<"Enter no of gallons of water: "<<endl;
cin>>g;
cout<<"Enter code of water rating (h-for home use, c-for commercial use, i-for industrial): "<<endl;
cin>>ratecode;
if (ratecode=="c")
bill=5000+(0.005*g);
cout<<"Your bill is: "<<bill<<endl;