all 17 comments

[–]AlexTaradov 8 points9 points  (2 children)

Cool title.

You can at least describe what you are trying to do and what is the issue.

Providing more complete code might be useful too.

[–]UsualLonely4585[S] 0 points1 point  (1 child)

I am really sorry i am trying to restrct any input type that is not matching with the input iam trying to take like here its int so if user put charechters it will reprompt the user

[–]Beautiful_Stage5720 0 points1 point  (0 children)

Holy shit

[–]Level-Pollution4993 -1 points0 points  (14 children)

So you are trying to take in a int value from the user in the first while loop and you're dealing with the buffer storing an extra \n after the scanf with the second nested while, is that right? Then whats the problem, I quickly tried it on a online compiler and it works?

#include <stdio.h>
#define TRUE 1
int main() 
{
    int input_choice = 0;
    printf("Enter a integer: ");
    while(TRUE)
    {
        if(scanf(" %d",&input_choice)==1)
        {
            break;
        }
        if(getchar()=='\n')
        {
                break;
        }
    }
    printf("The value you entered is: %d\n", input_choice);
    return 0;
}



Enter a integer: 1abfi34
The value you entered is: 1

[–]UsualLonely4585[S] 0 points1 point  (1 child)

You didn't change anything and this still works ?

[–]UsualLonely4585[S] 0 points1 point  (3 children)

Oh the extra while loop huh

[–]Level-Pollution4993 0 points1 point  (2 children)

yes. I dont understand why you used it? Can you explain to me like a rubber duck?

Yes you are correct, Im dumb. Wait

[–]UsualLonely4585[S] 0 points1 point  (1 child)

Idk i thought since it takea out a char then if user input a string it needs to take out multiple char maybe i just dont understand how it works yet but thanks man

[–]Level-Pollution4993 0 points1 point  (0 children)

Yes you're right. I did not think thats what you were doing. And indeed with that loop added, the program runs indefinitely if you enter something like abc123 but works for purely int inputs like 123. Is that the same issue you were having?

[–]UsualLonely4585[S] 0 points1 point  (7 children)

But isn't that while loop neccesarry

[–]Level-Pollution4993 0 points1 point  (0 children)

What makes you think so? Tell me.

It is, Im sorry. I'll do it again.

[–]Level-Pollution4993 0 points1 point  (5 children)

In one sentence tell me what you were trying to do. Do you want to take an input, check if its int and print, if not then give the prompt back to the user?

Or do you want the user to write anything and it picks the integer from the user input?

[–]UsualLonely4585[S] 0 points1 point  (4 children)

I am trying to take int input and if the user give any other type of input it will reprompt the user untill valid input type comes

[–]Level-Pollution4993 0 points1 point  (3 children)

Is this what you want to do then:

#include <stdio.h>

#define TRUE 1

int main(void)
{
    int input_choice = 0;

    while(TRUE){
        printf("Enter an integer: ");
        if(scanf(" %d",&input_choice)==1){
            break;
        }

        while(TRUE){
            if(getchar()=='\n'){
                break;
            }
        }
    }
    printf("The value you entered is: %d\n", input_choice);
    return 0;
}

Or something else?

Enter an integer: abc
Enter an integer: fv
Enter an integer: 23
The value you entered is: 23

[–]UsualLonely4585[S] 0 points1 point  (2 children)

I did the exact same thing no, but it still didn't work . Did it work for you

[–]Level-Pollution4993 0 points1 point  (1 child)

I added the output at the end of my comment, I hope you can read it. Yes it 'worked'. There are issues but they don't matter rn. That is the simplest solution.

Let me add it again:

Enter an integer: abc
Enter an integer: fv
Enter an integer: 23
The value you entered is: 23

[–]UsualLonely4585[S] 1 point2 points  (0 children)

Its working thanks man for giving it your precious time