I'm a beginner in the code world, taking my first programming class this semester. I got bored over spring break and wrote a program to send to some friends just for the fun of it, but I'm having a problem. I want to isolate any character entered that is not an integer and get it to print a silly error message. I used a scanf check of sorts to do it, but unfortunately it instantly loops if the statement fails. (I only wanted it to loop once). Code follows (sorry if it's a little choppy, I copied and pasted):
//Zephraph, 2010
#include <stdio.h>
#include <stdlib.h>
int Ndice (void);
int prog (void);
void Side1 (void); //Again, chopped these off
void Side2 (void); //so the code wouldn't be
void Side3 (void); //as long.
void Side4 (void);
void Side5 (void);
void Side6 (void);
int main (void)
{
char ans;
printf ("Welcome to the dice simulator!\n\n");
prog ();
printf ("Would you like to re-run the program? (y/n)\n");
scanf ("%c", &ans);
if (ans == 'y'){
printf("\n\n");
main();
}
else
exit(1);
return 0;
}
int prog (void)
{
int dice; //number of dice to be rolled
int times;
int side;
dice = Ndice ( );
printf ("\nYou've entered: %d\n\n", dice);
if (dice < 0){
printf ("\nVery funny, wise guy. I'd like to see you roll a negative number of dice.\n"
"While you're at it, how about dividing by zero? I'll wait. Meanwhile, try again. \n\n");
prog ();
}
else if (dice == 0){
printf ("\nYou DO know this is a dice simulator, right? Oh wait, yeah, I told you.\n"
"Well, if you enjoy feeling like an imbecile, feel free to enter zero again,\n"
"otherwise enter something greater than zero, yeah? Try again.\n\n");
prog ();
}
else if (dice > 0 && dice <= 10)
for ( times = 1;times <= dice; times++)
{
side = 1 + rand() % 6;
if (side == 1)
Side1();
else if (side == 2)
Side2();
else if (side == 3)
Side3();
else if (side == 4)
Side4();
else if (side == 5)
Side5();
else
Side6();
}
else {
printf ("Did you not get the memo? I said 10 or less, and %d is sure as heck not less\n"
"than 10. You're a numerical failure. Try again.\n\n", dice);
prog ();
}
return 0;
}
int Ndice ( void )
{
int nrolls = 0;
printf ("Please enter the numerical amount of dice you would like to roll (maximum 10):\n");
if (scanf("%d", &nrolls) !=1 ){
printf ("\nI'm not even programmed to recognize what you entered, so I'm guessing\n"
"that it wasn't even a number. You Sir/Madam, are retarded. Read the directions\n"
"and try again.\n\n");
prog ();
}
return nrolls;
}
For the sake of space I chopped off the end functions. All they contain are printf statements of dice "images" if you will. My error is occurring in Ndice(). That function basically asks the user how many dice to roll and, if in the correct bounds, returns the value of nrolls to help calculate other things in the prog() function. That part works golden. But if it fails the IF statement it's suppose to print the error message, call itself, and repeat... but not indefinitely? Which it does.... Any pointers? (By the way, if I remove the call statement it doesn't do the crazy infinite loop but I'm not sure how else to repeat the function).
Oh and also, I kinda want the program to exit upon completion. As in, the console completely close. I've fiddled around with the exit command and even the close and. Can't seem to get it.
Lastly, don't mind the insults on the program. It's all in good fun and my friends know that.
Thanks for any help!