Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**Use C and only *** https://onlinegdb.com/HJ2aXV0P7 The above link program invo

ID: 3743802 • Letter: #

Question

**Use C and only ***

https://onlinegdb.com/HJ2aXV0P7

The above link program involves looking at the user's input. For the program, it only takes positive integers, so if the user happens to input something else, like a string of characters, it'll prompt the user to input an integer. If the user inputs a negative integer then the user will be prompted to enter a positive integer. The problem I'm having is that when I try to mesh these conditions together into the while loop, and I test it nothing happens when I enter a negative value. (The program works for characters and strings)

Purpose of program:

This program is supposed to take positive integer input from the user, and return the integer that they have entered. However, if the user does not enter an integer or positive number, then the user is to be prompted to either input a positive number (case of a negative number) or to enter a number (case of string/characters).

Please only expand off of the program that I have, and explain why what I am doing is wrong, and why your implementation works.

Please help me, your help is greatly appreciated!

Explanation / Answer

Given below is the modified code .
Please do rate the answer if it was helpful. Thank you

#include<stdio.h>
int main()
{
int n;
int done = 0;
while(!done){
printf("Please enter an integer: ");
if(scanf("%d", &n) == 1){
if(n <= 0)
printf("Input should be a positive integer! ");
else
done = 1; //got correct input
}
else{
while(getchar() != ' ');
}
}

printf("You entered: %d ",n);
return 0;
}


output
------
Please enter an integer: abc
Please enter an integer: xyz
Please enter an integer: 0
Input should be a positive integer! Please enter an integer: -78
Input should be a positive integer! Please enter an integer: 67
You entered: 67