Hitting a brick wall.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Rhys
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Mon Mar 09, 2009 12:08 am
Current Project: Learning from my C++ book.
Favorite Gaming Platforms: PC. Lurv it.
Programming Language of Choice: C++.
Location: Melbourne, Australia.

Hitting a brick wall.

Post 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!
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Hitting a brick wall.

Post by avansc »

if (mood == "good" || mood == "Good"){
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Hitting a brick wall.

Post 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.
Rhys
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Mon Mar 09, 2009 12:08 am
Current Project: Learning from my C++ book.
Favorite Gaming Platforms: PC. Lurv it.
Programming Language of Choice: C++.
Location: Melbourne, Australia.

Re: Hitting a brick wall.

Post 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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Hitting a brick wall.

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
derbon
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Tue Jun 09, 2009 7:25 pm
Current Project: Big Nasty Enemies
Favorite Gaming Platforms: NES SMS snes PC N64 PS2 Vectrex The Arcade Machine
Programming Language of Choice: C++, Perl
Location: why shud i tell u u mite steal my tv
Contact:

Re: Hitting a brick wall.

Post 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".
i think the best paint program would be microsoft paint + paint.NET+graphics gale + Paint shop Pro 7 + photoshop CS3, it would be called Paint Gale Pro Shop.NET,

http://youtube.com/gonduda
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Hitting a brick wall.

Post 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:
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
CowboyX86
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Sat Jun 13, 2009 11:55 am

Re: Hitting a brick wall.

Post 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!
h0ts0up
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Fri Jun 05, 2009 6:17 pm

Re: Hitting a brick wall.

Post 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.
Post Reply