Yeah, I tested it with a string of random characters. It will still proceed like the input's all correct.
What I mean by "difference" is, why his is working while mine not. I don't know the reason within it. Brain's all messed up now ~.~
So, I should have never used char at all? The correct way is , using int?
I did, but "Intentionally empty" ? Which means the user only press enter? I kinda got lost in here about "Am I correct about it being as while the user input is not enter, do <add action yourself> "
I tried adding the do while loop like Subsonics suggested. And I first pressed enter , it does nothing. I have to press the 2nd time in order for the do while loop to comes into play.
For "while the user input is not enter, do <add action yourself> " , if I change to " while the user input is not Y or y, do <add action yourself> " , it doesn't work again...
So when you wrote the above piece of code you used getchar once (in case your input was Y313).Then this example != 'Y' && example != 'y' statement is going to be a false statement (as example holds 'Y'),so you will not enter the loop and skip it.
Hmm, so which means, you know it will be a true statement as getchar reads the first character in my case which is 'Y' and will skip the "Invalid entry" loop. So you play smart and assign 1 to flag, and while the statement is true ( which is actually false ) , it will print the error message? Damn, I like this trick ><
If EOF handling is required then you must declare your variable as int.
Else i suggest declaring it as char(if that fits you or int,there is no problem.
Haha, still have hard time understanding what is laserlight's concept in this line of code...So blur nooooooooow
Code:while((c = getchar()) != '\n') /* intentionally empty */;Code:#include <stdio.h> int main(void) { int c; while (1) { printf("Y/N? "); c = getchar(); if (c == 'Y' || c == 'y') { break; } else { /* Discard any characters left on the line. */ while ((c = getchar()) != '\n') /* intentionally empty */; } } return 0; }
It reads up to new line ! If i were you i would run it without this line in order to see why this line is required into our program
Uhh..............
Well...It prints Y/N:Y/N: _ without that line...
with that line , it prints Y/N: _
x.x
Guess I should go and get some sleep and figure it out when my brain's clear enough ==
Well the logic of his is completely different. I mean, his code sets a flag variable to true once the input goes wrong, and doesn't change it after that. Once the loop is broke, he checks the flag variable. If true, he prints the message. So to understand his example, you have to understand how getchar() works: why the input will eventually be '\n' and such.Yeah, I tested it with a string of random characters. It will still proceed like the input's all correct.
What I mean by "difference" is, why his is working while mine not. I don't know the reason within it. Brain's all messed up now ~.~
Uh, all of the work in laserlight's loop is done in the condition part of the loop. To read it properly, you should understand the part in parentheses first, then do the comparisons. Since there is nothing for the loop body to do, it is "intentionally empty". The body is terminated with a semicolon, which is valid syntax. Mind you it is not always good for the person who writes the loop to leave off the braces. This is really a rather special case.I did, but "Intentionally empty" ? Which means the user only press enter? I kinda got lost in here about "Am I correct about it being as while the user input is not enter, do <add action yourself> "
EOF handling is always required. Don't get under the impression that EOF is an optional state. Eventually you will reach the end of a file. It might be ugly, but a user could actually choose to not sit through your prompts, design a file with proper inputs, and direct the program to take input from that file. If you don't handle EOF, your code has a problem.If EOF handling is required