Archived

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

kingpinzs

using upper an lower case in a string varible?

Recommended Posts

kingpinzs    117
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
Peon    276
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
SabreMan    504
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
hellz    356

#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
Tac-Tics    197
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
hellz    356
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
kingpinzs    117
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
Sneftel    1788
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
kingpinzs    117
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
EdR    117
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
Lektrix    106
quote:
Original post by Edward Ropple
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;
}


That''s just for one character, so no.

You can use the generic algorithm that Sneftel mentioned. Just be careful with std::toupper if you are using MSVC++; I seem to remember an old version did not place it in the std namespace, and so you wouldn''t qualify it as shown.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites
kingpinzs    117
That did not work
I need something that lets me put in more then one carecter

any sugestions?


int menuinput(void)
{
std::string pingv;
//std::string input2;

char input2;
std::cout << "C:\>" ;
cin >> input2 ;
//result = strncmp( string1, string2 , 10 );

//if( strnicmp( input2.c_str(), "fdisk" ) == 0 )

//if ( = strnicmp(""))

if (std::toupper(input2) == 'FDISK')
{
FDISK();
}


if (std::toupper (input2) == 'DEBUG')
{
debug();
}




if (std::toupper (input2) == 'FORMAT')
{
std::cout << "The type of file system is NTFS."<< std::endl << std::endl;
FORMAT();
}


if (std::toupper (input2) == 'HELP')
{
help();
}

if (std::toupper (input2) == 'PING')
{
std::cin >> pingv;
std::cout << "
pinging " << pingv << std::endl;
ping();
}

if (std::toupper (input2) == 'ipconfig')
{
ipconfig();
}

if (std::toupper (input2) == 'EXIT')
{
std::cout << "
CLOSING SIMULATION" << std::endl;
exit (1);
}
if (std::toupper (input2) == 'TIME')
{
ttime();
}
if (std::toupper (input2) == 'DATE')
{
ttime();
}
else
{

std::cout <<"
NOT A VALID COMMAND:"<<std::endl;
menuinput();
}
return 0;


[edited by - kingpinzs on September 4, 2003 12:08:07 PM]

[edited by - kingpinzs on September 4, 2003 12:13:26 PM]

Share this post


Link to post
Share on other sites
Martel    122
If you want to do it "the right way", you need to override behavior for std::string so compares are done the way you want. You will end up with a new type and might have to do extra work to get conversions and various operators to work.

Here's the idea:


#include <string>
#include <iostream>
#include <cctype>

struct case_insensitive_char_traits : public std::char_traits< char > {
static bool eq( const char& left, const char& right ) {
return std::tolower( left ) == std::tolower( right );
}

static bool lt( const char& c1, const char& c2 ) {
return std::toupper( c1 ) < std::toupper( c2 );
}

static int compare( const char* s1, const char* s2, std::size_t n ) {
for( std::size_t i = 0; i < n; ++i ) {
if( !eq( s1[ i ], s2[ i ] ) ) {
return lt( s1[ i ], s2[ i ] ) ? -1 : 1;
}
}
return 0;
}
};

typedef std::basic_string< char, case_insensitive_char_traits > CaseInsensitiveString;


void main()
{
CaseInsensitiveString a( "STRING" );
CaseInsensitiveString b( "string" );
CaseInsensitiveString c( "sTrInG" );
CaseInsensitiveString d( "str0ng" );

std::string s( "std string" );

std::cout << (a == a) << std::endl;
std::cout << (a == b) << std::endl;
std::cout << (a == c) << std::endl;
std::cout << (a == d) << std::endl;

}


[edited by - Martel on September 4, 2003 10:31:42 PM]

Share this post


Link to post
Share on other sites
EdR    117
quote:
Original post by Lektrix
That's just for one character, so no.

You can use the generic algorithm that Sneftel mentioned. Just be careful with std::toupper if you are using MSVC++; I seem to remember an old version did not place it in the std namespace, and so you wouldn't qualify it as shown.



I don't use VC++, so it might be screwed up there, but std::toupper should be able to handle type string, so that'd work perfectly fine if you change the variable from a char to a string.

Oh yeah, and one character was all he was looking for. Read the guy's question next time.

quote:
Original post by kingpinzs
I need to be able to input "Y" ot "y"



[edited by - Edward Ropple on September 4, 2003 3:48:58 PM]

Share this post


Link to post
Share on other sites
kingpinzs    117
Nothing is getting the results that I want.

I am looking on google for a c++ string converter or non case sensitive string varibles.

But I am not sure what I am looking for to find it.

Share this post


Link to post
Share on other sites
Sneftel    1788
Whoops, forgot about the ambiguity of toupper. Here ya go.


#include <string>
#include <iostream>
#include <cctype>
#include <algorithm>

int main()
{
std::cout << "Enter a string: ";

std::string myString;
std::cin >> myString;

std::toupper('a');
std::transform(myString.begin(), myString.end(), myString.begin(), (int(*)(int))std::toupper);

std::cout << "Your string in uppercase is " << myString << "." << std::endl;
if(myString == "FOOD")
{
std::cout << "It is case insensitively equal to FOOD.";
}
else
{
std::cout << "It is not case insensitively equal to FOOD.";
}
}



How appropriate. You fight like a cow.

[edited by - sneftel on September 4, 2003 6:09:08 PM]

Share this post


Link to post
Share on other sites
twix    636
Here''s a question. Do you have any idea how that code actually works, or are you just going to blindly copy it?

Share this post


Link to post
Share on other sites
Sneftel    1788
quote:
Original post by kingpinzs
I have no idea what it does. But I think it is about cctype and toupper. which I think means change to upper case.

As I said before, the cool kids take the time to learn how it works. That''s what keeps them from being crappy programmers. I suggest you do the same.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
kingpinzs    117
I read what it said but it made no sence to me what so ever. If could can you try to explain it to me. I search on google and found pages on it but they just should how to use it not how it works.

I am still looking with no avail.

Share this post


Link to post
Share on other sites
Martel    122
quote:
Original post by Edward Ropple

Oh yeah, and one character was all he was looking for. Read the guy's question next time.

quote:
Original post by kingpinzs
I need to be able to input "Y" ot "y"





Actually, he later said:

quote:

That did not work
I need something that lets me put in more then one carecter

any sugestions?




[edited by - Martel on September 4, 2003 10:33:34 PM]

Share this post


Link to post
Share on other sites