Press enter to continue after yes/no in C - c

New programmer here with only some minor Java experience trying my hand at writing something in C. I want to ask someone a Yes/No question, do something depending on their answer, then ask them to press Enter to continue. I'm having two problems:
1.) I can't get the program to accept 'y', 'Y', or "Yes" as answers. I can get it to accept one, but not all three. The "logical OR" operator || isn't working.
2.) I can't get it to stop at "Press Enter to Continue" without two "Flush" commands of:
while (getchar() != '\n');
The code I have and am trying to use is as follows:
int main (int argc, const char * argv[]) {
printf("Would you like to continue? Please press y or n.\n");
if(getchar() == 'y'){
printf("You pressed yes! Continuing...");
}
else{
printf("Pressed no instead of yes.");
}
//flush commands go here
printf("\nPress ENTER to continue...");
if(getchar()=='\n'){
printf("\nGood work!");
}else{
printf("Didn't hit ENTER...");
return 0;
}
Any help would be appreciated, thanks.

Assuming that you are working in *nix environment,
You can create a buffer to store the incoming characters one after the other.
You have two cases:
1. Single character input
2. 3 character String
For all other cases you can blindly say that the input is not OK!
For case 1, i should be 1 and the character should be 'y' or 'Y'
For case 2, i should be 3 and the string should be 'Yes'
Any other case is incorrect. Here is the code:
#include<stdio.h>
int main()
{
char ch[3];
char c;
int i=0;
while(((c=getchar())!='\n')){
ch[i]=c;
i++;
}
ch[i]='\0';
if (i==1)
if (ch[0]=='Y'||ch[0]=='y')
printf("OK");
else
printf("Not OK");
else if(i==3)
if (strcmp(ch,"Yes")==0)
printf("OK");
else
printf("Not OK");
else
printf("NOT OK");
return 0;
}
I would recommend using something like this.

First off you might like to save the result of the first getchar() to test each possible value
eg
int c=getchar();
if(c=='y' || c=='Y')
....
The reason the "enter" part skips for the second test is because when you type 'y' or 'n' you press enter after to send your input - the \n is still in the buffer and it pulled by the next call to getchar()

Related

Implementing getch() functionality by using either fgets() or scanf()

I am trying to write a program in C where I intend to control the execution of a while loop according to user input in stdin. I am able to write the program in three different ways as following which perform as intended:
Using scanf() function
#include<stdio.h>
#include<string.h>
int main()
{
char loop= 'y';
while(loop=='y')
{
printf("Do you want to continue? [y|n]: ");
scanf("%c", &loop);
while(getchar() != '\n');
if(loop != 'y' && loop != 'n')
{
printf("Error! Please enter 'y' or 'n' \n");
loop = 'y';
}
}
return 0;
}
Using fgets() function
#include<stdio.h>
#include<string.h>
int main()
{
char loop[5]= "yes\n";
printf("%s",loop);
while(strcmp(loop,"yes\n")==0)
{
printf("Do you want to continue? [yes|no]: ");
if(strcmp(loop,"yes\n")!=0 && strcmp(loop,"no\n")!=0)
printf("Error! Please Enter 'yes' or 'no'\n");
else
fgets(loop,5,stdin);
}
return 0;
}
Using getch() from conio.h
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
char loop= 'y';
while(loop=='y')
{
printf("Do you want to continue? [y|n]: ");
loop = getch();
printf("\n");
if(loop != 'y' && loop != 'n')
{
printf("Error! Please enter 'y' or 'n' \n");
loop = 'y';
}
}
return 0;
}
Although all the aforementioned methods perform the task of stopping or continuing the while loop according to user input, the getch() is little different. Using getch(), the user does not have to press the enter after the user inputs y or n and the program terminates or continues as soon as the user provides input.
I am aware that conio.h is not included in the standard gcc library and is frowned upon to use. However, I would like to implement the functionality of getch() using either scanf() or fgets() or any other standard library function. By functionality of getch(), I mean that as soon as a user press the desired key on terminal, the while loop should terminate without needing to press the enter by the user.
Is it possible for either fgets() or scanf() to do this task?

