Page 1 of 1

Hitting a brick wall.

Posted: Fri Jun 26, 2009 12:00 pm
by Rhys
Can someone please clarify why this isn't working, I don't really understand why, I think it's the if and else statement.

Code: Select all


#include <iostream>
using namespace std;

int main(void)

{
	// Variables.
	string name;
	string mood;
	system("TITLE Conversing with Sam");
	cout << "Hi. What is your name?" << endl << endl;
	
	cin >> name;

	"Well, hello, " << name << ", how are you?" << endl << endl;

	cin >> mood;

	if (mood == "good" || "Good"){
		cout << "That's good.";
	}	else {
		cout << "Oh, that's no good.";
	}

		return 0;
	}
Help would be much appreciated!

Re: Hitting a brick wall.

Posted: Fri Jun 26, 2009 12:04 pm
by avansc
if (mood == "good" || mood == "Good"){

Re: Hitting a brick wall.

Posted: Fri Jun 26, 2009 12:57 pm
by zodiac976
Sorry I messed up the post anyways avansc is right and also
you are missing two important things:

#include <string>
and
cout << "Well, hello, " << name << ", how are you?" << endl << endl;

I recommend using cin.ignore() after each cin to kill the
newline in the buffer or change all variables to strings and
use getline(cin, string variable); the third parameter is an
optional terminating character.

Re: Hitting a brick wall.

Posted: Sat Jun 27, 2009 12:19 am
by Rhys
avansc wrote:if (mood == "good" || mood == "Good"){
Ah, thanks, thought that might be a bit of a problem.
zodiac976 wrote:#include <string>
and
cout << "Well, hello, " << name << ", how are you?" << endl << endl;

I recommend using cin.ignore() after each cin to kill the
newline in the buffer or change all variables to strings and
use getline(cin, string variable); the third parameter is an
optional terminating character.
That would also help, heh. I don't really get what you mean by the newline in the buffer.

Re: Hitting a brick wall.

Posted: Sat Jun 27, 2009 1:02 pm
by MarauderIIC
When you use cin, "\n" is left for processing, so you have to do something to eat it, otherwise if you call getline(), it'll return immediately, since it goes until it finds a \n. You can get rid of this "\n" so that your getline() doesn't immediately return in a whole slew of ways:
cin.sync()
cin.ignore()
getline(cin, myDummyString); [where myDummyString is a C++ string, as opposed to a character array]
cin.getline(myCharacterArray, sizeof(myCharacterArray)-1)

If you use cin.getline() or getline(), "\n" is not left on the buffer. Rather, it is automatically discarded.

cin.sync() looks like the "best" option, but I haven't used it yet.

Re: Hitting a brick wall.

Posted: Wed Jul 08, 2009 12:27 pm
by derbon
if(mood == "good" || "Good") doesn't mean 'if your mood is good or Good". The || is used like if(condition ||condition). So you have to check "good" and "Good".

Re: Hitting a brick wall.

Posted: Wed Jul 08, 2009 12:32 pm
by programmerinprogress
derbon wrote:if(mood == "good" || "Good") doesn't mean 'if your mood is good or Good". The || is used like if(condition ||condition). So you have to check "good" and "Good".
as far as i'm aware the latter part of the satement is wrong, mainly because you are evaluating whether the string "Good" is true or false, when you are supposed to be evaluating whether mood is equal to the string "Good"

so just pop mood == "Good" in there and the statement should be correct in terms of syntax, I don't know about the question itself yet as I haven't looked.

Code: Select all

// this is correct
if(mood == "good" || mood ==  "Good") 
EDIT: and after a closer look, avansc already solved this AGES ago, well I explained his reasoning anyway I guess :lol:

Re: Hitting a brick wall.

Posted: Thu Jul 09, 2009 10:12 am
by CowboyX86
Hi,

PS: I'm not C++ programmer, I just convert a C code to C++!

I always like to do this:

Code: Select all

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main ()
{
	char Key[] = "APPLE";
	char Input[80];
	
	do {
		cout << "Guess my favourite fruit? ";
		gets_s (Input);
		
		for(unsigned int i=0;i<79;i++)
			Input[i] = toupper(Input[i]);
		
	} while (strcmp (Key,Input));
	puts ("Correct answer!");
	getchar();
	return 0;
}
So, don't matter if you type: Apple, APPLE, apPLe... this will check great!

Bye!

Re: Hitting a brick wall.

Posted: Thu Jul 09, 2009 5:31 pm
by h0ts0up
programmerinprogress wrote:
derbon wrote:if(mood == "good" || "Good") doesn't mean 'if your mood is good or Good". The || is used like if(condition ||condition). So you have to check "good" and "Good".
as far as i'm aware the latter part of the satement is wrong, mainly because you are evaluating whether the string "Good" is true or false, when you are supposed to be evaluating whether mood is equal to the string "Good"
I think what derbon was trying to say was that you need to evaluate two different conditions because he originally only had one. Not saying your explanation is wrong, but you're pretty much repeating what derbon just said.