Yahoo Clever wird am 4. Mai 2021 (Eastern Time, Zeitzone US-Ostküste) eingestellt. Ab dem 20. April 2021 (Eastern Time) ist die Website von Yahoo Clever nur noch im reinen Lesemodus verfügbar. Andere Yahoo Produkte oder Dienste oder Ihr Yahoo Account sind von diesen Änderungen nicht betroffen. Auf dieser Hilfeseite finden Sie weitere Informationen zur Einstellung von Yahoo Clever und dazu, wie Sie Ihre Daten herunterladen.

C++ Programming toupper() and tolower() strings help?

So i'm a little new to programing and i', doing a small hangman program but i'm having trouble figuring out how to ignore the user inputs case sensitivity.... If that makes sense?

For an example, if the word they're trying to solve is 'American Airlines' , the user enters the letter a, it will only count the a next to c and n.

However, if they enter A, then it will only count for the first letter A in both words.

How can I program it to where if the user inputs 'a', it recognizes both capitalized and lowercase letters in the word?

I know i have to use the toupper() and tolower() functions but i'm not really sure where to put them or code them. (still a noob).

This is what i have so far:

cout << "Phrase to guess: ";

cout << GuessWord << endl;

cout << "Enter a letter: ";

cin >> Letter;

Location = SecretWord.find(Letter,0);

if (Location > SecretWord.size())

BadGuesses++;

else

while (Location < SecretWord.size())

{

GuessWord.replace(Location,1,Letter);

char a='A';

a=toupper(a);

Location = SecretWord.find(Letter, Location + 1);

}

4 Antworten

Relevanz
  • cpcii
    Lv 7
    vor 4 Jahren

    Try finding the ASCII value of the letter and if it falls into the range for upper case, add 32 to it and convert it back to a letter and test your table.

    http://www.asciitable.com/

    Use your text to find out how to cast a char (hint hint).

  • cja
    Lv 7
    vor 4 Jahren

    For the phrase to guess, change all letters to lowercase:

    void toLower(string &s) {

    . . string::iterator i = s.begin();

    . . while (i != s.end()) {

    . . . . *i = tolower(*i);

    . . . . ++i;

    . . }

    }

    ... and also use tlower() to convert characters as they're entered. Of course, you could go all uppercase as well.

    You can also use for_each, from <algorithm>:

    void chToLower(char &c) {

    . . c = tolower(c);

    }

    string toLower(const string &s0) {

    . . string s1 = s0;

    . . for_each(s1.begin(), s1.end(), chToLower);

    . . return s1;

    }

  • ?
    Lv 7
    vor 4 Jahren

    easy:

    #include <cctype>

    char Letter; //be sure that Letter is a single character NOT a string

    ...

    cout << "Enter a letter: ";

    cin >> Letter;

    Letter=toupper(Letter);

  • vor 4 Jahren

    make all your puzzle answers in uppercase.

    Then set your Letter Variable = letter.toupper().

    then find your location.

Haben Sie noch Fragen? Jetzt beantworten lassen.