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:
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.
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).
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.
// 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(constchar *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. =)
@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.