C getchar() doesn't wait for input/ conditional loop doesn't int

I am trying to add a feature to my C console application calculator that prompts the user to decide whether they want to perform another calculation using: y or n, but in testing, getchar() refuses to wait for input and the program proceeds as though it has received valid input. The following is a minimal example of the feature:
main()
{
char newCalculation;
do{
lengthFormula(); /* main calculation formula */
printf("Would you like to do another calculation? (Y/N)");
newCalculation = getchar();
}while(tolower( newCalculation ) == 'y');
if(tolower(newCalculation) == 'n'){
exitProgram(); /* exit the program */
}
while(tolower(newCalculation) != 'n' && tolower(newCalculation) != 'y'){
printf("This is not a valid response.\n Please enter \"Y\"
if you want to do another calculation,
or enter \"N\" to exit.\n");
newCalculation = getchar();
}
return 0;
}
When I run this, the program does not wait for input after:
Would you like to do another calculation? (Y/N)
, but instead proceeds as though it has received invalid input. The result is that it spits out the prompt and the invalid input notice one after the other without a space:
Would you like to do another calculation? (Y/N)
This is not a valid response.
Please enter \"Y\" if you want to do another calculation, or enter \"N\" to exit.
If I enter a "y" after this, main() returns 0 and the program terminates.
Is someone able to see where I went wrong here?
Why won't the console wait for input at getchar()?
Why does valid input terminate the program after the first invalid response?
P.S.: Please don't tell me to "read a book" or shoo me away to Dennis Ritchie or one of the previous SO discussions on input. I've been poring over Richie's discussion of I/O, as well as similar texts from Lynda.com and Wiley, and none of the previous "it won't wait for input" posts addresses my issue as far as I can tell.
#simplicisveritatis Here is the modification of your code that I tried. Still have the same getchar issues.
int main(void)
{
/* local variable declaration */
char newCalculation = 'y';
/* main function */
/*if(tolower( newCalculation ) == 'y')
{
lengthFormula(newCalculation);
}*/
do
{
lengthFormula();
printf("Would you like to do another calculation? (Y/N)");
newCalculation = getchar();
if( tolower( newCalculation ) == 'n' )
{
exitProgram();
}
while( tolower( newCalculation ) != 'n' && tolower( newCalculation ) != 'y' )
{
printf("This is not a valid response.\n Please enter \"Y\" if you want to do another calculation, or enter \"N\" to exit.\n");
newCalculation = getchar();
}
}while( tolower( newCalculation ) == 'y' );
return 0;
}
Your code has a lot of problems:
main should be:
int main(void){return 0;}
You need to cast getchar (read about getchar) and should be:
newCalculation = (char)getchar();
Your approach on do{}while; + while{} is also wrong used.
Try the following:
#include<stdio.h>
#include<stdlib.h>
int main(void){
int validate;
char menu_choice;
validate = 0;
do{
printf("Would you like another go?(y/n):\t" );
if(scanf(" %c", &menu_choice ) == 1){
if((menu_choice=='y') || (menu_choice=='Y')){
printf("You choosed Yes\n\n\n");
validate = 1;
}else if((menu_choice=='n') || (menu_choice=='N')){
printf("You choosed No\n\n\n");
validate = 2;
}else{
printf("Wrong Input.\n\n\n");
validate = 0;
}
}
}while( validate == 0 || validate == 1);
printf("Goodbye\n");
return 0;
}
Include your three cases: exit condition, wrong input and calculate within the while loop:
main(){
do{
printf("Would you like to do another calculation? (Y/N)");
// get input
char newCalculation;
newCalculation = getchar();
// exit condition
if(tolower(newCalculation) == 'n'){
exitProgram(); /* exit the program */
}
// wrong input condition
else if(tolower(newCalculation) != 'n' && tolower(newCalculation) != 'y'){
printf("This is not a valid response.\n Please enter \"Y\"
if you want to do another calculation,
or enter \"N\" to exit.\n");
// you should clear the input stream from the wrong input
}
else{
// calculate
lengthFormula();
}
}while(tolower(newCalculation) == 'y');
return 0;
}
Why won't the console wait for input at getchar()?
Most probably, your function lengthFormula() reads input (e. g. by using scanf() or whatever), but doesn't read the line ending character \n from the input buffer. Then after returning from lengthFormula(), the getchar() has to read remaining content from the input buffer rather than requesting fresh input.
Why does valid input terminate the program after the first invalid
response?
That's because your
while(tolower(newCalculation) != 'n' && tolower(newCalculation) != 'y')
does the same after a response of y as after a response of n - it leaves the loop and gets to the following
return 0;

