Thread: I need some quick help with code.

  1. #31
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    really close now please help

    OK, almost done. Here is what i havenow. The errors are posted below it. I marked the lines where the errors are with ************ followed by the line number. Hope that helps you out. please let me know how to fix the problems. Thank you guys so much for all of your help throughout this. I'm using gcc compiler in telnet on windows xp




    Code:
    #include <stdio.h>
    #include<string.h>
     
     char name[100] = {0};
     int numlives = 5;
     int score = 0;
     int round = 1;
     int player1lives = 5; 
     int player1turn = 0;
     int player1score = 0;
     int player2lives = 5;
     int player2turn = 0;
     int player2score = 0;
     int templives;
     void swapplayers(void);
     void activeplayerstats(void);     ************ line 16
     void playround(char name[], int* numlives, int*score, int round);  ********* line 17
    
     int main(void) {     **********  line 19
     
       char name1[100]= {0};
       char name2[100]= {0};
     
        printf("Player one, what is your name?\n");
             scanf("%s", name1);
        printf("Player two, what is your name?\n");
             scanf("%s", name2);
         strcpy(name,name1); 
         round = player1turn;
         numlives = player1lives; 
         score = player1score;  
     
       while (player1lives>0 || player2lives>0) 
         {
           if (numlives>0)
             {
              templives=numlives;  
       playround(name, &numlives, &score,round);  
             }
           if (numlives==0)
             {
              printf("Your Game is Over.\n");
              }
       activeplayerstats();   
           
    swapplayers( &name1, &name2); ****** line 46
    
    
          }
       printf("Here are the final standings:\n");  
           if(player1score>player2score) 
           {
              printf("%s      %d points\n", name1, player1score);
              printf("%s      %d points\n", name2, player2score); 
           }
           else 
           {
              printf("%s      %d points\n", name2, player2score);
              printf("%s      %d points\n", name1, player1score);
            }
                     
     }
     
       void activeplayerstats(void)  {
           printf("%s, you now have %d points and %d lives left.\n", name,
                   score, numlives);
    
       void swapplayers(char *p, char *q) {
         char tmp[100];   ******** line 70
             tmp = *p;
             *p = *q;
             *q = *tmp;
          }   **************** line 74
    errors....

    game3.c: In function 'swapplayers':
    game3.c:19: parse error before '{' token
    game3.c:19: declaration for parameter 'main' but no such
    parameter
    game3.c:17: declaration for parameter 'playround' but no such parameter
    game3.c:16: declaration for parameter 'activeplayerstats' but no such parameter
    game3.c:19: number of arguements doesn't match prototype
    cc1: prototype declaration ********whats this??
    game3.c:46: too many arguements to function 'swapplayers'
    game3.c: In function 'swapplayers':
    game3.c:70:incompatible types in assignment
    game3.c: In function 'activeplayerstats':
    game3.c:74arse error at end of input
    Last edited by stehigs321; 10-30-2003 at 10:11 AM.

  2. #32
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You cannot swap players this way. That is your main problem. You need to use strcpy or strncpy to copy the data from one to another.

    First off, why are you ever bothering with any parameters to any of your functions? You treat everything as a global variable, so why bother passing them as arguments?

    Furthermore, why don't you just use two player name arrays, and simply do as Walt said, and use an integer to toggle between the active player? Seriously, what's easier:

    Copying strings back and forth, or:
    Code:
    void swapplayer( void )
    {
        if( player == 1 )
            player = 2;
        else
            player = 1;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #33
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    OK finally no compiler errors!! but correct???

    I also attached the c file so u guys can look at it. Hi, have been writing a program for my comp science lab. I finaaly finished the program and there are no compiling errors. The program is a simple game. What i have to do is write the framework for the game without knowing any detailed information of the game. I am only given the function prototype for a function that executes a single round of a game as well as some other specifications for how the game should run. what im asking is, is my program doing everything it needs to be doing to be correct?? how can i check. when i first ran the program it asked for both players names correctly and the after i enter the second name it just goes to a new line. But the program is still running. What should i do. Please help.

    the prototype is:
    Pre-condition: name is the name of the current player. numlives
    is a reference to the variable storing the number of
    lives left of the current player. score is a reference
    to the variable storing the score of the current
    player, and round is the round the player is
    currently on.
    Post-conditions: A single round of the game will be executed. The
    player's score and number of lives will be
    adjusted accoridngly. The round will be
    completed unless the player has no more lives
    left.
    void playround(char name[], int* numlives, int* score, int round);

    Many games store a player's score and allow the player to have multiple lives. The final score of playing a game is the score the player attains at the point in time that they have no lives left. Furthermore, many of these games have several rounds or boards. If a player completes a round without losing a life, rather than the opponent getting a chance to play, that player gets to continue playing.
    You will execute a competition between two players in this mystery game. Before the game starts, ask each player their name and set their scores to 0 and their total number of lives to 5.

    After this, the game begins. Allow player 1 to go first. On a particular round, if a player does NOT lose any lives, then do NOT change the turn to the next player. Instead, allow the same player to continue their turn playing the next round.

    In the situation that the player does lose at least one life, the subsequent turn is taken by the other player. The only exception to either of these rules is if one player has 0 lives. If one player has 0 lives, then all turns will be given to his/her competitor till they lose all their lives.

    Your main goal will be to call the playround function when appropriate, control who's turn it is and quit the game and print out the final results of both players. You should print out a status report for each player after they finish a round. (Or if they do not succeed in finishing a round and end up with 0 lives while playing a round.)

    Note: This setup is DIFFERENT than standard video games where your opponent always gets to go after you lose a life. Here the change in turn is indicated by finishing a round in which you could lose more than one life. Furthermore, if you finish a round without losing a life, the turn doesn't change, but you DO have to make a new call to the playround function. It is also possible for you to finish a round, lose a life or more in the process, but not lose one at the end of the round. In this case, play advances to the other player if they have lives left.

    Example of Output to be produced by your code

    Since your code can only really be run in conjunction with a written playround function, the following example will only explicitly list the output that shouuld be produced by your code. Where the function should produce output, I will place the words "FUNCTION OUTPUT" in that location, followed by a description of the function's output. This does NOT indicate the literal output of the function. User input is indicated by bold italics.

    Player 1, what is your name?
    Sarah
    Player 2, what is your name?
    Billy
    FUNCTION OUTPUT FOR SARAH'S TURN
    Sarah, you now have 100 points and 4 lives left.
    FUNCTION OUTPUT FOR BILLY'S TURN
    Billy, you now have 80 points and 5 lives left.
    FUNCTION OUTPUT FOR BILLY'S TURN, SINCE HE DIDN'T LOSE
    ANY LIVES
    Billy, you now have 145 points and 3 lives left.
    FUNCTION OUTPUT FOR SARAH'S TURN
    Sarah, you now have 230 points and 4 lives left.
    FUNCTION OUTPUT FOR SARAH'S TURN, SINCE HE DIDN'T LOSE
    ANY LIVES
    Sarah, you now have 380 points and 3 lives left.
    FUNCTION OUTPUT FOR BILLY'S TURN
    Billy, you now have 245 points and 0 lives left.
    Your game is now over.
    FUNCTION OUTPUT FOR SARAH'S TURN
    Sarah, you now have 500 points and 2 lives left.
    FUNCTION OUTPUT FOR SARAH'S TURN, SINCE BILLY HAS NO LIVES LEFT.
    Sarah, you now have 700 points and 2 lives left.
    FUNCTION OUTPUT FOR SARAH'S TURN
    Sarah, you now have 825 points and 1 life left.
    FUNCTION OUTPUT FOR SARAH'S TURN
    Sarah, you now have 1000 points and 0 lives left.
    Here are the final standings:
    Sarah 1000 points
    Billy 245 points

    Notes: You do not have to follow the proper pluralization of the word 'life' as indicated in the example. You may format the status reports of the players' scores as you wish. The output above is only to give you a rough example of what's expected.

    heres the code::

    Code:
    #include <stdio.h>
    #include<string.h>
     
     
     char name[100] = {0};
     int numlives = 5;
     int score = 0;
     int round = 1;
     int playerlives[2] = {5,5}; 
     int playerturn[2] = {0,0};
     int playerscore[2] = {0,0};
     int currentplayer = 0;
     int templives;
     void swapplayers(void);
     void activeplayerstats(void);
     void playround(char name[], int* numlives, int*score, int round);
    
     int main() {
     
       char name1[100]= {0};
       char name2[100]= {0};
     
        printf("Player one, what is your name?\n");
             scanf("%s", name1);
        printf("Player two, what is your name?\n");
             scanf("%s", name2);
         strcpy(name,name1); 
         round = playerturn[currentplayer];
         numlives = playerlives[currentplayer]; 
         score = playerscore[currentplayer];  
     
       while (playerlives[0]>0 || playerlives[1]>0) 
         {
           if (numlives>0)
             {
              templives=numlives;  
       playround(name, &numlives, &score, round);
       activeplayerstats; 
             }
           if (numlives==0)
             {
              printf("Your Game is Over.\n");
              }
          
           
       swapplayers();
    
           
          }
       printf("Here are the final standings:\n");
           if(playerscore[0]>playerscore[1]) 
           {
              printf("%s      %d points\n", name1, playerscore[0]);
              printf("%s      %d points\n", name2, playerscore[1]);
           }
           else 
           {
              printf("%s      %d points\n", name2, playerscore[1]);
              printf("%s      %d points\n", name1, playerscore[0]);
            }
       return 0;              
     }
     
       void activeplayerstats(void)  {
           printf("%s, you now have %d points and %d lives left.\n", name,
                   score, numlives);
          }
       void swapplayers(void)  {
           if (numlives<templives) {
              if (currentplayer=0)   {
                  currentplayer=1;   }
              if (currentplayer=1)   {
                  currentplayer=0;   }
                                   }
                                }
       void playround(char name[], int *numlives, int *score, int round){}

  4. #34
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    like 2 errors now. PLEASE HELP

    ERRORS POSTED AT BOTTOM WHATS WRONG???? WHAT CAN I DO??



    Code:
    #include <stdio.h>
    #include<string.h>
    
     int playerlives[2] = {5,5};
     int round=1;
     int playerscore[2] = {0,0};
     int currentplayer = 0;
     int templives;
     void swapplayers();
     void activeplayerstats(char,int,int,int);
     void playround(char name[], int* numlives, int*score, int round);
    
     int main() {
    
    
       int numlives = 5;
       int score = 0;
       char playername[2][100]= {0};
       char name[100]= {0};
    
        printf("Player one, what is your name?\n");
             scanf("%s", name[0]);
        printf("Player two, what is your name?\n");
             scanf("%s", name[1]);
         strcpy(name,playername[currentplayer]);
         numlives = playerlives[currentplayer];
         score = playerscore[currentplayer];
    while (numlives>0)
         {
          templives=numlives;
       playround(name, &numlives, &score, round);
         round+=1;
       activeplayerstats(name[currentplayer], score, numlives,currentplayer);
    
           if (numlives==0)
             {
              printf("Your Game is Over.\n");
             }
            swapplayers();
         }
       printf("Here are the final standings:\n");
           if(playerscore[0]>playerscore[1])
           {
              printf("%s      %d points\n", name[0], playerscore[0]);
              printf("%s      %d points\n", name[1], playerscore[1]);
           }
           else
           {
              printf("%s      %d points\n", name[1], playerscore[1]);
              printf("%s      %d points\n", name[0], playerscore[0]);
           }
       return 0;
     }
    
       void activeplayerstats( char playername[], int score[currentplayer],
                              int numlives, int currentplayer)  {
           printf("%s, you now have %d points and %d lives left.\n",
           playername[currentplayer],score[currentplayer],
            numlives[currentplayer]);
          }
       void swapplayers(int numlives,int templives, int currentplayer)  {
           if (numlives<templives) {
              if (currentplayer==0)   {
                  currentplayer=1;   }
              if (currentplayer==1)   {
                  currentplayer=0;   }
                                   }
                                }
       void playround(char name[], int *numlives, int *score, int round){
    game3.c:65: conflicting types for `activeplayerstats'
    game3.c:18: previous declaration of `activeplayerstats'
    game3.c: In function `activeplayerstats':
    game3.c:68: subscripted value is neither array nor pointer ]


    I USe GCC compiler
    Last edited by stehigs321; 10-30-2003 at 08:46 PM.

  5. #35
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void activeplayerstats(char,int,int,int);
    ... 
    void activeplayerstats( char playername[], int score[currentplayer], int numlives, int currentplayer)
    See the problem?

    Next...
    Code:
    playername[currentplayer]
    Is wrong, since you pass the function:
    Code:
    activeplayerstats(name[currentplayer], score, numlives,currentplayer);
    And you've got name declared as:
    Code:
    char name[100]= {0};
    You should use a 2d array there instead for what you're intending.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #36
    Registered User
    Join Date
    Sep 2003
    Posts
    56
    I have no idea of what you are telling me to do?? I kinda of understand but i dont see the problem. could you show me the cde to use?? Really. i wont get it. I got the majority of the code. please help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM