Google is an important tool, one that every good programmer should understand how to use.
See?
Does C not execute two scanf's in the main function? Because I had my first scanf detecting values entered into an array with a while loop and then did error checks for values entered using multiple if statements.
Then after the error checking process was complete, I printed out several lines giving options to the user as to what he/she wants to do. So there were 3 options, after these lines were printed out I put a scanf after those lines, but when i execute the program, everything runs fine, except the program terminates after the last printf lines and does not execute the scanf, any ideas?
Code:#include <stdio.h> #include <ctype.h> #include <stdlib.h> #define RETURN '\n' #define SPACE ' ' int err (int); int main (int argc, char **argv) { int diceVal[5]; int n;//index of array int next;//next value that is read //int c; int option;//option for what to do with dice n = 0; printf ("Please enter dice values: \n"); while (scanf("%d", &next)){ diceVal[n] = next; n = n + 1; if ((next < 1) || (next > 6)) { printf ("Value Out of Range \n"); exit (0); } } if ((next > 6) && (n > 5)){ printf ("Value Out of Range \n"); exit (0); } else if ((next > 6) && (n < 1)){ printf ("Value Out of Range \n"); exit (0); } else if (n > 6) { printf ("Incorrect Number of Values \n"); exit (0); } printf ("%d values read into array \n", n); printf ("Please choose: \n"); printf (" 1 - > Reroll some dice \n"); printf (" 2 - > Reroll all dice \n"); printf (" 3 - > Keep dice \n"); scanf ("%d", &option); if ((option < 1) && (option > 3)) { printf ("Invalid choice \n"); exit (0); } return (0); }
It's impossible to say what's going on simply from looking at your code because the indentation in a mess.
Courtesy of indent -kr -nut -ts2 foo.c
What do you type in to make the first scanf in the while loop exit?Code:#include <stdio.h> #include <ctype.h> #include <stdlib.h> #define RETURN '\n' #define SPACE ' ' int err(int); int main(int argc, char **argv) { int diceVal[5]; int n; //index of array int next; //next value that is read //int c; int option; //option for what to do with dice n = 0; printf("Please enter dice values: \n"); while (scanf("%d", &next)) { diceVal[n] = next; n = n + 1; if ((next < 1) || (next > 6)) { printf("Value Out of Range \n"); exit(0); } } if ((next > 6) && (n > 5)) { printf("Value Out of Range \n"); exit(0); } else if ((next > 6) && (n < 1)) { printf("Value Out of Range \n"); exit(0); } else if (n > 6) { printf("Incorrect Number of Values \n"); exit(0); } printf("%d values read into array \n", n); printf("Please choose: \n"); printf(" 1 - > Reroll some dice \n"); printf(" 2 - > Reroll all dice \n"); printf(" 3 - > Keep dice \n"); scanf("%d", &option); if ((option < 1) && (option > 3)) { printf("Invalid choice \n"); exit(0); } return (0); }
Because whatever you type in will make the 2nd one fail immediately as well.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Your have some ridiculous conditions inside your code.
Redundant. You only need one of these two conditions inside your if statement. Even then, you then check the lower and upper bounds after this. Just do them both once using one if statement. Furthermore:Code:if ((next > 6) && (n > 5))
These will never be true. I imagine you meant these respective conditions:Code:if ((next > 6) && (n < 1)) ... if ((option < 1) && (option > 3))
Code:if ((next > 6) || (n < 1)) ... if ((option < 1) || (option > 3))
I agree with the last one, as || and not && I realized that when recompiling. But I do not understand what you mean with the previous ones. You do realize next and n are two different variables right?
Next is the value(s) entered into the array, n is the index of the array. But I will go through that part and try to make sense of it. THanks!
The bold black text can be deleted. The bold blue text indicates a change.Code:if ((next > 6) && (n > 5)) { printf("Value Out of Range \n"); exit(0); } else if ((next > 6) || (n < 1)) { printf("Value Out of Range \n"); exit(0); }
That was my first point.
I had a scanf in a while loop in the program, and everytihng was going fine until I had to use a second scanf in the main function. It just wouldn''t execute the scanf... or get char..
I highlighted the culprit in red, I think it's to do with the while loop in the first scanf. No joke, I've been trying to figure out what the hell to do for 1.5 hours and I tried so many combinations like puttting that scanf into a second function, using pointers, using get char, and it just doesn't work!!! Any ideas?
Code:#include <stdio.h> #include <ctype.h> #include <stdlib.h> #define RETURN '\n' #define SPACE ' ' int keep (int option); int main (int argc, char **argv) { int diceVal[5]; int n;//index of array int next;//next value that is read int c; int option;//option for what to do with dice int THK;//Three of a kind int FOK;//Four of a kind int FH;//Full House int SS;//Small straight int Straight; //Straight score int Yahtzee; int Chance; int sum; //sum of dice n = 0; printf ("Please enter dice values: \n"); while (scanf("%d", &next)){ diceVal[n] = next; n = n + 1; if ((next < 1) || (next > 6)) { printf ("Value Out of Range \n"); exit (0); } } if ((next > 6) || (n < 1)){ printf ("Value Out of Range \n"); exit (0); } else if (n > 6) { printf ("Incorrect Number of Values \n"); exit (0); } printf ("%d values read into array \n", n); printf ("Please choose: \n"); printf (" 1 - > Reroll some dice \n"); printf (" 2 - > Reroll all dice \n"); printf (" 3 - > Keep dice \n"); scanf ("%d", &option); if (option == 3) { keep (option); } return (0); } int keep (int option) { printf ("Your score options are: \n"); printf ("1 - > Three of a Kind \n"); printf ("2 - > Chance \n"); return (0); }
Well, your indentation is still a mess, you still don't say what you type in to get out of the first while loop.
> while (scanf("%d", &next))
The only way to make this exit the loop is to type in something %d doesn't match.
But as soon as you do that, it won't match scanf ("%d", &option); either.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
So your telling me it's because of my first scanf-while loop not exiting that's causing the problem? Now that you've mentioned it, that certainly seems to be true, .........
Thanks for pointing that out, I hope I don't have to change the whole code again