My teacher is looking for ways to mess up our programs and i need some help badly. during my while-switch i have 5 cases and a default and im using numbers for the variable that is switching and when the user inputs a letter it sends it on a loop here is the code for my function main. i tried to comment heavely too.
int main (void) //start main function
{ //open main
int item = 0; //variable that keeps the while going
names(); //function call for names
instructions(); //function call for instructions
while ( item != 5)
{ //open while
//Prompt total Scores of both players with their names
printf("\n\nScore for %s is: %d", player1name, player1score);
printf("\nScore for %s is: %d", player2name, player2score);
//Menu for choosing category
printf("\nPlease pick your category\n\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("5. To End Game\n");
scanf("%d", &item); //input from user
switch (item) //item is switching
{ //open switch
case 1: //if item equals one
add (); //function call to add
break; //break case
case 2: //if item equals two
subtract (); //function call too subtract
break; //break case
case 3: //if item equals three
multiply (); //function call to multiply
break; //break case
case 4: //if item equals four
divide (); //function call to divide
break; //break case
case 5: //if item equals five
end(); //function call to end
break; //break case
case'\n':
case'\t':
case' ':
break;
default:
printf("Incorrect item entered.");
printf(" Enter a new catorgory.\n");
break;
} //end switch
} //end while
printf("\n\nTHANKS FOR PLAYING :)"); //output to user
return 0; //end main successfully
system("cls");
} //end function main
A. Please use the code tags when posting code so that it is legible.
B. scanf() has a return code. Use it.
The biggest sin in C development is failure to check return codes. New C developers should be fitted with shock collars for their first year of professional programming for this very reason. It's why exceptions were invented: "don't ignore this error you stupid git!"
sorry for the legible part i will repost tomorrow more clearly. scanf has a return code? please explain me and this is professionally i just took the class for because it sounded interesting and i love it so if you could not tell me the answer but kind of teach it to me. thanks again
but it doesnt return 0. it goes on a loop ive been looking up on that page which has help me understand alittle more but now when i enter a char it just stops?
while (scanf("%d") != '\n');
this is what i put in to stop the loop but it doesnt continue? sorry in my class we have only covered the 1st couple of chapters
i have it where when item equals 5 then it ends the math game so i couldnt use greater than 0. there as to be a way while item != 5 that i can put something inside that function, like a case or put something as default, to where if a char is inputed that it will read invaild entry please enter a correct entry. right?
You aren't understanding correctly. Checking scanf's return value does not affect your program in any way. Try to get that out of your head. You while loop should loop until the value you recieve through scanf is 5, or when scanf returns something < 0 (these are different things).
Please note the "Reference" section on this site. Use it regularly. A developer must know function names, purpose, what header it is declared in, its arguments, how it takes those arguments (by value, reference or pointer), limitations, side effects, return value, exceptions thrown, error codes that it will set, etc. Details matter in this profession. This is what you teacher is trying to show you.
ok so ya'll are saying there is nothing wrong with the scanf. thanks for the link to the reference. ill bookmark it. so im i still having that infinite loop when a letter is entered instead of a number? and again sorry for my ignorance on this subject
im not sure i get your question. i have the value of item in scanf as a integer, when a letter is inputted it loops. i have item set to zero in the beginning of function main. would me sending you a copy of the whole program help any?
Do you understand the difference between a function parameter (or argument) and a function return value? On line 33, your scanf() has two function arguments 1) "%d", 2) &item. And it has a return value which you are ignoring (discarding).
Compare line 1 in my example with line 33 in your code. I do not ignore the return value. I store it in a variable called "result" and then I do something with that return value.
ok im following that logic and your saying result equals whatever i put in scanf() and if the results is greater than one then the program execute's this. right?
No, it does NOT equal what you put into scanf(). scanf() returns a value indicating how many items were successfully read. It does NOT mean anything about what it read, only that it was unable to process it with the format specifier you provided.