Advertisement

Strings?

Started by August 08, 2002 12:48 PM
8 comments, last by TearsKnight 22 years, 4 months ago
Ok, I know what strings are, and I know how to use them (I think..), but when it comes to if statements, I don't know how to tell the computer that when somebody types a certain thing (yes, no, etc.) to recognize the string to activate the if statement..... For instance: string szYesDrink = "yes"; string szNoDrink = "no"; cout << "Wanna drink?\n"; if (WHAT GOES HERE?!?!?) { cout << "Ok then! Let's see how good you can hold it!\n"; } if (See above) { cout << "You don't like a wee bit o' rum ev'ry now an' then?\n"; } I just don't know what the conditions are.. Cin >> "yes"? I'm so confused.... Oh, BTW, this is for a text-adventure game... So I'm guessing strings are of penultimate importance. [edited by - TearsKnight on August 8, 2002 1:52:21 PM]
string szYesDrink = "yes";string szNoDrink = "no";string response;cout << "Wanna drink?\n";cin >> response;if (response == szYesDrink){cout << "Ok then! Let''s see how good you can hold it!\n";} 


Now, an important point here: these things you''re using are STL strings. They''re a C++-specific thing. What you''ll often see used are C-style strings, which are arrays of characters terminated with a null character.

The thing that makes me assume you aren''t aware of this is that you''re naming your strings with identifiers starting "sz", which is the hungarian notation for a C-style string (string zero-terminated). You should be sure to keep the difference straight in your mind, or big problems will result.


Don''t listen to me. I''ve had too much coffee.
Advertisement
i think it it should work like that:

string input;

cin >> input;

if(input == ''y'')
cout << "drink";
else
cout << "no drink";
quote: Original post by TearsKnight
Oh, BTW, this is for a text-adventure game... So I''m guessing strings are of penultimate importance.

Are you really sure that you think strings are of second-to-last importance?

http://www.dictionary.com/search?q=penultimate
string szYesDrink = "yes";
string szNoDrink = "no";

string doYouWantToDrink;

cout << "Wanna drink?\n";
cin >> doYouWantToDrink;

if(doYouWantToDrink==szYesDrink)
{
cout << "Ok then! Let''s see how good you can hold it!\n";
}

else
{
cout << "You don''t like a wee bit o'' rum ev''ry now an'' then?\n";
}
This will only work if the user types in "Yes" or "No". You don''t need to create string constants, you just need to check the input, right? so why not just do this:

char Answer = 0;
while(Answer != ''Y'' || Answer != ''N'')
{
cout<<"Do ya wanna get plastered? (Y/N):";
cin>>Answer;
}


switch(Answer)
{
case ''Y'':
{
cout<<"Hellz ya!";
}
case ''N'':
{
cout<<"Loser. You suck.";
}
}

Just a suggestion...

quote:
Are you really sure that you think strings are of second-to-last importance?


Is that what that means? sonofabitch!

[EDIT need to learn how to spell quote]


------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
Advertisement
quote: Original post by TearsKnight
Ok, I know what strings are, and I know how to use them (I think..), but when it comes to if statements, I don''t know how to tell the computer that when somebody types a certain thing (yes, no, etc.) to recognize the string to activate the if statement.....

For instance:

string szYesDrink = "yes";
string szNoDrink = "no";

cout << "Wanna drink?\n";

if (WHAT GOES HERE?!?!?)
{
cout << "Ok then! Let''s see how good you can hold it!\n";
}

if (See above)
{
cout << "You don''t like a wee bit o'' rum ev''ry now an'' then?\n";
}

I just don''t know what the conditions are.. Cin >> "yes"? I''m so confused....

Oh, BTW, this is for a text-adventure game... So I''m guessing strings are of penultimate importance.

<SPAN CLASS=editedby>[edited by - TearsKnight on August 8, 2002 1:52:21 PM]</SPAN>


Well the easiest way to do this is with a function called _stricmp. This function takes 2 strings, converts them into lower case, and returns 0 if they are identical. This will save you from having to do if "YES" and if "yes" or if "yEs" and the like.

string drink;
cout << "Hey buddy do you want a drink?" << endl;
cin >> drink;
if( _stricmp(drink,"yes" == 0 )
{
cout << "Here''s your drink" << endl;
}

the C-style string functions won''t work with STL strings unless you use the c_str() member function. Like this:
if( strcmp(drink.c_str(),"yes") == 0 ) 



Don''t listen to me. I''ve had too much coffee.
quote: Original post by Anonymous Poster
Original post by TearsKnight
Ok, I know what strings are, and I know how to use them (I think..), but when it comes to if statements, I don''t know how to tell the computer that when somebody types a certain thing (yes, no, etc.) to recognize the string to activate the if statement…..

For instance:

string szYesDrink = "yes";
string szNoDrink = "no";

cout << "Wanna drink?\n";

if (WHAT GOES HERE?!?!?)
{
cout << "Ok then! Let''s see how good you can hold it!\n";
}

if (See above)
{
cout << "You don''t like a wee bit o'' rum ev''ry now an'' then?\n";
}

I just don''t know what the conditions are.. Cin >> "yes"? I''m so confused….

Oh, BTW, this is for a text-adventure game… So I''m guessing strings are of penultimate importance.

<SPAN CLASS=editedby>[edited by - TearsKnight on August 8, 2002 1:52:21 PM]</SPAN>


Well the easiest way to do this is with a function called _stricmp. This function takes 2 strings, converts them into lower case, and returns 0 if they are identical. This will save you from having to do if "YES" and if "yes" or if "yEs" and the like.

string drink;
cout << "Hey buddy do you want a drink?" << endl;
cin >> drink;
if( _stricmp(drink,"yes" == 0 )
{
cout << "Here''s your drink" << endl;
}



ACK! that smiley face is supposed to be a bracket like this ) stupid message board.
Well, I managed to ge t working...

char decArray[2] = {''y'', ''n''};
char response;

cout << "Need a drink?\n";

cin >> response;

if (response == decArray[0])
{
cout << "Well here you go!";
}

And so on and so forth... Can I create just two variables (''y'' and ''n'') for the entire game? Are they independent for each if branch?

This topic is closed to new replies.

Advertisement