0

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
GSerg
  • 76,472
  • 17
  • 159
  • 346
Sana J
  • 25
  • 3
  • 2
    The Code does not seem to be clear. The description says that you enter *one* letter, but the code takes a string and loop exits if the input is single letter – WARhead Apr 24 '21 at 15:01
  • Since no one has mentioned it yet, [you shouldn't be `using namespace std;`](https://stackoverflow.com/q/1452721/6296561) – Zoe Apr 24 '21 at 16:55

2 Answers2

2

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

  • 1
    should check for all because string letters = "abcdefghijklmnopqrstuvwxyz"; all are lowercase. – Sana J Apr 24 '21 at 15:58
  • 1
    i am fairly new to this language this is my first class, takes a while to learn stuff :D – Sana J Apr 24 '21 at 16:00
  • 1
    @SanaJ been there do not worry!!! To answer your first comment, you are than modifying `letterGuess` in `getline(cin, letterGuess);` and checking if any letters in input are capitalized. –  Apr 24 '21 at 16:04
  • 1
    Yeah, i think i was trying to figure out best way to check the `letterGuess` must be lowercase or it produces an error saying letter must be lowercase. but like you said it's modifying it then this is not the best way. What other ways can i check if letter entered is lowercase? Also thank you for taking your time to reply to me and the info on `srand(time(0));` i am reading it now. – Sana J Apr 24 '21 at 16:27
  • 1
    @SanaJ I see your problem now!!! when you enter incorect one character `!=1` is true. see my edit –  Apr 24 '21 at 16:36
  • 1
    Aha! so i need to loop it till the answer is correct and not stop at 1 letter entered, I see, you're the best. Let me see what i can do with the edits and guidance you provided, thank you sir! – Sana J Apr 24 '21 at 16:53
  • 1
    I used do while loop `while(letterGuess != randomLetter || isupper(letterGuess[0]));` which resolved the issue, thank you again! my entire code is working as intended! – Sana J Apr 24 '21 at 18:15
1

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?

WARhead
  • 643
  • 5
  • 17