I got test on my 2d course of Institute, I have to write program, where all input from user would be checked, and user shouldn't be able to input char/simbol in integer place!
first of all i tried "isdigit" from ctype.h library
but its not very suitable for my program, cause its check char is it digit or character, but in my program should be int in first place! than with "isdigit" u can check only from 0 to 9... but according our task user should be able to enter any number! so if user will enter 2/3/4 digit number it will not work! so "isdigit" doesnt match here
then i asked my teacher, she suggest this way:
if ( scanf("%d", &input) == 1 )
its very good working, but in my program if user enter wrong value, he should be give another chance and another.... until user ener it right
so i put it in "while loop" like this:
#include <conio.h>
#include <stdio.h>
main()
{
int input, correct;
do
{
printf("input: ");
if ( scanf("%d", &input) == 1 )
{
printf("true\n");
correct =1;
}
else
{
printf("false\n");
}
}
while (correct != 1);
printf("Exit");
getch();
}
but now when i input character in input my while loop just go crazy and become infinitive in spite of "scanf" statement inside this loop...
I find another solution which working some how :) but its not correct solution from programmers etiquette, its kind of cheating and i dont like it!
if u scan in int a letter or simbol it store it as 7 digits number... so i did like this:
#include <conio.h>
#include <stdio.h>
main()
{
int input, correct;
do
{
printf("input: ");
scanf("%d", &input);
if (input < 999999)
{
printf("accepted");
correct =1;
}
else
{
printf("not accepted");
correct = 0;
}
}
while(correct != 1);
getch();
}
but it still crash program and make while loop infinitive!
can u suggest me how can i solve this problem?
Many thanx!!!!


Sign In
Create Account

Back to top









