switch problem

This is a discussion on switch problem within the C Programming forums, part of the General Programming Boards category; Hi, guys, I wrote this project in VS, it works as it is supposed to. Then I copied the file ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    switch problem

    Hi, guys, I wrote this project in VS, it works as it is supposed to. Then I copied the file to UNIX, and after compiling it does not work as it should. When I enter 101 it displays the message and then, unexpectedly says it is grade F. How come? What happened?
    I will appreciate your help here...
    Code:
    
    
    #include <stdio.h>
    
    
    main()
    {
    int num_grade, tens;
    
    
    
    
    	printf("Enter numerical grade:");
    	scanf("%d", &num_grade);
    
    
    
    
    	if ((num_grade < 0) || (num_grade > 100))
    		printf("Grade must be positive AND less than 100.\n");
    	else 
    		tens = num_grade / 10;
    
    
    
    
    
    	switch (tens) {
    	case 0: 
    		
    	case 1:
    		
    	case 2:
    		
                    case 3:
    		
    	case 4:
    		
    	case 5: printf("The grade is F.\n");
    		break;
    	case 6: printf("The grade is D.\n");
    		break;
    	case 7: printf("The grade is C.\n");
    		break;
    	case 8: printf("The grade is B.\n");
    		break;
    	case 9: printf("The grade is A.\n");
    		break;
    	case 10: printf("The grade is A.\n");
    		break;
    
    	}
    
    
    return 0;
    }
    Oh, one more question. Should I include break after the first 4 "case"'s?

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by kocika73
    Hi, guys, I wrote this project in VS, it works as it is supposed to. Then I copied the file to UNIX, and after compiling it does not work as it should. When I enter 101 it displays the message and then, unexpectedly says it is grade F. How come? What happened?
    I will appreciate your help here...

    First time through the program: If num_grade is > 100, tens is not assigned, then you switch(tens). Check your logic.

    Regards,

    Dave

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Yeah it goes into the switch statement without any value in the variable tens (well, it could well be a garbage value) - the weird part is that on my Linux machine it still runs fine - it prints the message about the allowable range and then exits...

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by kermit
    Yeah it goes into the switch statement without any value in the variable tens - the weird part is that on my Linux machine it still runs fine - it prints the message about the allowable range and then exits...
    Since he doesn't have a default: for his switch, it is possible that the location for tens has a value that is not satisfied by any of his labels so it just fell through. Anything is possible.

    Just another example of the fact that you can't prove a program works by testing.

    Regards,

    Dave

    (Dave's corollary to Murphy's law: If everything seems to be going OK, you have obviously overlooked something.)

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Indeed - I thought it hads something to do with the value in the variable tens. Upon initialising it to 0, I find that it works the way the OP had described.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    "Could you have any more whitespace?" -- Friends' Chandler
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Thanks for your response!
    Well, I do not know what might be wrong with "tens"... It is assigned after checking for bad input...
    I just tried inserting return 0; in the if else like this:
    Code:
    	if ((num_grade < 0) || (num_grade > 100))
    	{
    		printf("Grade must be positive AND less than 100.\n");
    		return 0;
    	}
    		else 
    		tens = num_grade / 10;
    I tried again in UNIX and now it works. Was that the problem?
    I thought my logic statements were fine... What do you think?

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    If you eliminate most of the stuff and just focus on the root of the problem one possible execution branch of your code is:
    Code:
    int main(void)
    {
      int num_grade, tens;
    
      printf("Enter numerical grade:");
      scanf("%d", &num_grade);
    
      if ((num_grade < 0) || (num_grade > 100))
        printf("Grade must be positive AND less than 100.\n");
    
      switch (tens) {
        ...
      }
    
      return 0;
    }
    tens is being used uninitialized. It's not guaranteed to start at 0 or any other value. It starts with whatever value was in memory (before your program even began) at the location the compiler set aside for that variable.

    The reason your return 0; fixed it is because now it never gets down to the switch() statement when tens is uninitialized.
    Last edited by itsme86; 09-26-2004 at 12:44 PM.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by kocika73
    Thanks for your response!
    Well, I do not know what might be wrong with "tens"... It is assigned after checking for bad input...
    I just tried inserting return 0; in the if else like this:
    Code:
    	if ((num_grade < 0) || (num_grade > 100))
    	{
    		printf("Grade must be positive AND less than 100.\n");
    		return 0;
    	}
    		else 
    		tens = num_grade / 10;
    I tried again in UNIX and now it works. Was that the problem?
    I thought my logic statements were fine... What do you think?
    Using your basic program organization, I would rather see something like:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    int num_grade, tens;
    
      printf("Enter numerical grade:");
      scanf("%d", &num_grade);
    
      if ((num_grade < 0) || (num_grade > 100)) {
        printf("Grade must be positive AND less than 100.\n");
      }
      else {
        tens = num_grade / 10;
        switch (tens) {
          case 0: 
          case 1:
          case 2:
          case 3:
          case 4:
          case 5: printf("The grade is F.\n");
                  break;
    
          case 6: printf("The grade is D.\n");
                  break;
    
          case 7: printf("The grade is C.\n");
                  break;
    
          case 8: printf("The grade is B.\n");
                  break;
    
          case 9: 
          case 10: printf("The grade is A.\n");
                  break;
    
        }
      }
    
      return 0;
    }
    I always use {} after if and else clauses so that the intended logic is clear (to me, at least). But that's just me.

    Regards,

    Dave

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Yes, Dave, you're totally right. NOW I see. The brackets... Now I see what all of you were talking about with tens uninitialized. Right. Thanks. Let me go ahead and put it all together as it should be.
    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM
  2. problem on switch
    By toxicherry in forum C Programming
    Replies: 11
    Last Post: 12-31-2007, 05:17 AM
  3. Switch Problem
    By Tynnhammar in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2004, 12:57 PM
  4. Replies: 1
    Last Post: 08-31-2004, 05:07 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21