If else- letters not allowed
[c]
Just want to be able to prevent my program from crashing when i input a letter.
I thought that when i use if else statement the program will take into account all the keys and not the numbers. This seems not to be the case. Then, i thought of declaring value as char instead of double. This does not work.
Value is of type double.
scanf("%lf",&value);
if(value < 1 || value > 50|| )
{
printf("not allowed\n");
printf("Please choose value between 0 and 90");
}
else
{
}
I thought that when i use 'if else' statement the program will take into account all the keys and not the numbers only. This seems not to be the case. Then, i thought of declaring 'value' as 'char' instead of 'double'. This does not work.
What i want is that ONLY the values between 0 and 50 are allowed.
[c]
Re: If else- letters not allowed
Well? What's the expected result, the actual result? Any compile errors (for example from that extra logical OR in the if)?
As it is now, the else will be only executed if the number is between 1 and 50 inclusive. Isn't that what you want?
Re: If else- letters not allowed
Quote:
Originally Posted by CornedBee
Well? What's the expected result, the actual result? Any compile errors (for example from that extra logical OR in the if)?
As it is now, the else will be only executed if the number is between 1 and 50 inclusive. Isn't that what you want?
Yes, i rectify what i have written. There should not be any extra ||. I forgot to erase it. What i was doing was the following:
if(value < 1 || value > 50|| (char) value)
So,when i type letters, the characters should not be accepted. Unfortunately, this does not work.
There is no compile errors if the extra|| is taken out.
But suppose by mistake instead of typing values between 1 and 50 I type a letter. Then, the program crashes.
So i want some flexibility so that no letters are allowed. Only the values between 1 and 50. Is that possible?
Re: If else- letters not allowed
Hello,
Welcome to Forums :wave:
what you are trying to accomplish can be acheived by playing with ASCII values but i won't suggest you for this. you may try reversing the if-then-else statement. like:
Code:
scanf("%lf",&value);
if(value > 1 && value < 50 )
{
//do your stuff here
}
else
{
printf("not allowed\n");
printf("Please choose value between 0 and 50");
}
hope it helps you.
Re: If else- letters not allowed
Quote:
Originally Posted by Harsh Gupta
Hello,
Welcome to Forums :wave:
what you are trying to accomplish can be acheived by playing with ASCII values but i won't suggest you for this. you may try reversing the if-then-else statement. like:
Code:
scanf("%lf",&value);
if(value > 1 && value < 50 )
{
//do your stuff here
}
else
{
printf("not allowed\n");
printf("Please choose value between 0 and 50");
}
hope it helps you.
No this does not help. A lot of ' Not allowed' appears on the window when i typed a letter.
Re: If else- letters not allowed
in that case, i request you to please post the whole code. if thats not possible, then please post the function code in which it is running, because it is running on my PC!!
Re: If else- letters not allowed
Quote:
Originally Posted by Harsh Gupta
in that case, i request you to please post the whole code. if thats not possible, then please post the function code in which it is running, because it is running on my PC!!
#include <stdio.h>
int main()
{
double value;
int leaving =1;
while (leaving == 1)
{
printf("enter");
scanf("%lf",&value);
if(value > 1 && value < 50 )
{
leaving = 0;
}
else
{
printf("not allowed\n");
printf("Please choose value between 0 and 50");
}
}
}
I thought by using this code, the program will ask me again and again to enter a value between 1 and 50 if my value is out or I pressed a letter.
Re: If else- letters not allowed
Code:
#include <stdio.h>
int main()
{
double value;
int leaving =1;
while (leaving == 1)
{
printf("enter");
scanf("%lf",&value);
if(value > 1 && value < 50 )
{
leaving = 0;
}
else
{
fflush(stdin);
printf("not allowed\n");
printf("Please choose value between 0 and 50");
}
}
}
my bad, thats was an abnormal behavior generally showed by C. now it will ask again even if you press a character key. just add the highlighted line to your code and give it a try.
and please revert back if it helps you. i just want to be sure if i am right.
Re: If else- letters not allowed
Quote:
Originally Posted by Harsh Gupta
Code:
#include <stdio.h>
int main()
{
double value;
int leaving =1;
while (leaving == 1)
{
printf("enter");
scanf("%lf",&value);
if(value > 1 && value < 50 )
{
leaving = 0;
}
else
{
fflush(stdin);
printf("not allowed\n");
printf("Please choose value between 0 and 50");
}
}
}
my bad, thats was an abnormal behavior generally showed by C. now it will ask again even if you press a character key. just add the highlighted line to your code and give it a try.
and please revert back if it helps you. i just want to be sure if i am right.
Yes. Indeed, it works. I do not know this line. Could you tell me what this line does? In my book there are many 'f' but few explanations!
Re: If else- letters not allowed
for details on fflush(), please take a look here.
Re: If else- letters not allowed
Note also that this is non-standard and unportable behaviour! For more details, see this FAQ entry.
Re: If else- letters not allowed
Quote:
Originally Posted by CornedBee
Note also that this is non-standard and unportable behaviour! For more details, see
this FAQ entry.
yes, you are right.
so is there any other way to do this instead of using fflush()?
thank you.
Re: If else- letters not allowed
You can read the remaining line into a scratch buffer with fgets(). In C++, you can also use istream::ignore(), so you don't need a buffer.
Re: If else- letters not allowed
sorry, could not get them. could you please provide an example?
EDIT: i am familiar with fgets() and gets(). but never used ignore(). so could you please explain with a small example how to use it?
thank you.