Hi guys,
Here's a little bit of my code I'd like help with (I can't post it all as it's an assignment, but I've completed this portion for you to test if necessary):
Code:
#include <stdio.h>
int main (void)
{ int n;
do {
printf ("\nHow many resistors would you like to enter?\n [2 to 10 Resistors can be checked]\n\n");
scanf ("%d", &n);
if (n < 2 || n > 10)
{ printf ("\nYou have entered a number out of range.\nPlease re-enter!\n"); }
} while (n < 2 || n > 10);
printf ("\nNumber of resistors to test: %d\n", n);
system ("PAUSE");
return (0);
}
I'd like to implement the following code into this part of my program, such that if the user does not input a digit, or inputs a character, then the program will output an error message "Input not accepted".
I don't want it to display the bit where it says "one converted as..", if chk does equal 1 then I want it to continue the program as normal.
Code:
#include <stdio.h>
int main (void)
{ int x, chk;
printf ("\n Input one decimal\n");
chk = scanf ("%d", &x);
if (chk == 1)
printf ("\n one converted as %d\n",x);
else
printf ("\nInput not accepted");
system ("PAUSE");
return (0);
}
No matter how I try to implement it, everytime I hit a character it'll just initiate an endless loop.
How can I do it? Or can you not do it inside a do while loop?
Thanks in advance!