+ 3
How to place take input with condition greater than 10 & only integer is accepted??
plz ans give in c if possible
13 ответов
+ 5
@William La Flamme I do not think you are over thinking but the getchar method is to avoid user errors. If you write an a in your number, scanf will break your program (with %d). %c could be use to replace getchar, indeed. getchar can read EOF but to validate an input, you need to press the return key which is '\n'. But if you are afraid the user write something like that : text.txt > myprogam then yes, check EOF in the same time as '\n'.
I hope I understood what you said as I am not fluent in English :)
+ 2
int n;
char c;
int error;
do{
n=0;
error=0;
do{
c=getchar();
if(c<'0' || c>'9' || error){
error = 1;
}else{
n*=10;
n+=c-'0';
}
}while(!error);
if(error && c!='\n'){
while(getchar()!='\n');
}else{
error = 0;
}
}while(n<=10 || error);
You just have to add messages for user.
I hope I understood your question properly :)
+ 2
@shruti just remove your first scanf and it will work
No problem, I hope you'll enjoy coding in C as much as I do :)
+ 2
My pleasure :)
+ 1
@shruti I tried it and did not get the error you get, that's strange .. what was the input you tried ?
@William La Flamme, I do not know for Java, but in C, characters are integers coded on only one byte so I do not get why there could be errors. Can you explain it to me please ?
+ 1
@shruti it is normal ! You have leave the scanf, so it will ask twice :)
0
the loop is executed one time extra.. the same value is entered at 2 times for ur code baptiste
0
im more curious why to assure you take an int you used getchar? i get the 'trick' per se but that is so error prone its mind boggling. Trying to get the compiler to properly understand a < or > for a char is risky, OS and locale dependant, and just a very hacky way to error check, its the same reason you should (in java) not compare a string like "strA == strB" but use instead "strA.equals(strB)".
I'm not 100% on the proper C code for this but i would recommend whatever method is "isNaN()" to do proper sanity checking, and may need to be converted to/from a float if you really need an int return
0
#include<stdio.h>
#include<conio.h>
int cnt=0,correct;
int age,tage,lb,r,q,b=11;
int base,newage,i=0;
int pow10=1,j,k;
int ansBase,ansAge;
void main()
{
clrscr();
printf("Please enter the age: ");
scanf("%ld",&age);
char c;
int error;
do{
age=0;
error=0;
do{
c=getchar();
if(c<'0' || c>'9' || error){
error = 1;
}
else{
age*=10;
age+=c-'0';
}
}while(!error);
if(error && c!='\n'){
while(getchar()!='\n');
}else{
error = 0;
}
}while(age<10 || error);
printf("Please enter the lower bound: ");
scanf("%ld",&lb);
do{
lb=0;
error=0;
do{
c=getchar();
if(c<'0' || c>'9' || error){
error =1;
}
else{
lb*=10;
lb+=c-'0';
}
}while(!error);
if(error && c!='\n'){
while(getchar()!='\n');
}else{
error = 0;
}
}while(lb<=10 && lb<=age || error);
printf("\nOriginal age:%ld",age);
printf("\nLower bound:%ld",lb);
printf("\n Base NewAge");
for(b=10;b<=age;b++)
{
pow10=1;
cnt=0;
tage=age;
base=b;
newage=0;
while(tage!=0)
{
r=tage%b;
if(cnt>0)
pow10=pow10*10;
if(r>=10)
break;
newage=newage+pow10*r;
tage=tage/b;
cnt++;
}
if(newage>=lb)
{
printf("\n%ld ",base);
printf(" %ld ",newage);
ansAge=newage;
ansBase=base;
}
i++;
}
printf("\n\nBase is %ld",ansBase);
printf("with respective to new age is%ld",ansAge);
getch();
}
just see this program...& run...if I enter the value 2 times then go to the next statement
0
@Baptiste plz.give the solution of that age which inputs only integer & that value is greater than equal to 10
0
can you give me a correct code??with changes...i am New in c
0
It's work...thank you so much😊
0
@Baptiste i was looking at your code and was more thinking the person using it may see getchar and oh ok, scanf("%c", &n) which may be an issue, or %d, c as well, also EOF is a valid getchar, so i still rather another method, im used to needing detailed sanity checks for security implementation so maybe im just over thinking it lol