Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

kingpinzs

using upper an lower case in a string varible?

This topic is 5405 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I tryed using code is in c++ std::string input; if (input == "y") { function(); } This code worked but did not give me what I wanted. and i tryed strncmp but I could not get it to work I need to be able to input "Y" ot "y" so I want to input in a capital or non capital letter without having to wright out to or if statments for the same thing. does any one have any ideas? [edited by - kingpinzs on September 2, 2003 2:26:20 PM]

Share this post


Link to post
Share on other sites
Advertisement
strncmp is kind of a weird function because I think it''s the one that returns -1 if the strings match. I also don''t know whether it would work for strings or not.

Looking at that code, you SHOULD be doing:

if (input == "y")

I have no experiences with Strings, but the single ''='' you did is a common beginner mistake; you are actually assigning a value of "y" to input, rather than checking if it IS a "y".

Share this post


Link to post
Share on other sites
quote:
Original post by kingpinzs
I tryed using
std:string input;
if input = "y"
{
//work
}
this did not work

What do you expect it to do? What language is it? If that''s supposed to be C++, then I''d expect the compiler to barf on it.

When posting questions about code, please post the exact code you tried, what you expect it to do, and what it actually did (or did not) do.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <cstring>

using std::cout;

int main()
{
char *s1 = "hellz";
char *s2 = "Hellz";

// If the strings match...
if (strcmp(s1, s2) == 0)
cout << "Strings are identical.\n";
else
cout << "Strings do not match.\n";

return 0;
}


strcmp() returns zero if the strings are identical (case-sensitive). Taken from this reference, is the following:

"The sign of a non-zero return value shall be determined by the sign of the difference between the values of the first pair of bytes (both interpreted as type unsigned char) that differ in the strings being compared."

--hellz

Share this post


Link to post
Share on other sites
quote:

std:string input;
if input = "y"
{
//work
}



Yeah, what SaberMan said. This isn''t correct C syntax. It''s not even psuedo code! `input` is never defined, explicitly or implied.

quote:

strncmp is kind of a weird function because I think it''s the one that returns -1 if the strings match. I also don''t know whether it would work for strings or not.



strncmp does not work with std:strings, it works with char*''s. Also, the strncmp function''s return value is not weird. It returns 0 if they are equal (ie: zero difference) and 1 or -1 depending on which string is greater.

kingpinzs, be more specific when asking your questions or you won''t get helpful replies.

/¯\_/¯\_/¯\_/¯\_/¯\_/¯\
"You TK''ed my chicken!"
\_/¯\_/¯\_/¯\_/¯\_/¯\_/

Share this post


Link to post
Share on other sites
Ah, just noticed you''re using std::string (although you''ve used one semi-colon, which is wrong). So instead, here''s a sample program to demonstrate how to compare strings:


#include <iostream>
#include <string>

using std::cout;
using std::string;

int main()
{
string s1 = "hellz";
string s2 = "Hellz";

// If the strings match...
if (s1 == s2)
cout << "Strings are identical.\n";
else
cout << "Strings do not match.\n";

return 0;
}


Hope that helps,

--hellz

Share this post


Link to post
Share on other sites
Her the sample code

Int menu(void)
{
std::string input;

if (input == “Y”)
{
Function ();
}
if (input == “n”)
{
function2();
}
if (input == “life”)
{
life();
}
if (input == “Fight”)
{
fight();
}
else
exit(1);
}

I want to be able to use captial or non captial letters for input
Or is there a better way to do thsi?

[edited by - kingpinzs on September 2, 2003 2:43:38 PM]

Share this post


Link to post
Share on other sites
convert the string to uppercase, then test against the uppercase version of the string. That way, case won''t matter.

BTW, all the cool kids use std::transform(mystring.begin(), mystring.end(), mystring.begin(), std::toupper);, and they also take the time to learn about it.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
So let me see if I understand On what you said Sneftel.
I take the input change it to caps then see if it works with what I have and if no then exit.
I could do that.
You also said to you std::transform

Ok let me see if I can figure out how to use it.

Share this post


Link to post
Share on other sites
just use



#include <iostream>

int main()
{
char prompt;

std::cin >> prompt;
if (std::toupper(prompt) == ''Y'')
{
std::cout << "Yes crap goes here";
else
std::cout << "Non-yes crap goes here";
}

return 0;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!