teacher is looping my while-switch by inputing a char.

Pages: 12
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.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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




and my preprocessor directive's are:

1
2
3
#include "stdio.h" 
#include "stdlib.h" 
#include "time.h" 




IF I NEED TO POST MORE INFORMATION PLEASE LET ME KNOW THANKS FORUM
Last edited on
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
scanf() returns the number of arguments that it successfully read.

So in your case, if it got invalid data, it would return 0.

More info: http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
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
and again for everyone trying to help me out im sorry for my lack of education on this subject
scanf returns the number of arguments read, not a character. Comparing it to '\n' is very random...it's like doing this:

1
2
3
int x = 2;
if(x == '%') { //unrelated comparison, no point
//... 


Make the while loop loop while scanf is greater than 0 or something.
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?
is there a certain function i could put in a case in my switch that would take care of the problem like


case char:

or something like that?
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).
OK, in pseudo-code:

1
2
3
4
int result = scanf("%d", &item);
if (result < 1) {
    // then the user input garbage
}


That is the "return value" of which I speak.

http://www.cplusplus.com/reference/clibrary/cstdio/scanf/ -- find the section labeled "Return Value".

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
What is the return value from scanf when that happens?
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?
If you have not yet changed line 33, there is definitely something wrong with your use of scanf().
The value of the parameter "item" is irrelevant. That it not a return value. It is a parameter.
ok so is there something wrong with the %d im using?
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.
Pages: 12