Sana J
Sana J

Reputation: 25

Why the code does not loop when user enter incorrect lowercase letter

I am writing a code that generates a random number then assign the number to a letter in string letters then user has to guess the letter.

I am trying to loop the the question "Enter one lowercase letter: " if the user enters a letter that does not match randomLetter, but at the same time I have to make sure the letter is all in lower case (something we haven't learned in class but after searching I found a solution, hopefully it's the right way to go about it).

This while loop ends if user enters the incorrect lower letter. It does work when the letterGuess matches randomLetter.

The other thing I have to do is if user enters an incorrect lower letter then it needs to give feedback that the correct letter comes before or after the entered letter.

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;

//function prototype
void displayNumber(int);

int main()
{
    string letters = "abcdefghijklmnopqrstuvwxyz";
    string randomLetter;
    string letterGuess = "";
    string wordLocation = " ";
    int randomNumber = 0;

    //display random number then assign a random number to letter string
    displayNumber(randomNumber);
    randomLetter = letters[randomNumber];

    while (letterGuess.length() != 1) {
        //ask user to enter a lowercase letter and determine if it matches the random letter
        cout << "Enter one lowercase letter: ";
        getline(cin, letterGuess);

        if (all_of(letterGuess.begin(), letterGuess.end(), &::isupper)) {
            cout << "letter must be lowercase.";
        }
        else if (randomLetter.find(letterGuess, 0) == -1) {
            cout << "\nYou guessed the correct letter.";
        }
        else {
            wordLocation.insert(0, letters);
        } //end if
    } //end while
    return 0;
} //end of main function

void displayNumber(int num)
{
    srand(time(0));
    num = (rand() % 26) + 1;
    cout << num << endl
         << endl;
} // end of displayNumber function

Upvotes: 0

Views: 87

Answers (2)

user5550963
user5550963

Reputation:

You your problem might be is here:

while (letterGuess.length() != 1)

because you when you enter one character you break the loop. To fix this you might go different routes, but easiest one is to check if letter is lower or upper.

while (letterGuess.length() != 1 || std::islower(letterGuess[0]))

Also now that I look at it closer, did you want to check if any characters in string are upper or all of them are upper?

all_of(letterGuess.begin(), letterGuess.end(), &::isupper)

would check if all characters are lower, but to check if only one character is upper use any_of()

any_of(letterGuess.begin(), letterGuess.end(), &::isupper)

Here is my little example code:

#include <iostream>
#include <algorithm>

using namespace std;
int main (void) {
    std::string s = "Hello";
    if (any_of(s.begin(), s.end(), &::isupper))
        std::cout << "One is upper\n\n";

    if (all_of(s.begin(), s.end(), &::isupper))
        std::cout << "All are upper\n";
    else
        std::cout << "some might be upper\n";                                                                                               

    return 0;
}

Output

EXECUTING       ==================================================

One is upper

some might be upper

DONE            ==================================================

Since you are newbie, another thing that I just noticed that I still remember from my newbie days. call srand(time(0)); once. Maybe at the begging of main(). Way you have it does produces the same random number every time. Read this

Upvotes: 2

WARhead
WARhead

Reputation: 691

The specific problems you highlighted is caused by the looping condition,

while(letterGuess.length() != 1)

here when the user in the first iteration enters just one letter, the letterGuess string will have size = 1, and hence will cause breaking of loop.

Another problem is the fact that randomNumber remains 0, for it not to remain zero the displayNumber function must take a reference or return a value.

EDIT

To update randomNumber, modify the display function to :

int displayNumber()
{
    srand(time(0));
    int num = (rand() % 26) + 1;
    cout << num << endl
         << endl;
    return num;
} 

and in the main:

randomNumber = displayNumber();

or alternatively

void displayNumber(int& num)
{
    srand(time(0));
    num = (rand() % 26) + 1;
    cout << num << endl
         << endl;
} 

and in the main:

displayNumber(randomNumber);

maybe rename it to generateRandom() which seems more accurate?

Upvotes: 1

Related Questions