Thread: Printing in reverse order

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

    Printing in reverse order

    Whenever my print statements execute the print the values fine but they reverse their order...the first value gets printed and it kinds of moves down and the second value moves on top, but i want the first value on top and the second value on the bottome and etc. Code highlighted in red are the main print statements

    Code:
    //ASSIGNMENT 1, TENNIS GAME
    #include <stdio.h>
    
    #define PRINT 0
    #define GAME_A 1
    #define GAME_B 2
    #define DEUCE 3
    #define AD_SERVER 4
    #define AD_RECEIVER 5
    
    int scoreA = 0;
    int scoreB = 0;
    
    int scoreFunct(char c, char team);
    void printFunct(int scoreA, int scoreB, char c, char team);
    
    int main (int argc, char **argv){
      
      char team;
      char c;
      
      printf ("Team A or Team B serves first?:\n");
      
      while((team = getchar()) != EOF){
        if((team == 'A') || (team == 'a') || (team == 'B') || (team == 'b')){
          printf("\nTeam &#37;c to serve\n", team);
        }
      break;
      }    
      scoreFunct(c,team);    
      return(0);
    }
    
    
    int scoreFunct(char c, char team){
      
      while((c = getchar()) != EOF){
    
        if ((c == 'S') || (c == 's')){
          printFunct(scoreA,scoreB,c,team);
          return(PRINT);
        }  
            
        else if((c == 'B') || (c == 'b')){
          if(scoreB < 30){
            scoreB+=15;
          }  
          else if(scoreB == 30){
            scoreB+=10;
          }
        }  
        else if((c == 'A') || (c == 'a')){
          if(scoreA < 30){
            scoreA+=15;
          }  
          else if(scoreA == 30){
            scoreA+=10;
          }
        }
      }      
    
      return(0);
    }
    
    void printFunct(int scoreA, int scoreB, char c, char team){
    
      
      if(scoreFunct(c,team) == PRINT){
        if((scoreA == 0) && (scoreB == 0)){
          printf("Love-All\n");
          printf("\n");
        } 
        else if((scoreA > 0) && (scoreB > 0)){
          printf("Team %c to serve\n", team);
          printf("%d-%d\n", scoreA, scoreB);
          printf("\n");
        }  
        else if((scoreA == 0) && (scoreB > 0)){
          printf("Team %c to serve\n", team);
          printf("Love-%d\n",scoreB);
          printf("\n");
        }  
        else if((scoreA > 0) && (scoreB == 0)){
          printf("Team %c to serve\n", team);
          printf("%d-Love\n",scoreA);
          printf("\n");
        }
      }
    }  

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I can't say for sure what the problem is, but I'm pretty sure that scoreFunct is called printfFunct and printFunct is in turn calling scoreFunct, which doesn't seem very reasonable in the way I see it.

    I can't see the function scoreFunct returning anything other than zero (it's called PRINT in one place, but it's replaced by the preprocessor with a zero), so the comparison with ==PRINT at printfunct will always be true, so the score will always be "Love-all".

    I have a guess that what you read from getchar() isn't what you expect it to be, and you are printing something other than the A or B that you expect it to be - you may want to check that out. Not that the rest of the code will work right due to at least the above list of problems, but at least you'll get the printout to work, perhaps.

    Also, your "break" in the while-loop is misplaced, it will break whether you enter A, B, or anything else.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i want to print my linked list in the reverse order please help
    By raghu_equinox in forum C Programming
    Replies: 9
    Last Post: 10-14-2006, 12:45 AM
  2. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  3. Divide and Conquer: array in the reverse order
    By Steamer in forum C Programming
    Replies: 11
    Last Post: 03-08-2004, 07:31 PM
  4. Printing name in reverse order and in caps
    By Guti14 in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2003, 07:02 AM
  5. Printing String in reverse order
    By ling in forum C Programming
    Replies: 14
    Last Post: 10-18-2001, 09:03 AM