hello,
i have made a program where it asks the user to enter an interger, but i want a error message to come up when the user enters a letter, how can i do this??
thanks
hello,
i have made a program where it asks the user to enter an interger, but i want a error message to come up when the user enters a letter, how can i do this??
thanks
Post your code.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
you cant prevent them from inputing a character without telling us your operating system and compiler.
you can use fgets() to input and atoi() to convert to int
this works by letting them input everything but continuing input until a number is provided.Code:char string[100] int number; do { fgets(string, 100, stdin) }while(!(number = atoi(string)));
The scanf() function returns the number of values assigned to the the given addresses (the ones that comesafter the control string.).For example, if you have
scanf ("%d %d",&x,&y) where x and y are integers,
and the input is say: 20 c
then no value will be assigned to y and 20 will be assigned to x.
and the return value of scanf() is 1.
Note:the character c will be available for the next scanf() function.
Thus,just check the return value of scanf().
ex:
Code:printf ("Enter an integer:"); temp=scanf("%d",&x); if(temp!=1) printf("Wish u knew what an integer is\n");
here is the code where i want to prevent users from entering a letter:
thanks for any helpCode:printf("Please enter the temperture:\n"); scanf("%d", &temp); if ( temp > 45 || temp < -25) { do { printf("The temperture is out of range.\n"); printf("Please re-enter temperture:\n"); scanf("%d", &temp); } while ( temp > 45 || temp < -25); else //rest of my program
Better still, read the FAQ .
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Try this...
Hope this serves the purpose...Code:temp=100; do{ printf("Please enter the temperture:\n"); int_var = scanf("%d", &temp); if (int_var==0) printf ("Enter an integer....") else if ( temp > 45 || temp < -25) printf("The temperture is out of range.\n"); }while (temp>45 || temp<-25);
Try this FAQ then:Originally posted by Mahesh Herle B
Try this...
Hope this serves the purpose...Code:temp=100; do{ printf("Please enter the temperture:\n"); int_var = scanf("%d", &temp); if (int_var==0) printf ("Enter an integer....") else if ( temp > 45 || temp < -25) printf("The temperture is out of range.\n"); }while (temp>45 || temp<-25);
http://www.eskimo.com/~scs/C-faq/q12.20.html
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
>>Try this FAQ then:
They didn't read the last FAQ I posted, what makes you think they'll read that one
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Good point...Originally posted by Hammer
>>Try this FAQ then:
They didn't read the last FAQ I posted, what makes you think they'll read that one
As I posted elsewhere, when I click on the FAQ links in Salem's messages, I don't get a FAQ, I get the index, which is not the link posted. I'm wondering if there is a link problem within the forum's server.
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
Simply use isalpha() - http://www.mkssoftware.com/docs/man3/isalpha.3.asp
am I the only one who read the FAQ!?
Do you people know what FAQ means!?
and How come people avoid the FAQ like the plague. The link Hammer posted HAS your solution if you read through it. AND you will LEARN something. I really don't get it but whatever.
NOW to right the injustice:
FAQ
-LC
Asking the right question is sometimes more important than knowing the answer.
Please read the FAQ
C Reference Card (A MUST!)
Pointers and Memory
The Essentials
CString lib
>Do you people know what FAQ means!?
FAQ is a way of saying "........ off", as can be found by the proper pronunciation of the acronym. The commonly accepted pronunciation is a compromise: two letters are sounded
as if they were forming a syllable and the third letter is named: it is 'fuh queue'. That's not only how it's said when it stands for "frequently asked question." When it means a compilation of frequently asked questions and their answers, that's 'fuh queue' too.
>and How come people avoid the FAQ like the plague.
Probably because they think we've been telling them to. See above.
My best code is written with the delete key.