Program skipping while loop

This is a discussion on Program skipping while loop within the C Programming forums, part of the General Programming Boards category; I basically got two problems, I get a control reaches end of non void function warning in my function even ...

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Program skipping while loop

    I basically got two problems, I get a control reaches end of non void function warning in my function even though I got a return.

    And secondly my get char in the while loop near the end of the main function is not being evaluated, the program just ends after I enter a team letter, it wasn't doing this earlier when I used scanf instead of get char, but scanf was giving me character constant too long warning.

    Code:
    //ASSIGNMENT 1
    
    #include <stdio.h>
    
    #define RETURN '\n'
    #define PRINT_SCORE 1
    
    char gameScore (char s);
    
    
    int main (int argc, char **argv) {
    
      char team;//Team
      char s;
      char teamA;
      char teamB;
    
    
      printf ("Which team to serve? A or B?\n");
      scanf ("%c", &team);
    
      if ( team  == 'A') {
        printf ("Team A to serve:\n");
      }  
      else if ( team == 'B'){
        printf ("Team B to serve:\n");
        }
      else{
        printf ("Incorrect entry, try again\n");
        return 0;
      }  
    
      while ((s = getchar()) != '\n') {
       
        if (gameScore( s ) == PRINT_SCORE){
          printf("%d-%d\n", teamA, teamB);
        }
      }     
         
    
    return (0);
    }
    
    char gameScore (char s){
    
      int teamA;
      int teamB;
    
      teamA = 0;
      teamB = 0;
    
      if(s == 'A'){
        teamA = teamA+15;
      }
      
      else if(s == 'B'){
        teamB = teamB+15;
      }  
      
      else if(s == 'S'){
        return(PRINT_SCORE);
      }
      
    }
    Any help would be greatly appreciated, cheers

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    a non-void function must always return a value, such that if I was to do the following

    Code:
    char value = gameScore('Z');
    The result would be unpredictable.

    In other words, all control paths must lead to a return value, even if it is just 0.

    Code:
    char gameScore (char s){
    
      int teamA;
      int teamB;
    
      teamA = 0;
      teamB = 0;
    
      if(s == 'A'){
        teamA = teamA+15;
      }
      
      else if(s == 'B'){
        teamB = teamB+15;
      }  
      
      else if(s == 'S'){
        return(PRINT_SCORE);
      }
      return 0;
    }
    getchar() returns an int, not a char. The FAQ contains an example which relates to your problem: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Last edited by zacs7; 08-09-2007 at 02:25 AM.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    But i'm not concerned with it returning anything...yet, I'm just wondering why it doesn't even get to the while loop but just heads to return 0;

    The FAQ wasn't entirely helpful, unless you mean I have to clear my input buffer before going into the while loop ??? :S?

    Thanks for letting me know I have to put return 0 for the other function btw

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    Well you've got some problems. Let's talk about your function first.
    Code:
    char gameScore (char s);
    If you write a function prototype such that it returns a value, you have to return something for all code paths. It doesn't matter which team's score you need to retrieve, even if you don't know which team the user is talking about, you have to return a value.

    You should also return values that agree with the type. While there is nothing wrong per se with returning 1 as a char, it is different than returning something like 'f'. It would make more sense to return an integer because that is what 1 is.
    Code:
      if(s == 'A'){
        teamA = teamA+15;
      }
      
      else if(s == 'B'){
        teamB = teamB+15;
      }
    I've also noticed that you seem to think that team variables will increase with each call to your function, but this won't be the case. A function's argument is a copy of the variable it is passed, so that any changes you make to the argument will not affect similar variables elsewhere.

    What teamA contains in main will always be different than teamA in getScore.

    In order to change something purposefully, it can be easiest to pass a pointer containing the address of the object you want to change.

    Code:
     while ((s = getchar()) != '\n') {
       
        if (gameScore( s ) == PRINT_SCORE){
          printf("%d-%d\n", teamA, teamB);
        }
    scanf will keave characters that aren't part of a format in your input stream. Characters like '\n'. So, functions like getchar() will read them, and usually your loops will break after one turn.

  5. #5
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Thanks for the info!

    I already decided to try using scanf again with some other conditions in it and it seems to give no errors this time, however I guess I need to change the type of the function, thanks for pointing that out!

    As for pointers, not learnt them yet, so I can't use it

    Cheers
    Last edited by JFonseka; 08-09-2007 at 02:51 AM.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    > As for pointers, not learnt them yet, so I can't use it

    Well I guess the teamA and teamB variables could be global (so that they could be updated regardless of scope), but pointers are better in this situation. Or use an array to represent scores. If you're interested in working ahead of the class, use the internet to learn about them, and then use them here.

    Either way, bother your professor about it soon. It's bread-and-butter type of knowledge.

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by citizen View Post
    > As for pointers, not learnt them yet, so I can't use it

    Well I guess the teamA and teamB variables could be global (so that they could be updated regardless of scope), but pointers are better in this situation. Or use an array to represent scores. If you're interested in working ahead of the class, use the internet to learn about them, and then use them here.

    Either way, bother your professor about it soon. It's bread-and-butter type of knowledge.
    Aha, global variables! I forgot about them, now I seem to be getting somewhere, thanks a lot dude! Now things should start falling into place.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Or make teamA and teamB static

    and gameScore returns their values.

  9. #9
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    If they are global variables, then if the variable is used in one function and it's value changes, and you use that variable in another function, say the main, the value should be identical right?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    If they are global variables, then if the variable is used in one function and it's value changes, and you use that variable in another function, say the main, the value should be identical right?
    Yes, and that is why they can be problematic: hard to track down just what is the state of the variable at a given point in the program.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 07-06-2009, 07:12 AM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. Program gets stuck in infinite loop
    By Xanth in forum C++ Programming
    Replies: 10
    Last Post: 02-07-2005, 11:51 PM
  4. detect infinite loop in c program
    By abhivyakat in forum C Programming
    Replies: 19
    Last Post: 10-01-2003, 06:55 AM
  5. Need Help on Program Using For Loop
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-26-2002, 05:54 PM

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