0

i'm making simple program to show "True" if user input 'z' and show "False" if user input anything else. However, the problem is when user input more than a character, such as when user input 'zz' the output is

True
Input : True

and when user input such as 'zs' which should be wrong, the output is

True
Input : Wrong

Here's my code

#include <iostream>

using namespace std;

int main()
{
    char input;

    cout << "Check input" << endl;

    while(true){
        cout << "Input : ";
        cin >> input;
        if(input=='z'){
            cout << "True" << endl;
        } else {
            cout << "Wrong" << endl;
        }
    }

    return 0;
}

I wonder if there are ways to prevent this without change variable type to string?

I use CodeBlocks 16.04 (MinGW) with GNU GCC Compiler on Windows 10 x64

  • What about reading a `std::string` instead of a single `char`? – user0042 Oct 23 '17 at 15:41
  • @user0042 I've tried that and success, but since my goal to input and check single character, i want use char if possible. –  Oct 23 '17 at 15:44
  • The output you provided doesn't match what the example would produce. – François Andrieux Oct 23 '17 at 15:49
  • how about using getchar instead of cin – Hariom Singh Oct 23 '17 at 15:49
  • Consider clearing `std::cin` after you read the first character. Otherwise, each character will be read one by one. It makes no difference to `std::cin` rather or not the user waits between typing characters. See https://stackoverflow.com/questions/257091/how-do-i-flush-the-cin-buffer – François Andrieux Oct 23 '17 at 15:52
  • 2
    If you read a single character, you will have read the first character of the input. You cannot possibly know if there are more characters to follow unless you try to read them as well. – n. m. could be an AI Oct 23 '17 at 15:58
  • 1
    How do you want your program to know when it has read all the input the user is going to provide? If your answer is, "I want to read in a line of input and then check if it's valid", then why not write code that does exactly that? – David Schwartz Oct 23 '17 at 17:27

2 Answers2

1

You cannot do that by reading single chars. The point is that if the user enters e.g. zz he actually did enter those two chars and these are the chars you are getting when you read from cin.

Just read a std::string as suggested and check only the first character of the string. That's just as simple that what you're doing.

So you probably want this:

#include <iostream>
#include <string>

using namespace std;    

int main()
{
  string input;

  cout << "Check input" << endl;

  while (true) {
    cout << "Input : ";
    cin >> input;
    if (input.length() > 0 && input[0] == 'z') {
      cout << "True" << endl;
    }
    else {
      cout << "Wrong" << endl;
    }
  }

  return 0;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • this still gives true in scenarios like "zz" or "zs" try to change the input.length() > 0 to input.length() == 1 – Nandee Oct 23 '17 at 16:54
0

Its definitely possible you just have to check the first character and make sure It is the only character entered than flush the buffer to get rid of the rest of the string.

code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    char input;

    cout << "Check input" << endl;

    while (true) {

        cout << "Input : ";
        cin >> input;
        //Check if the input is z and there is only 1 character inputted
        if (cin.rdbuf()->in_avail() == 1 && input == 'z') {
            cout << "True" << endl;
        }
        else {
            cout << "Wrong" << endl;
        }
        //Flush the buffer
        cin.clear();
        cin.ignore(INT_MAX, '\n');
    }

    return 0;
}
Nandee
  • 598
  • 6
  • 11