Don't want user to enter a char

I'm using a while() set up to run as long as my bool is set to 1. When its set to 0, it breaks, just as its supposed to.

Only problem is if the user enter a letter, it breaks the loop.
Why can it not be this simple?:

1
2
3
4
    if(cin == char)
    {
        cout << "FAT ERROR! Try again.";
    }


How do I solve this?

Thanks in advance!
Last edited on
closed account (zb0S216C)
Krusing wrote:
"Why can it not be this simple?:"

Because cin == char doesn't make sense, since cin is an object of a class, and char is a type. How would the compiler even begin to compare the two?

To check whether or not a user entered a character, your best bet would be to use the isalpha() function. This function returns a Boolean expression indicating whether or not the character you gave it is within the alphabet. For instance:

1
2
3
4
5
6
7
char input = '\0';
std::cin >> input;

if(isalpha(input))
    // 'input' contains a letter.

else // 'input' contains something else. 

I'll let you figure out how to incorporate it into your program.

Wazzak
I did manage to make the if-statement run if my input contains a letter. But now there is a problem with my while()-loop.

The while is set up like this: while(igen), and "igen" was a bool making the loop run as long as it's set to true.

When the program executed everything else in my loop, its supposed to ask
Continue?
1 : YES
0 : NO

The input is the new value of my bool "igen".

I changed the bool to the char igen = '\0' you posted, but when i use '\0' the program terminateted. I changed it to '\1', and now I can not break my loop....?

Thank you for pointing me in the right direction. It is much more fun than a sulotion.
Can you post your code ?
I'm having a hard time understanding what you're trying to accomplish. Why did you change igen to a character? Framework was telling you to use the input variable as a character, not your test variable.

Aside from the information framework offered, there is a way to use integers to make sure the user doesn't enter a character and crash the standard input stream (wreaks havoc on while loops). Simply test the flags set by the input stream. If the user enters something out of bounds (enters a character when asking for a number), it sets a failbit flag. You can check the flag during each loop (make sure they didn't enter a number), clear the flag, and try again (typically used with continue).

If you'd like an example, let me know.
use the traditional way: use string, check whether any of its data is equal to char

CMIIW (if i'm wrong, then, sorry for being a smart*ss)
I'm far from finished with my program, so it might look a bit wierd, but I guess you can see where the problem is?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
using namespace std;

class Film
{
    public:
        string titel;
        string typ;
        Film(string titel, string typ);
};

Film::Film(string titel="", string typ="")
{
    this->titel=titel;
    this->typ=typ;
}

int main()
{
    setlocale(LC_ALL, "swedish");

    bool igen('\1');
    Film nyFilm;

    while(igen)
    {

    getline(cin, nyFilm.titel);

    cout << "Fortsätt? \n 1: JA \n 0: NEJ" << endl;
    cin >> igen;

    if(isalpha(igen))
    {
        cout << "FAT ERROR! \n \n";
    }
        cin.fail();         //Kolla om fail bit uppstår
        cin.clear();        //Återställ fail bit
    }
}


And if you can see more problems, please let me know =)

Volatile Pulse

I would love to see an example. This was much more tricky than I thought...

Thank you all! =)
I had misunderstood what you were trying to accomplish since you're making it more difficult than it needs to be. I typed up a quick example for a simple loop that should suffice:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

int main() {
   std::string movieTitle;
   char continueLoop = 'Y';

   while (continueLoop != 'N' && continueLoop != 'n') {
      std::cout << "Please enter the Title: ";
      getline(std::cin, movieTitle);

      std::cout << "Would you like to enter another? (Y/N) ";
      std::cin >> continueLoop;
      std::cin.ignore(80, '\n');
   }

   return 0;
}


The loop only stops if the user enters 'n' or 'N', so it will continue even if they enter a number, any letter other than n, and even special characters.

I was assuming you were accepting a value and when the user enters any letter, it would break the standard input.
closed account (zb0S216C)
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// This macro simply checks a single character to see if it's
// a 0, 1, 2, 3, ..., or 9.
#define is_number(a) (((a) >= '0') && ((a) <= '9'))

bool input_is_number(const char *str)
{
    // While the character pointed-to by 'str' is not a
    // null-character...
    while(*str)
    {
        // ...check if the current character is a number. If it's
        // not, then it's not a number; return false.
        if(!is_number(*str))
            return(false);

        // Otherwise, move onto the next character.
        ++str;
    }

    // All of the characters were digits, so it's obviously
    // a number.
    return(true);
}

int main()
{ 
    // Get a string from the user.
    std::string input;
    std::cin >> input;

    // Is it a number?
    if(!input_is_number(input.c_str()))
        std::cout << "Error: That's not a number\n";

    else std::cout << "Well Done!\n";

    return(0);
}

Krusing wrote:
"Now I just have to figure it out, what it is and how to explain it to my self. =)

I'll annotate it for you.

Wazzak
Last edited on
That's a lot of new, unknown stuff. But it seems very useful, thanks a lot Framework!

Now I just have to figure it out, what it is and how to explain it to my self. =)

Thank you!
@Framework
Unless I missed something, I believe the OP was trying to have the user enter a string for the DVD titles and then respond to a Yes or No question to determine if the program should continue or not. I don't believe you code, albeit good, applies to the OP's situation.

I was going to head down the road of checking the input stream to make sure the user didn't enter a character when you were asking for a number, but I see that you're not technically asking for a number, but a 1 or 0 for true or false. The issue that goes into handling that isn't necessarily worth all of the extra code, especially for such a simple program. Frameworks example is actually really good, and saves from having to use a separate header file. You can modify it to fit your program accordingly and just tell the user to enter -1 or something for the string and have that terminate your loop.

The beauty of C++, one desired outcome, infinite possible ways to get there.
Did I really just see you define a MACRO function ._.?
closed account (zb0S216C)
Yes, you did. Not all macros are evil, you know.

Wazzak
Last edited on
Topic archived. No new replies allowed.