tags:

views:

122

answers:

3

Does it matter if I use a string or char for a simple input function? (aka y/n)

This is what I'm using at the moment:

using namespace std;

string somestr;

getline(cin,somestr);

if(somestr.empty())
{ //do something }
else if (somestr == "y"){
//do something else
}
else{}

And if it makes more sense to user char what would be the equivalent char code to this?

+5  A: 

Yes, it matters, because std::string cannot be compared with a char using ==. You can compare it with a string literal:

if (somestr == "y")

or you can test the initial element of the std::string:

if (somestr[0] == 'y')

In the latter case, you might want to check the length as well, otherwise you would accept such inputs as "yacht" and "yellow." Comparing with a string literal containing the expected text is probably a better choice for most use cases.

James McNellis
1.)Are you saying that in your opinion you would use string over char?2.)What would be the equivalent of somestr.empty() for char? Since that's the first check in the if statement.
Tek
@Tek: Perhaps I misunderstood your question. Do you want to replace `"y"` with `'y'` (how I interpreted your question) or do you want to replace `string somestr` with `char somechar`? If you want the latter, then as long as you only want to read a single character from the user, it's fine to use a `char`.
James McNellis
@Tek: As for how to do error checking, you should test to be sure that the extraction from the stream succeeded by testing `if (cin)`; if that test fails, then the extraction failed. You should even do this when you are calling `getline`.
James McNellis
I just wondered if there was anything wrong with using string over char. But from what I understood it's fine to use either? In which case I'd like to learn how to check when a char is empty with an if statement. If you could answer the latter that would be great because I tried using char instead but I'm failing horribly and won't compile lol.
Tek
@James McNellis: In that case I could dump .empty(), right?
Tek
@Tek: char is never "empty". If you do something like `cin >> mychar`, then the user will have to enter a non-whitespace character before the read will work. Read the comment @JamesMcNellis made on your question above for how `>>` works.
Brent Nash
If you check both (somestr[0] == 'y') and lenght of somestring, that cannot be better than testing (somestr == "y")
zvonimir
+2  A: 

I think James McNellis gives good rationale for why you would use either case. Personally, if you're asking a "yes/no" question, I find the single character easier because it minimizes the number of different scenarios you have to deal with.

Here's some sample code that you could use to read an answer from the user via a single character:

#include <iostream>
#include <limits>

using namespace std;

int main()
{
  //keep looping until the user enters something valid
  while(true)
  {
    char answer;
    cout << "Does this sound good (y/n)? ";
    cin >> answer;

    if(answer == 'y' || answer == 'Y')
    {
      //user entered yes, do some stuff and leave the loop
      cout << "You answered yes!" << endl;
      break;
    }
    else if(answer == 'n' || answer == 'N')
    {
      //user entered no, do some stuff and leave the loop
      cout << "You answered no!" << endl;
      break;
    }
    else
    {
      cout << "You did not enter a valid answer.  Please try again." << endl;

      //if we got bad input (not 'y'/'Y' or 'n'/'N'), wipe cin and try again
      cin.clear();
      cin.ignore(numeric_limits<int>::max(),'\n');
    }
  }
}

If you're planning on reading more than a single character answer though, then I think you're probably fine with getline and doing your reasoning that way.

Brent Nash
A: 

It is better to use char because you only need to store one character

using namespace std;

char chr;

getline(cin,chr);

if(chr == null) { //do something } else if (chr == "y"){ //do something else } else{}

Mehdei