well I have been trying to write this program which accepts any number of integers until the condition is true using do while loop but the loop gets terminated even if the condition is met.I am not able to understand the flaw in my code... I would be thankful for any help
#include<stdio.h>
#include<conio.h>
int main()
{
int max,min,test;
char next;
printf("enter an integer:\n");
scanf("%d",&max);
min=max;
do
{
printf("enter next integer:\n");
scanf("%d",&test);
if(test>max)
max=test;
else if(test<min)
min=test;
printf("want to enter next number y/n\n");
scanf("%c",&next);
}while(next=='y');
printf("Maximum=%d\nMinimum=%d\nRange=%d\n",max,min,max-min);
getch();
}
Try this ...
#include<stdio.h>
#include<conio.h>
int main()
{
int max,min,test;
char next;
printf("enter an integer:\n");
scanf("%d",&max);
min=max;
do
{
printf("enter next integer:\n");
scanf("%d",&test);
getchar(); // added this
if(test>max)
max=test;
else if(test<min)
min=test;
printf("want to enter next number y/n\n");
scanf("%c",&next);
}while(next=='y');
printf("Maximum=%d\nMinimum=%d\nRange=%d\n",max,min,max-min);
getch();
}
After taking input test when you are pressing Enter your code takes Enter as a new line character. To avoid this, take that new line character through getchar().
When reading input through scanf(), It will not consume return key so '\n' will remain there in stdin waiting to read for next char input. That is why your program is coming out of loop. So to avoid this problem use getchar() after scanf("%d",&test);.
in else if (condition) you have written test<min, where variable min does not have any value. you should write
else if(test<max)
(
min = test
)
Related
I am trying to make a simple code that will read a char from input and execute "Correct" or "Incorrect input" and run the code again until the correct input is entered. First of all it does not work for capital X. The other issue that I want to fix is that after the incorrect input I have to press enter to get the "Enter x" message, instead of getting in immediately after the incorrect input message.
#include <stdio.h>
int main()
{
do
{
printf("Enter x\n");
if (getchar()=='x'|| getchar()=='X')
{
printf("Entered char is X\n");
return 0;
}
else
{
printf("Input incorrect! Please try again!!!\n");
}
}
while (getchar()!='x' || getchar()!='X');
return 0;
}
You need to store the input in a variable, otherwise you keep asking for input several times in a row, for each getchar call.
For weird historic reasons, getchar actually returns an int, since the value EOF that can be returned from it is an int. So the variable must be int.
And finally, each time the user hits enter, a invisible line feed character \n is appended to the input stream. This character does you no good, so you should discard it with an extra read.
#include <stdio.h>
int main (void)
{
int input;
do
{
printf("Enter x\n");
input = getchar();
getchar(); // extra getchar to chew up line feed from stdin
if (input=='x'|| input=='X')
{
printf("Entered char is X\n");
}
else
{
printf("Input incorrect! Please try again!!!\n");
}
} while (input!='x' && input!='X');
return 0;
}
Please note that the opposite of input=='x'|| input=='X' is input!='x' && input!='X' (De Morgan's laws). "If input is not 'x' and input is not 'X' then loop".
When you hit the ENTER key the newline character \n is placed in input buffer. You need to consume that newline character in order to read the next character.
Also you are reading two time, which is unnecessary in this case. So your code should be like this
#include <stdio.h>
int main()
{
char inp;
do
{
printf("Enter x\n");
inp = getchar();
getchar(); // reading the newline character '\n'
if (inp == 'x'|| inp =='X')
{
printf("Entered char is X\n");
return 0;
}
else
{
printf("Input incorrect! Please try again!!!\n");
}
}
while (inp !='x' || inp !='X');
return 0;
}
p.s There is no need to put condition checking in while loop, since you are returning in if condition. while(true) would work fine. Thanks #bruno for pointing that out.
In your code:
if (getchar()=='x'|| getchar()=='X')
getchar() is called twice.
Instead, you should write it this way:
char c = getchar();
if (c=='x'|| c=='X')
for the second part, if your goal is print the message on a new line, then just simply change your printf to:
printf("\nInput incorrect! Please try again!!!\n");
Here, user is asked to press respective key to perform particular function, but suppose I pressed any character value such as "g", it goes into an infinite loop.
How to solve this issue?
int item,choice;
clrscr();
while(1)
{
printf("QUEUE SIMULAtOR");
printf("\n1:Insert");
printf("\n2:Delete");
printf("\n3:Display");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:qinsert();
break;
case 2:item=qdelete();
if(item!=-1)
printf("Deleted item is %d",item);
break;
case 3:printf("\nElements in the queue are:");
qdisplay();
break;
case 4:exit(0);
default:printf("\nWrong choice try again:");
}
}
Add this after the scanf()
char ch;
while( ( ch = getchar() ) != '\n' && ch != EOF );
This should do the trick.
The problem was caused as scanf() does not store characters ( since you are using %d ) and thus they remain in the input buffer. The next scanf() tries to read this and again, instead of storing it, ignores it and it remains in the buffer. This process repeats, which causes the infinite loop.
The code I gave reads all the characters in the buffer, thus reomoving the infinite loop.
Cheers... Hope this helps you :)
This is a really awesome question.You must try to use a break statement inside a if().
Example:
void main()
{
int n;
printf("Enter the number 5\n");
while(1)
{
scanf("%d",n);
if(n==5)
{ break;
}
else
{
printf(" enter the correct num again \n");
}
}
printf(" you've entered the right number\n");
}
There you go this program will run until u enter the number 5
If you want any integer
you could use isdigit() function which is under the header file ctype
I'm just a beginner and am trying to make a a program that asks for a number and if a letter is input, it says "that's not a number" and asks for a number again, until a number is input.
However, my program keeps going into an infinite loop with the current code. Any help would be appreciated to fix this. Also, I would also like the program to say "please input something" if nothing is input, but don't know how to do this. Thanks.
#include <stdio.h>
int main()
{
float i;
printf("enter a number");
while(scanf("%f", &i) != 1)
{
puts("That is not a number.");
scanf("%f", &i);
}
}
You need to clear the bad input from stdin after your scanf fails:
#include <stdio.h>
int main()
{
float i;
char trash[1024];
while (1)
{
printf("Please enter a number: ");
fflush(stdout);
if (1 == scanf("%f", &i))
break;
/* scanf failed: clear the bad input from stdin */
if (NULL == fgets(trash, sizeof(trash), stdin)) /* NOTE: assumes 1 entry per line and no line longer than 1023 characters */
exit((fprintf(stderr, "Unexpected EOF or error!\n"), 1));
puts("That is not a number.");
}
printf("You entered: %f!\n", i);
return 0;
}
As an alternative to the fgets() to clear the line, you could call scanf("%1023s", trash), which would only suck in the next whitespace delimited series of characters. This would allow you to handle multiple entries on a single line with mistakes intermixed, for example.
Your program goes into infinite loop because after the invalid input, (scanf("%d", &i) != 1)condition being TRUE, the invalid input which is left in the input buffer is not consumed, it's still in the buffer. So the same invalid input is read over and over again.
To avoid, once scanf() fails, you need to flush out all the input buffer contains before calling next scanf().
Maybe inside the while loop, calling getchar() until a newline or EOF will help. Also, the second scanf() can be removed, IMHO.
After your non numeric[More precisely input which doesn't match the formating of scanf()] input you need to clear the stdin. If not the same input will be read till stdin get cleaned or you terminate the program. A reference answer can be found on this question
Quoted
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
Reason for infinite loop : Since you don't clear stdin, same values will be read by scanf() and always full fill while condition resulting in a infinite loop.
Use following edited code :
#include <stdio.h>
int main()
{
float i;
char c;
printf("enter a number");
while(scanf("%f", &i) != 1)
{
puts("That is not a number.");
scanf("%f", &i);
while ((c = getchar()) != '\n' && c != EOF); // Flush stdin
}
}
Your code is taking input of a number. Thats why if you even give input a letter, it will take the ASCII value of the letter, which is a valid number. And Further more please take a character as an input. Here I have modified your code which should work
#include <stdio.h>
int main()
{
char i;
printf("enter a number");
while(1)
{
scanf("%c",&i);
if (c >=48 && c <= 57) // here ascii value of numbers between 0-9 is 48-57 respectively
{
puts("That is a number.");
break;
}
else
{
puts("That is not a number.");
scanf("%c",&i);
}
}
}
The problem is when it asks the user to enter the name of the band.The program bypass the first fgets, but executes the printf and not the fgets. I tried to handle this problem with if but again the same problem. Bypass the first fgets. I provide you an image also.
#include <stdio.h>
#include <stdlib.h>
struct cd
{
char band[100];
};
struct cd *music;
int main()
{
int n,i;
printf("Give how many albums you want: ");
scanf("%d",&n);
struct cd *music = malloc(sizeof(struct cd)*n);
for(i=0;i<n;i++)
{
printf("\nEnter Band: ");
fgets(music->band,sizeof(music->band),stdin);
music++;
}
for(i=n-1;i>=0;i--)
music--;
for(i=0;i<n;i++)
{
printf("\nBand: %s",music->band);
music++;
}
printf("\n\n");
return 0;
}
Scanf() treats '\n' as a character and since it is not in the format string, it leaves it there.
try this
char newline;
int n,i;
printf("Give how many albums you want: ");
scanf("%d%c",&n,&newline);
This will remove the '\n' from the stdin.
when you read with scanf() it reads everything leaving the following '\n' from the end
And Now when you try to read with fgets()it reads the'\n' character left by scanf
To solve this you can use fgetc(stdin); after your scanf so that it gets consume.
In the code below in lesson2() i have used a password to enter the function but when i enter the function it does not takes in the passord and says incorrect password.By not taking in the password,i mean to say that i have used gets but its waiting for me to input a password.please dont tell me not to use gets!
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
int mistakes=0,mistakes_length,len;
char temp[100];
void lesson1(void);
void lesson2(void);
void result(void);
void main(void)
{
int choice;
clrscr();
printf("Enter a lesson number to practise:1-10 \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
lesson1();
result();
break;
case 2:
lesson2();
result();
break;
default:
printf("You did not entered a valid choice program quitting..\n");
exit(0);
}
getch();
}
void lesson1(void)
{
int i;
char str1[100]="testing the typing tutor if it works";
mistakes_length=5;
clrscr();
gotoxy(25,2);
puts("Welcome to lesson 1");
puts(str1);
len=strlen(str1);
for(i=0;i<len;i++)
{
temp[i]=getche();
if(strncmpi(&str1[i],&temp[i],1))//does not match
{
mistakes++;
sound(100);
delay(1000);
nosound();
}
}
getch();
}
void result(void)
{
printf("Your statsistics are as under:\nYou performed this lesson with %d mistakes\n",mistakes);
if(mistakes>=mistakes_length)
{
printf("\n Your typing is very bad");//allow anything to be typed with any mistake in lesson 1
}
if(mistakes>3&&mistakes<5)
{
printf("Bad!,You need to practise this excercise more\n");
}
if(mistakes>=1&&mistakes<=3)
{
printf("Good!you can still do better\n");
}
if(mistakes==0)
{
printf("Excellent!You are qualified for the next lesson\n");
printf("The next lessons password is \n\t\t\t:12345");
}
}
void lesson2(void)
{
char password[]="12345",str2[]="My name is khan and i am not a criminal";
int i;
mistakes=0,mistakes_length=0,
printf("Enter password:\n");
gets(temp);
if(strcmp(temp,password))
{
gotoxy(20,25);
printf("Wrong Password,Program Quitting.\n");
getch();
exit(1);
}
gotoxy(25,25);
printf("Password Accpted!");
getch();
clrscr();
gotoxy(25,2);
printf("Welcome to lesson 2\n");
printf("Type the text shown below:\n");
puts(str2);
len=strlen(str2);
for(i=0;i<len;i++)
{
temp[i]=getche();
if(strncmp(&str2[i],&temp[i],1));
{
mistakes++;
}
}
getch();
}
I think that it is the scanf and gets together making a problem!
Your problem is that prior to calling gets(), you have called scanf("%d",&choice); (in main()). The problem with this is that console input is line oriented. This means that although you are only waiting for a number to be entered the user has to enter a complete line ending in newline. The %d format specifier only consumes the digit characters leaving the rest of the line in the buffer to be used by the next console input call; which in this case is gets() which sees the newline in the buffer and returns an empty string without waiting for further input.
A solution:
scanf("%d",&choice);
while(getchar() != '\n' ) { /*no nothing*/}
I think the problem you are seeing is in fact coming from the main method.
The scanf() call that you make is only looking for "%d" rather than "%d\n".
Therefore, you enter the number 2 in order to try test 2, and must press enter before that value gets to the program. Since scanf is not trying to match it, that newline remains as part of the input stream and thus is passed directly to gets() within the next function.
Therefore, if you change the string within scanf inside the main function, you should see the code start working.
Now on another note (and I know that you asked us not to but...) you really shouldn't use gets(). In order to switch to fgets, just replace gets(temp) with fgets(temp,99,stdin). That 99 is any number that is less than the size of the temp buffer you have made, which in this case has size 100.
Try printing temp like this: printf("[%s]\n", temp); after the gets(temp); to see what is saved in temp. It may takes as input a previous given input from lesson1 function?
If this is the case, something like this maybe will fix the problem:
char other_tmp[5];
gets(other_tmp);
gets(temp);
although in such a case you should better correct the lesson1 function instead.
it supposed to work.
edit your code:
int r = gets(temp);
printf("returned: %d, Entered: %s\n", r, temp);
and post here the result