Jump to content

Input should be only integer

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Archy

Archy

    Newbie

  • Members
  • Pip
  • 1 posts
hello!

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!!!!

#2
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
It is looping because, when you are using scanf(), you have to press enter, which loads a "\n" into the buffer, so next time it comes to the 'scanf()' function, it uses that - which gives the "invalid input" error!

This should fix it for you :)


#include <conio.h>

#include <stdio.h>





main()

{


    int input, correct;


    do

    {

        printf("input: ");

        scanf("%d", &input);

        [B]fflush(stdin);[/b]

        if (input < 999999)

        {

            printf("accepted");

            correct =1;

        }

        else

        {

            printf("not accepted\n");

            correct = 0;

        }

    }

    while(correct != 1);


    getchar();


}



#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
You should make yourself aware of the issues with scanf, and if your teacher recommended it for this situation she fails.

#4
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts

John said:

You should make yourself aware of the issues with scanf, and if your teacher recommended it for this situation she fails.

fgets() would be a better choice really.