The program control flow doesn't work as expected

This is a problem in C. The program Control flow is not as expected. It ask to enter the character in but fail to ask to enter character x.
int foo();
int main(int argc, const char * argv[]) {
foo();
return 0;
}
int foo(){
char in;
char x;
printf("Do you wanna party \n");
if((in = getchar()) == 'y')
printf("Go Sleep!, I was kidding\n");
else
printf("Oh! you are so boaring..\n");
printf("\nOk, Another Question\n");
printf("Wanna Go to Sleep\n");
if((x = getchar()) == 'y')
printf("ok lets go, Sleepy Head\n");
else
printf("No, lets go\n");
return 0;
}
To clarify the comments mentioned above, in the process of giving input, you're pressing Y and then pressing ENTER. So, the y is considered as the input to first getchar(), and the ENTER key press [\n] is stored in the input buffer.
On the call to next getchar(), the \n is read, which is considered a perfectly valid input for getchar() and hence your code is not waiting for the next input.

Unexpected call of printf

This is the first question I've posted about C programming on here as I just started learning C just a few weeks ago. Ill write up my code and ask what my problem is :) If Anyone please knows how I can fix my mistake or whatever I should replace for my code please reply:)!
The problem I am having, is that if you run the code for yourself, you will see that everything works fine, except for the 'else' part in the statement. The issue I am having is that when someone types more than one letter, it will run the last printf statement more than once, and will printf as many times as the user inputs a character other than y or n.
The first part with the Y or N is working fine, yet if they type any number of other chars, it doesnt just state "Please select again", one time and then re-scanf, it types out at least 2 printfs, just for even one character entered, "Please select again" "Please select again", and then, if you type more chars for the answer, it will just type even more "please select again"'s.
Please help me understand what I am doing wrong as I'm so keen on learning to program properly, but I am just stuck here atm :)
#include <stdio.h>
#include <conio.h>
int main()
{
char answer;
int loop = 0;
printf("Please select. [Y/N]:\n");
while (loop == 0)
{
scanf("%c", &answer);
if (answer == 'y' || answer == 'Y')
{
printf("Seeyou Later Aligator.\n");
break;
return 0;
}
else if (answer == 'n' || answer == 'N')
{
printf("Mmkay.\n");
break;
return 0;
}
else
{
printf("Please select again [Y/N]:\n");
loop = 0;
}
}
getch();
return 0;
}
scanf reads the required number of characters each time. If there are more characters, they are not ignored. They are read next time you call scanf. Hence you see multiple prints for every character. Inorder to explicitly ignore pending input, call fflush(stdin) after scanf. Which means to flush out any data in standard input stream.
Update:
fflush should not be used on input streams as said in comments. Use the accepted solution for ignoring output. However I recommend using toupper or tolower instead of bit hack.
The reason as many have pointed out is that your scanf is reading the extra newline character left in the input buffer after the user presses ENTER. So here is an alternative way to read input to avoid that whole mess:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char answer;
printf("Please select. [Y/N]:\n");
while (1)
{
scanf("%1s%*[^\n]", &answer);
answer |= 0x20;
if (answer == 'y')
{
puts("Seeyou Later Aligator.");
break;
}
else if (answer == 'n')
{
puts("Mmkay.");
break;
}
else
{
puts("Please select again [Y/N]:");
}
}
getchar();
return 0;
}
This will read just the first character found on stdin and ignore everything else after that and at the same time clear the input buffer of the newline character
break; is enough ... return will never be executed as you will break out of the while
Its printing more than once because scanf is taking in '\n' and extra inputs from previous entry
also the variable loop is pointless in your code
here is the fixed code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char answer;
int loop = 0;
printf("Please select. [Y/N]:\n");
while (1)
{
scanf("%c", &answer);
if (answer == 'y' || answer == 'Y')
{
printf("Seeyou Later Aligator.\n");
break;
//return 0;
}
else if (answer == 'n' || answer == 'N')
{
printf("Mmkay.\n");
break;
// return 0;
}
else
{
printf("Please select again [Y/N]:\n");
while(getchar()!='\n'){
getchar();
if(getchar() == '\n'){
break;
}
}
}
}
getchar();
return 0;
}
Output:
$ ./test
Please select. [Y/N]:
dddd
Please select again [Y/N]:
ffffff
Please select again [Y/N]:
y
Seeyou Later Aligator.

