-3

I'm brand new into C++. I would like to know, why my code is not working. I've got string called Response. If the response begins with y/Y it should proceed. However I would like to proceed when the user types " y"/" Y" (the white space) as well. Here is my code. Thanks in advance!

bool AskToPlayAgain()
{
    std::cout << "Would you like to play again? Yes/N";
    std::string Response = "";
    std::getline(std::cin, Response);
    return (Response[0] == 'y') || (Response[0] == 'Y' +) ||(Response[0] == ' y' +) (Response[0] == ' Y' +); 
    std::cout << std::endl;


}
  • 2
    Try to `trim` the response and then check the first character. Furthermore, your statement `std::cout << std::endl;` is unreachable – Robert Kock Oct 18 '18 at 10:12
  • `''` is used for a single char literal. You can't use it for a string like ` y`. For strings, `""` should be used. But then, you can't compare string with the first char of response. I suggest looking at [this question](https://stackoverflow.com/questions/83439/remove-spaces-from-stdstring-in-c) to remove whitespaces first and then compare your string to somethign. – Yksisarvinen Oct 18 '18 at 10:14
  • I would trim the string to remove whitespace (https://stackoverflow.com/questions/1798112/removing-leading-and-trailing-spaces-from-a-string) and use [std::tolower](https://en.cppreference.com/w/cpp/string/byte/tolower) to make the input lowercase and then perform a single check for `'y'`. – Galik Oct 18 '18 at 10:22
  • The code you've posted doesn't even compile. Try putting together a [mcve]. – Sneftel Oct 18 '18 at 10:24

5 Answers5

2

With Response[0] you access the first character of the string. You can compare that, as you do, with a character constant, such as 'y'. However, if you want to allow the leading space, this is no longer a single character, so your comparison Response[0] == ' y' cannot work.

Here is a version which allows as many space characters as needed, and then y or Y (C++11 version):

for (auto c:Response)
    if (c!=' ')
        return (c=='y') || (c=='Y');

 //string is only spaces
 return false;
Demosthenes
  • 1,515
  • 10
  • 22
1

You can traverse string Response and ignore white spaces(I am considering Tabs and Spaces in this case) while traversing.
If first character is non white space then you can break the loop I am using flag yes in this case.

bool AskToPlayAgain()
{
    std::cout << "Would you like to play again? Yes/N";
    auto Response="";
    std::getline(std::cin, Response);
    auto yes = false;
    auto itr = Response.begin();
    while(itr != Response.end())
    {
        if(*itr != ' ' && *itr !='\t')
        {
            yes = (*itr == 'y' || *itr =='Y') ;
            break;
        }
        ++itr;
    }
    return yes; 
}
Mayur
  • 2,583
  • 16
  • 28
0

Use a loop to check each of your character inside the input string...

For example, like this, usage of the usual loop which can be used in all versions of C++ (You can use the range based for loop like in the above answer...):

bool ask_to_play_again(std::string Response)
{
    for (auto i = 0; i < Response.size(); i++)
        if (!isspace(Response[i])) // Is not a whitespace character
            return tolower(Response[i]) == 'y';
    return false; // If the user enters invalid input, close the game anyway...
}

Kind regards,

Ruks.

Ruks
  • 3,886
  • 1
  • 10
  • 22
0

You can remove the spaces from the response and check with only 'Y'/'y'.

bool AskToPlayAgain()  
{  
    std::cout << "Would you like to play again? Yes/N";  
    std::string Response = "";  
    std::getline(std::cin, Response);  
    Response.erase(remove(Response.begin(), Response.end(), ' '), Response.end()); 
    return (Response[0] == 'y') || (Response[0] == 'Y'); 
}
Rishabh Agrawal
  • 122
  • 1
  • 8
0

Maybe you can use std::find function to resolve the problem.

bool AskToPlayAgain()
{
    std::cout << "Would you like to play again? Yes/N";
    std::string Response = "";
    std::getline(std::cin, Response);
    auto pos1 = Response.find("Y");
    auto pos2 = Response.find("y");
    if (pos1 != std::string::npos || pos2 != std::string::npos)
        return true;
    return false;
}
R.W.
  • 11
  • 2