Thread: I need some quick help with code.

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

    What i have

    Code:
    #include <stdio.h>
    
    int numlives = 5;
    int score = 0;
    int round = 1;
    
    int main(void) {
    
      char name1[100]= {0};
      char name2[100]= {0};
    
       printf("Player one, what is your name?\n"); 
       scanf("%s", name1[100]);
       printf("Player two, what is your name?\n");
       scanf("%s", name2[100]);
    Heres what i have so far. I dont think it works though. How do i print the names once they are stored in the array? I tried it and it printed them incorrectly.

    is this how i would print the names??

    Code:
    printf("%s", name1[100]);
    printf("%s", name2[100]);
    Last edited by stehigs321; 10-27-2003 at 05:32 PM.

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    
    int numlives = 5;
    int score = 0;
    int round = 1;
    
    int main(void)
    {
      char  name1[100];
      char  name2[100];
    
      printf("Player one, what is your name?\n");
      scanf("%s", name1);
      printf("Player two, what is your name?\n");
      scanf("%s", name2);
    
      printf("%s\n", name1);
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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

    so far.. hows it look

    Heres what i have so far thanks to all of your guys help. thanks again. This is how far i am. The things i need help with are labeled with ************* please help me with anythig you can You can see the problem in the first page of this posting.
    thanks again




    Code:
    #include <stdio.h>
    
    char name[100] = {0};
    int numlives = 5;
    int score = 0;
    int round = 1;
    int player1live = 5;
    int player1turn = 0;
    int player1score = 0;
    int player2lives = 5;
    int player2turn = 0;
    int player2score = 0;
    int templives
    
    void activeplayerstats(void);
    
    int main(void) {
    
      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);
        name = name1;  **************************
        round = player1turn;  ************************
        numlives = player1lives;  ************************
        score = player1score;  *************************
    
      while (player1lives>0 || player2lives>0);
        {
          if (numlives>0);
            {
             templives=numlives;  *********************
        ****CALL playround(char name[], int* numlives, int* score, int round);
            }
          if (numlives=0)
    
             printf("Your Game is Over.\n");
    
      void activeplayerstats(void);
          if (numlives < templives)
    
             switch to other player  ********************
    
         }
      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);
          else {
    }
    
      void activeplayerstats(void)  {
          printf("%s, you now have %d points and %d lives left.\n", name,
                  score, numlives);

  4. #19
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This is still wrong:
    >>scanf("%s", &name2);
    see my example.


    For this:
    >>name = name1;
    use strcpy().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #20
    Registered User
    Join Date
    Sep 2003
    Posts
    56
    I still have no idea how to use these things please help me.
    i think im only allowed to use <stdio.h>.
    unless its impossible to do this program wiyth just that.

  6. #21
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Using strcpy :
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char source[] = "my name";
      char target[100];
      strcpy (target, source);
      puts (source);
      puts (target);
    
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #22
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    what is the puts??

    what do u mean puts???

  8. #23
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: what is the puts??

    Originally posted by stehigs321
    what do u mean puts???
    http://www.rt.com/man/puts.3.html
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #24
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    I have no idea

    I have no idea what any of that means. Could someone plese just look at the code i posted and there are only like 7 lines at most that i need help with. Please shoe me what i need to do. thanks so much. Its almost done. then i can stop pestering you guys. hehe jk

  10. #25
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: I have no idea

    >>I have no idea what any of that means.
    puts() is simply a function that writes to the screen. Its the similar to using printf ("%s\n", somestringvar);

    >>there are only like 7 lines at most that i need help with.
    Fix the ones I have helped you with so far.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #26
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    update

    OK i have this now please help me on how to switch to player 2?
    And any other errors you see. Thanks so much.
    Code:
    #include <stdio.h>
    
    char name[100] = {0};
    int numlives = 5;
    int score = 0;
    int round = 1;
    int player1live = 5;
    int player1turn = 0;
    int player1score = 0;
    int player2lives = 5;
    int player2turn = 0;
    int player2score = 0;
    int templives
    
    void activeplayerstats(void);
    
    int main(void) {
    
      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(name1, name)
        round = player1turn;  
        numlives = player1lives; 
        score = player1score; 
    
      while (player1lives>0 || player2lives>0);
        {
          if (numlives>0);
            {
             templives=numlives; 
    
        ****CALL playround(char name[], int* numlives, int* score, int round);
    
            }
          if (numlives=0)
    
             printf("Your Game is Over.\n");
    
      void activeplayerstats(void);
          if (numlives < templives)
    
             switch to other player  ********************
    
         }
      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);
          else {
    }
    
      void activeplayerstats(void)  {
          printf("%s, you now have %d points and %d lives left.\n", name,
                  score, numlives);

  12. #27
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    There are a few syntax errors...

    int templives - missing semi-colon.

    scanf( "%s", &name1 ); - remove &.
    scanf( "%s", &name2 ); - remove &.
    (name1 and name2 are arrays of char, which can be passed to scanf directly).

    strcpy( name1, name ); - should be the other way around, the second variable is copied into the first...

    if(numlives=0) - should be if(numlives==0), '=' sets the value of the right hand variable to be the same as the value of the left, '==' checks to see if the variables are the same value. I fell for this bug lots of times when I first started.

    if / else / else statement is not properly bracketed. should be

    Code:
    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);
    }
    (can't see what the last else is for... )

    printf( "%s %d points... - better as printf( "%s\t\t%d points... \t inserts a tab into the printout.
    There is no such thing as a humble opinion

  13. #28
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    Oh yeah... and

    if(numlives>0);

    is wrong too - lose the semi-colon.
    There is no such thing as a humble opinion

  14. #29
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    OK new update!!

    Heres what i have almost finished!!
    How do i switch from player 1 to player two after player one's turn. then switch back when player 2 is done??
    Please help me figure this out guys. I put a spot where i think the code belongs. its labeled with **********
    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 activeplayerstats(void);
     
     int main(void) {
     
       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(char name[], int* numlives, int* score, int round); 
             }
           if (numlives=0)
     
              printf("Your Game is Over.\n");
     
       activeplayerstats();   // See below. 
           if (numlives < templives) {
     
              switch to other player  ************
           ;}
          }
       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);

  15. #30
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    To make it easy to switch from player 1 to 2, you should put all the variables that deal with each player in an array.

    Instead of defining:
    Code:
    int player1lives = 5; 
    int player1turn = 0;
    int player1score = 0;
    int player2lives = 5;
    int player2turn = 0;
    int player2score = 0;
    change the deinitions to:
    Code:
    int playerlives[2] = {5,5}; 
    int playerturn[2] = {0,0};
    int playerscore[2] = {0,0};
    int currentPlayer;
    This way when currentPlayer is 0, you reference the lives of player 1 with playerlives[currentPlayer]

    change to player 2 by setting currentPlayer to 1

    Also, with all the changes you've made, you should really compile and test your code AS you write it, don't wait until you finish the program and try to fix all the syntax errors.
    Add a small piece at a time, compile, repeat.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

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