reading user command to continue does not work

I'm Writing a program for Billing System. I'm using do-while loop in my program. And the program is executed according to user input. If the user want to continue the execution, the program will be continue. But I Got a prob in Execution. I was trying my logic in simple do-while loop. The same Problem arises for simple do-while loop also.
Problem is: If the input is yes, the program does not get the further input from user.
That simple do-while loop is:
#include <stdio.h>
main()
{
int c;
char ch;
do
{
printf("enter the no less then 4:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("In 1\n");
break;
case 2:
printf("In 2\n");
break;
case 3:
printf("In 3\n");
break;
}
printf("do u want to continue?:");
ch=getchar();
}while(ch=='y');
}
If i put while(ch != 'n') instead of while(ch=='y') the program working fine. I couldn't understand the problem behind this. Please Help me to rectify this. And Explain about this problem.Thank u in advance.
first run, 3 is printed, user types "y" and presses return
getchar() reads 'y' and program loops
second time, getchar() reads newline character from the previous key press
newline is not 'y' so program does not loop
Several problems:
getchar returns an int, not a char, so ch must be an int just like c.
scanf needs a pointer to go with the %d, so it should be scanf("%d", &c);
The while should rather test for EOF, as in while ((ch = getchar()) != EOF)
Note that the input will contain the newlines, which you should deal with (e.g. ignore).
This should be quite robust:
#include <stdio.h>
int main(void)
{
int c, ch;
for (;;) {
printf ("Enter a number (1, 2 or 3):");
fflush (stdout);
if (scanf ("%d", &c) == 1) {
switch (c) {
case 1:
printf ("In 1\n");
break;
case 2:
printf ("In 2\n");
break;
case 3:
printf ("In 3\n");
break;
}
printf ("Do you want to continue? [y/n]:");
fflush (stdout);
while ((ch = getchar ())) {
if (ch == 'y')
break;
else if (ch == 'n' || ch == EOF)
return 0;
}
} else {
printf ("That was not a number. Exiting.\n");
return 0;
}
}
}
While(ch=='y') or the character whatever in while() it will sent to case 3 as per your coding....y is pressing ,it wil sent to case 3 otherwise it wont work
Instead of reading the answer using getchar, use fgets.
As others explained, the second getchar call gives you the newline, which was typed after the first y.
With fgets, you'll get everything the user typed. Then you can check if it's y (just check the first character, or use strcmp).

Resources