Thread: character strings

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    99

    character strings

    hey everyone i kinda need help with this program i wrote today. i am trying to get the names of 8 game characters off the user, then i ask what position they came in using switch statement to assign a certain amount of points to whatever position they came in and finally print to the screen their total points after 4 games.
    note: the last line only includes one game character because i didnt want to bother writing 8 print statements until i was sure the program would work. the only thing wrong with the program is character string declaration. im not sure how to declare a,b,c,d,e,f,g,h as char strings . PLEASE HELP! AND ALSO IF YOU HAVE ANY TIPS OR TRICKS TO SHORTEN THE PROGRAM THEN I WILL HONOURABLY ACCEPT. THANKS

    Code:
    #include<stdio.h>
    Code:
    int main()
    {
    char stringa[20], stringb[20], stringc[20], stringd[20], stringe[20], stringf[20], stringg[20], stringh[20];
    int place,i;
    printf("Enter the ctr characters\n");
    scanf("%s%s%s%s%s%s%s%s",&a,&b,&c,&d,&e,&f,&g,&h);
    
    
    for(i=1;i<5;i++)
    {
    printf("where did %s come?\n",a);
    scanf("%d",&place);
    
    switch(place)
    {
    case '1':
    a = a+9;
    break;
    
    case '2':
    a = a + 7;
    break;
    
    case '3':
    a = a+6;
    break;
    
    case '4':
    a = a + 5;
    break;
    
    case '5':
    a = a+4;
    break;
    
    case '6':
    a = a + 2;
    break;
    
    case '7':
    a = a+1;
    break;
    
    case '8':
    a = a + 0;
    break;
    }
    printf("where did %s come?\n",b);
    scanf("%d",&place);
    
    switch(place)
    {
    case '1':
    b = b+9;
    break;
    
    case '2':
    b = b + 7;
    break;
    
    case '3':
    b = b+6;
    break;
    
    case '4':
    b = b + 5;
    break;
    
    case '5':
    b = b+4;
    break;
    
    case '6':
    b = b + 2;
    break;
    
    case '7':
    b = b+1;
    break;
    
    case '8':
    b = b + 0;
    break;
    }
    printf("where did %s come?\n",c);
    scanf("%d",&place);
    
    switch(place)
    {
    case '1':
    c = c+9;
    break;
    
    case '2':
    c = c + 7;
    break;
    
    case '3':
    c = c+6;
    break;
    
    case '4':
    c = c + 5;
    break;
    
    case '5':
    c = c+4;
    break;
    
    case '6':
    c = c + 2;
    break;
    
    case '7':
    c = c+1;
    break;
    
    case '8':
    c = c + 0;
    break;
    }
    printf("where did %s come?\n",d);
    scanf("%d",&place);
    
    switch(place)
    {
    case '1':
    d = d+9;
    break;
    
    case '2':
    d = d + 7;
    break;
    
    case '3':
    d = d+6;
    break;
    
    case '4':
    d = d + 5;
    break;
    
    case '5':
    d = d+4;
    break;
    
    case '6':
    d = d + 2;
    break;
    
    case '7':
    d = d+1;
    break;
    
    case '8':
    d = d + 0;
    break;
    }
    printf("where did %s come?\n",e);
    scanf("%d",&place);
    
    switch(place)
    {
    case '1':
    e = e+9;
    break;
    
    case '2':
    e = e + 7;
    break;
    
    case '3':
    e = e+6;
    break;
    
    case '4':
    e = e + 5;
    break;
    
    case '5':
    e = e+4;
    break;
    
    case '6':
    e = e + 2;
    break;
    
    case '7':
    e = e+1;
    break;
    
    case '8':
    e = e + 0;
    break;
    }
    printf("where did %s come?\n",f);
    scanf("%d",&place);
    
    switch(place)
    {
    case '1':
    f = f+9;
    break;
    
    case '2':
    f = f + 7;
    break;
    
    case '3':
    f = f+6;
    break;
    
    case '4':
    f = f + 5;
    break;
    
    case '5':
    f = f+4;
    break;
    
    case '6':
    f = f + 2;
    break;
    
    case '7':
    f = f+1;
    break;
    
    case '8':
    f = f + 0;
    break;
    }
    printf("where did %s come?\n",g);
    scanf("%d",&place);
    
    switch(place)
    {
    case '1':
    g = g+9;
    break;
    
    case '2':
    g = g + 7;
    break;
    
    case '3':
    g = g+6;
    break;
    
    case '4':
    g = g + 5;
    break;
    
    case '5':
    g = g+4;
    break;
    
    case '6':
    g = g + 2;
    break;
    
    case '7':
    g = g+1;
    break;
    
    case '8':
    g = g + 0;
    break;
    }
    printf("where did %s come?\n",h);
    scanf("%d",&place);
    
    switch(place)
    {
    case '1':
    h = h+9;
    break;
    
    case '2':
    h = h + 7;
    break;
    
    case '3':
    h = h+6;
    break;
    
    case '4':
    h = h + 5;
    break;
    
    case '5':
    h = h+4;
    break;
    
    case '6':
    h = h + 2;
    break;
    
    case '7':
    h = h+1;
    break;
    
    case '8':
    h = h + 0;
    break;
    }
    }
    printf("%s has a total of %d points\n",a,place);
    return(0);
    }
    

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Make the switch statement its own function, and just call it (and pass a, b, or h) whenever you need to use it.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    Quote Originally Posted by memcpy View Post
    Make the switch statement its own function, and just call it (and pass a, b, or h) whenever you need to use it.
    hey thanks but im not quite sure what u mean by make the switch statement its own function. see when i want to ask them what position the character came in i have to use scanf but i have to read in the integer so therefore how do i collect the integer and put that into a switch statement, assign points to the character according to the position they came in and then add the points to their current score.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Another thing you can do to make the program shorter and more elegant (assuming you know how arrays work) would be something like
    Code:
    int increment[9] = { 0, 9, 7, 6, 5, 4, 2, 1, 0 };
    /* other code */
    scanf("%d",&place);
    h = h + increment[place];
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    Quote Originally Posted by TheBigH View Post
    Another thing you can do to make the program shorter and more elegant (assuming you know how arrays work) would be something like
    Code:
    int increment[9] = { 0, 9, 7, 6, 5, 4, 2, 1, 0 };
    /* other code */
    scanf("%d",&place);
    h = h + increment[place];
    cheers ill try that. not sure what the asleep in while(!asleep) means or sheep++; but thanks very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character Arrays and Strings?
    By ocswimfast in forum C Programming
    Replies: 4
    Last Post: 09-15-2010, 10:44 AM
  2. gibrish when copying strings character by character
    By captain-cat in forum C Programming
    Replies: 2
    Last Post: 07-23-2004, 07:48 AM
  3. Character strings
    By campermama in forum C++ Programming
    Replies: 1
    Last Post: 06-17-2004, 04:26 PM
  4. character strings
    By linucksrox in forum C Programming
    Replies: 6
    Last Post: 06-01-2004, 12:24 PM
  5. character strings
    By yankee888 in forum C Programming
    Replies: 4
    Last Post: 01-27-2002, 04:06 PM

Tags for this Thread