|
-
Jan 5th, 2006, 05:42 PM
#1
Thread Starter
New Member
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]
-
Jan 5th, 2006, 06:22 PM
#2
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?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 6th, 2006, 04:04 AM
#3
Thread Starter
New Member
Re: If else- letters not allowed
 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?
-
Jan 6th, 2006, 07:39 AM
#4
Re: If else- letters not allowed
Hello,
Welcome to Forums 
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.
-
Jan 6th, 2006, 03:40 PM
#5
Thread Starter
New Member
Re: If else- letters not allowed
 Originally Posted by Harsh Gupta
Hello,
Welcome to Forums
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.
-
Jan 6th, 2006, 03:52 PM
#6
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!!
-
Jan 6th, 2006, 04:23 PM
#7
Thread Starter
New Member
Re: If else- letters not allowed
 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.
-
Jan 6th, 2006, 04:33 PM
#8
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.
-
Jan 6th, 2006, 04:37 PM
#9
Thread Starter
New Member
Re: If else- letters not allowed
 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!
-
Jan 6th, 2006, 04:46 PM
#10
Re: If else- letters not allowed
for details on fflush(), please take a look here.
-
Jan 6th, 2006, 05:24 PM
#11
Re: If else- letters not allowed
Note also that this is non-standard and unportable behaviour! For more details, see this FAQ entry.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 7th, 2006, 04:38 AM
#12
Re: If else- letters not allowed
 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.
-
Jan 7th, 2006, 04:42 AM
#13
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 7th, 2006, 04:53 AM
#14
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.
Last edited by Harsh Gupta; Jan 8th, 2006 at 04:01 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|