using upper an lower case in a string varible?
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]
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".
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".
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.
#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
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!"
\_/¯\_/¯\_/¯\_/¯\_/¯\_/
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:
Hope that helps,
--hellz
#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
Her the sample code
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]
Int menu(void){std::string input;if (input == “Y”){Function ();}if (input == “n”){function2();}if (input == “life”){life();}if (input == “Fight”){fight();}elseexit(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]
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
How appropriate. You fight like a cow.
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.
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.
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.
just use
#include <iostream>int main(){char prompt;std::cin >> prompt;if (std::toupper(prompt) == ''Y''){std::cout << "Yes crap goes here";elsestd::cout << "Non-yes crap goes here";}return 0;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement