I have a program that asks for the temperature and returns a response depending on what the temperature is.
int main(void)
{
while(1)
{
int temp;
printf("What is the temperature?\nTemp: "); // fetch temperature
scanf("%d", &temp);
if (temp == -858993460) // for some reason parsing "q" returns this value
{
break;
}
in
// Att trycka q först funkar, men om man först skriver in en tempratur så funkar det inte att i senare iterations skriva q, då tar programet bara den senaste inlagda tempraturen
#include <stdio.h>
#include <stdlib.h>
#pragma warning (disable: 4996)
int main(void)
{
while(1)
{
int temp;
printf("What is the temperature?\nTemp: "); // fetch temperature
scanf("%d", &temp);
if (temp == -858993460) // for some reason parsing "q" returns this value
{
break;
}
// check the temperature against diffrent values
if (temp > 32 && temp < 40)
{
printf("%d is too hot!", temp);
}
else if (temp > 18 && temp < 33)
{
printf("%d is a good temperature", temp);
}
else if (temp > 39)
{
printf("It's %d degrees, turn on your AC!", temp);
}
else if (temp < 19)
{
printf("%d is too cold!", temp);
}
else
{
printf("Something has went very wrong...")
}
printf("\n\n----------\n\n");
}
return 0;
}
if you input "q" during the first iteration it works as expected:
temperatur.exe (process 26592) exited with code 0. Press any key to close this window . . .
but inputting a number during the first iteration, the "q" during iteration n>1
returns
(assume first input is "12" second input is "q")
What is the temperature?
Temp: 12
12 is too cold!
----------
What is the temperature?
Temp: q
12 is too cold!
----------
What is the temperature?
Temp: 12 is too cold!
----------
What is the temperature?
Temp: 12 is too cold!
----------
...
I can't find anything on any forums of anyone having a similar issue
CodePudding user response:
In order to determine whether scanf
was successful, you should check the return value of scanf
. It will return an int
which specifies the number of arguments that were successfully converted (which should be 1
in your case). If something went wrong, it will return a number less than the number of arguments that you asked for (i.e. 0
in your case). When no arguments were converted, it may also return the special value EOF
instead of 0
in some situations.
Therefore, if scanf
does not return 1
, you should ignore the value of temp
, as it is not guaranteed to contain a meaningful value.
For this reason, it would probably be best to change the lines
scanf("%d", &temp);
if (temp == -858993460) // for some reason parsing "q" returns this value
{
break;
}
to:
if ( scanf( "%d", &temp ) != 1 )
{
break;
}
However, I generally do not recommend using scanf
for user input, as that is not what the function is designed for. That function does not behave in an intuitive manner when dealing with user input. For example, it does not always read an entire line of input at once.
You may want to read this guide for further information:
A beginners' guide away from scanf()
CodePudding user response:
Taking a look at https://www.geeksforgeeks.org/scanf-in-c/ which tells us that the return value of scanf tells us
>0: The number of values converted and assigned successfully.
0: No value was assigned.
<0: Read error encountered or end-of-file(EOF) reached before any assignment was made.
The problem is that when scanf encounters the q, it doesn't jump over the q, it stays at the q and you get the same every iteration.
To solve this, you can simply get characters from the input stream until you encounter a new line like this:
if(scanf("%d", &temp) != 1)
; // handle invalid input
while(fgetc(stdin) != '\n'); // or getchar() if you prefer