Thread: Table spacing problem while using strings

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    8

    Table spacing problem while using strings

    When I run the program, choose create character and start the battle, the spacing in the table goes wrong. Any ideas on how to fix this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef char string20[21];
    
    
    void LapuLapu()
    {
      
      printf("Lapu-Lapu excels in close combat fights.\n");
      printf("HP = 1000 MP = 500\n");
      printf("Skills: Sipa't Sapak  -  Makiling Slash  -  Luzon Whirlwind  -  Boat Ride\n");
      printf("\n");
    }
    void Polgas()
    {
      printf("Polgas prefers attacking from a distance and deal great damage with arrows.\n");
      printf("HP = 800 MP = 600\n");
      printf("Skils: Dog Shot  -  Pugad Baboy Shot  -  Doggy Arrow  -  Polgas Special\n");
      printf("\n");
    }
    void JoseRizal()
    {
    printf("Jose Rizal specializes in the way of magic to defeat the enemy.\n");
    printf("HP = 600 MP = 1000\n");
    printf("Skills: Tsinelas Throw  -  Maria Clara Beam  -  El Fili Blast  -  Padre Salvi Shot\t\n");
    }
    
    
    void LapuLapuTable()
    {
         printf("-------------------------------------------------------\n");
         printf("| Skill           | Button |      Damage       |  MP  |\n");
         printf("-------------------------------------------------------\n");
         printf("| Sipa't Sapak    |   A    |      70-90        |  10  |\n");
         printf("| Makiling Slash  |   B    |      50-70        |  15  |\n");
         printf("| Luzon Whirlwind |   C    |      80-100       |  20  |\n");
         printf("| Bangka Slam     |  any   |     100-120       |  25  |\n");
         printf("-------------------------------------------------------\n");
    }
    
    
    void PolgasTable()
    {
         printf("-------------------------------------------------------\n");
         printf("| Skill           | Button |   Damage          |  MP  |\n");
         printf("-------------------------------------------------------\n");
         printf("| Dog Shot        |   A    |      70-90        |  10  |\n");
         printf("| Pugad Baboy Shot|   B    |      80-100       |  15  |\n");
         printf("| Doggy Arrow     |   C    |     100-120       |  20  |\n");
         printf("| Polgas Special  |  any   |     130-150       |  25  |\n");
         printf("-------------------------------------------------------\n");
    }
    
    
    void JoseRizalTable()
    {
         printf("-------------------------------------------------------\n");
         printf("| Skill           | Button |      Damage       |  MP  |\n");
         printf("-------------------------------------------------------\n");
         printf("| Tsinelas Throw  |   A    |      70-90        |  10  |\n");
         printf("| Maria Clara Beam|   B    |      65-85        |  15  |\n");
         printf("| El Fili Blast   |   C    |      80-100       |  20  |\n");
         printf("| Padre Salvi Shot|  any   |     100-130       |  25  |\n");
         printf("-------------------------------------------------------\n");
    }
    
    
    int RandInt(int a,int b)            
    {
       return a + rand() % (b - a + 1);
    }
    
    
    int main()
    {
    int hp1, hp2, mp1, mp2, mode, sa1, sa2, sa3, sa4, sb1, sb2, sb3, sb4, newhp, newmp, newskill1, newskill2, newskill3, newskill4, MP1, MP2,MP3, MP4,mpa, mpb, mpc, mpd;
    char Player1, Player2, p1, p2;
    char newname[21];
    string20 ns1, ns2, ns3, ns4;
    
    
    
    
    
    
    
    
    srand(time(NULL));
    
    
    do{
    printf("Good day! Please select a mode: \n1=Preview Lapu-Lapu\n2=Preview Polgas\n3=Preview Jose Rizal\n4=Create Character\n5=Start Battle\n");
    
    
    
    
    printf("Mode:");
    scanf("%d", &mode);
    
    
    if(mode==1)
      LapuLapu();
      else if(mode == 2)
      Polgas();
      else if(mode == 3)
      JoseRizal();
      else if(mode == 4) 
      {
    
    
            fflush(stdin);
            printf("Input new character name: ");
            fgets(newname, sizeof(char) * 21, stdin);
            newname[strlen(newname)-1] = '\0';
           fflush(stdin);
       
           printf("Input HP: ");
           scanf("%d", &newhp);
           printf("\n");
           
           printf("Input MP: ");
           scanf("%d", &newmp);
           printf("\n");
       
             fflush(stdin);
           printf("Input 1st skill name: ");
           fgets(ns1, sizeof(string20), stdin);
           ns1[strlen(ns1)-1] = '\0';
           fflush(stdin);
           printf("Input damage of %s: ", ns1);
           scanf("%d", &newskill1);
       
          fflush(stdin);
           printf("\nInput 2nd skill name: ");
           fgets(ns2, sizeof(string20), stdin);
           ns2[strlen(ns2)-1] = '\0';
           printf("Input damage of %s: ", ns2);
           scanf("%d", &newskill2);
       
           fflush(stdin);
           printf("\nInput 3rd skill name: ");
           fgets(ns3, sizeof(string20), stdin);
           ns3[strlen(ns3)-1] = '\0';
           printf("Input damage of %s: ", ns3);
           scanf("%d", &newskill3);
       
       
           fflush(stdin);
           printf("\nInput 4th skill name: ");
           fgets(ns4, sizeof(string20), stdin);
           ns4[strlen(ns4)-1] = '\0';
           printf("Input damage of %s: ", ns4);
           scanf("%d", &newskill4);
       }
       
      else if(mode == 5)
      {
           system("cls");
           printf("Lets Begin!!!\n");
           printf("Player 1 please choose your hero: Lapu-Lapu = A, Polgas = B, Jose Rizal = C, D = Created Char \n");
           scanf("%c", &Player1);
           scanf("%c", &Player1);
    
    
           if(Player1 == 'A' || Player1 == 'a')
           {
                hp1 = 1000;
                mp1= 500;
                sa1 = RandInt(70,90);  
                MP1 = 10;
                sa2 = RandInt (50,70); 
                MP2 = 15;
                sa3 = RandInt(80,100); 
                MP3 = 20; 
                sa4 = RandInt(100,120); 
                MP4 = 25;
                printf("Player 1 chose LAPU-LAPU!\n");
           }
           else if(Player1 == 'B' || Player1 == 'b')
           {
                hp1 = 800;
                mp1 = 500;
                sa1 = RandInt(70,90);  
                MP1 = 10;
                sa2 = RandInt(80,100);  
                MP2 = 15;
                sa3 = RandInt(100,120);
                MP3 = 20;
                sa4 = RandInt(130,150); 
                MP4 = 25;
                printf("Player 1 chose POLGAS!\n");
           }
           else if(Player1 == 'C' || Player1 == 'c')
           {
                hp1 = 900;
                mp1 = 500;
                sa1 = RandInt(70,90); MP1 = 10;
                sa2 = RandInt(65,85); MP2 = 15;
                sa3 = RandInt(80,100); MP3 = 20;
                sa4 = RandInt(100,130); MP4 = 25;
                printf("Player1 chose JOSE RIZAL!\n");
           }
           else if(Player1 == 'D' || Player1 == 'd')
           {
            hp1 = newhp;
            mp1 = newmp;
            sa1 = newskill1; 
                MP1 = 10;
            sa2 = newskill2;
                MP2 = 15;
            sa3 = newskill3; 
                MP3 = 20;
            sa4 = newskill4; 
                MP4 = 25;
             printf("Player 1 chose %s!\n", newname);
           }
     
           printf("Player 2 please choose your class: Lapu-Lapu = A, Polgas = B, Jose Rizal = C \n");
           scanf("%c", &Player2);
           scanf("%c", &Player2);
    
    
           if(Player2 == 'A' || Player2 == 'a')
           {
                 hp2 = 1000;
                 mp2 = 500;
                 sb1 = RandInt(70,90); 
                 mpa = 10;
                 sb2 = RandInt(50,70); 
                 mpb = 15;
                 sb3 = RandInt(80,100);
             mpc = 20;
                 sb4 = RandInt(100,120); 
             mpd = 25;
                 printf("Player 2 chose LAPU-LAPU!\n");
           }
    
    
           else if(Player2 == 'B' || Player2 == 'b')
           {
                hp2 = 800;
                mp2 = 500;
                sb1 = RandInt(70,90);  
            mpa = 10;
                sb2 = RandInt(80,100); 
            mpb = 15;
                sb3 = RandInt(100,120); 
            mpc = 20;
                sb4 = RandInt(130,150); 
            mpd = 25;
                printf("Player 2 chose POLGAS!\n");
           }
           else if(Player2 == 'C' || Player2 == 'c')
           {
                hp2 = 900;
                mp2 = 500;
                sb1 = RandInt(70,90); 
            mpa = 10;
                sb2 = RandInt(65,85);
            mpb = 15;
                sb3 = RandInt(80,100); 
            mpc = 20;
                sb4 = RandInt(100,130); 
            mpd = 25;
                printf("Player 2 chose JOSE RIZAL!\n");
           }
           else if(Player2 == 'D' || Player2 == 'd')
           {
            hp2 = newhp;
            mp2 = newmp;
            sb1 = newskill1; 
            mpa = 10;
            sb2 = newskill2; 
            mpb = 15;
            sb3 = newskill3; 
            mpc = 20;
            sb4 = newskill4; 
            mpd = 25;
             printf("Player 2 chose %s!\n", newname);
           }
                                                              
           printf("Start Battle!\n\n"); 
           do {
    
    
           if(Player1 == 'A' || Player1 == 'a')
           {    
           printf("\nPlayer 1 Status\n");
           printf("HP = %d MP = %d\n", hp1, mp1);
           LapuLapuTable();
           }
           
           else if(Player1 == 'B' || Player1 == 'b')
           {
           printf("\nPlayer 1 Status\n");
           printf("HP = %d MP = %d\n", hp1, mp1);
           PolgasTable();
           }
           else if(Player1 == 'C' || Player1 == 'c')
           {
              printf("\nPlayer 1 Status\n");
              printf("HP = %d MP = %d\n", hp1, mp1);
              JoseRizalTable();
           }
           else if(Player1 == 'd' || Player1 == 'D')
           {
        printf("Player1 Status\n");
            printf("HP = %d MP = %d\n", hp1, mp1);
            printf("-----------------------------------------------------------\n");
            printf("| Skill                   |  Button   |      Damage       |  MP  |\n");
            printf("-----------------------------------------------------------\n");
            printf("| %s                      |     A     |       %d          |  10  |\n", ns1, newskill1);
            printf("| %s                      |     B     |       %d          |  15  |\n", ns2, newskill2);
            printf("| %s                      |     C     |       %d          |  20  |\n", ns3, newskill3);
            printf("| %s                      |    any    |       %d          |  25  |\n", ns4, newskill4);
            printf("-----------------------------------------------------------\n");
           }
    
    
    
    
    if(sa1 < hp2 && sa2 < hp2 && sa3 <hp2)
    {
    printf("Player 1 please choose a skill: ");
    scanf("%c", &p1);
    scanf("%c", &p1);
    
    
    
    
      if(p1 == 'A' || p1 == 'a') 
        {
         hp2 -= sa1; 
         mp1 -= MP1;
        }
      else if(p1 == 'B' || p1 == 'b') 
       {
        hp2 -= sa2; 
        mp1 -= MP2;
       }
      else if(p1 == 'C' || p1 == 'c') 
       { 
        hp2 -= sa3; 
        mp1 -= MP3;
       }
      else 
        hp2 -= sa4;
        mp1 -= MP4;
    }
    else
    {
    hp2 = 0;
    printf("\nREMAINING HP OF PLAYER 2 = %d\n", hp2);
    printf("\nREMAINING MP OF PLAYER 2 = %d\n", mp2);
    }
    
    
    if(Player2 == 'A' || Player2 == 'a')
    {    
    printf("\nPlayer 2 Status");
    printf("\nHP = %d MP = %d\n", hp2, mp2);
    LapuLapuTable();
    }
    else if(Player2 == 'B' || Player2 == 'b')
    {
    printf("\nPlayer 2 Status");
    printf("\nHP = %d MP = %d\n", hp2, mp2);
    PolgasTable();
    }
    else if(Player2 == 'C' || Player2 == 'c')
    {
    printf("\nPlayer 2 Status");
    printf("\nHP = %d MP = %d\n", hp2, mp2);
    JoseRizalTable();
    }
     else if(Player1 == 'd' || Player1 == 'D')
    {
      printf("Player1 Status\n");
      printf("HP = %d MP = %d\n", hp1, mp1);
      printf("-------------------------------------------------------\n");
      printf("| Skill           | Button |      Damage       |  MP  |\n");
      printf("-------------------------------------------------------\n");
      printf("| %s              |   A    |       %d          |  10  |\n", ns1, newskill1);
      printf("| %s              |   B    |       %d          |  15  |\n", ns2, newskill2);
      printf("| %s              |   C    |       %d          |  20  |\n", ns3, newskill3);
      printf("| %s              |  any   |       %d          |  25  |\n", ns4, newskill4);
      printf("-------------------------------------------------------\n");
    }
    
    
    
    
    if(sb1 < hp1 && sb2 < hp1 && sb3 <hp1){
    printf("Player 2 please choose a skill: ");
    scanf("%c", &p2);
    scanf("%c", &p2);
    
    
    if(p2 == 'A' || p2 == 'a') 
     {
      hp1 -= sb1; 
      mp2 -= mpa;
     }
    else if(p2 == 'B' || p2 == 'b') 
     {
      hp1 -= sb2;
      mp2 -= mpb; 
     }
    else if(p2 == 'C' || p2 == 'c') 
     {
      hp1 -= sb3;
      mp2 -= mpc; 
     }
    else 
      hp1 -= sb4; 
      mp2 -= mpd;
    
    
    }
    else
    if(hp1 < 0)
    hp1=0;
    printf("\nREMAINING HP OF PLAYER 1 = %d\n", hp1);
    
    
    }
    while(hp1 > 0 && hp2 > 0); 
    
    
    if(hp1 <= 0)
    {
    printf("Player 2 wins!\n");
    }
           
    else if(hp2 <= 0)
    {
    printf("Player 1 wins!\n");
    }
    }
      
    }while(mode !=6);
    system("pause");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    8
    In the program, when the "create a character" option is selected, the skill name and damage, HP, MP and character name is stored using strings. However, when I try to display it on a table, the spacing goes wrong.

    Table spacing problem while using strings-lol-gifTable spacing problem while using strings-lol2-gif
    Last edited by hyetoch; 12-08-2011 at 05:03 AM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Your %s and %d format specifiers will output whatever length the arguments dictate. If you want to make the width more predictable you should look into using %20s or %-20s for example to specify 20 characters.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by hyetoch View Post
    When I run the program, choose create character and start the battle, the spacing in the table goes wrong. Any ideas on how to fix this?
    Code:
            printf("-----------------------------------------------------------\n");
            printf("| Skill                   |  Button   |      Damage       |  MP  |\n");
            printf("-----------------------------------------------------------\n");
            printf("| %s                      |     A     |       %d          |  10  |\n", ns1, newskill1);
            printf("| %s                      |     B     |       %d          |  15  |\n", ns2, newskill2);
            printf("| %s                      |     C     |       %d          |  20  |\n", ns3, newskill3);
            printf("| %s                      |    any    |       %d          |  25  |\n", ns4, newskill4);
            printf("-----------------------------------------------------------\n");
    Your problem here is pretty simple... the length of the strings is pushing all those blank spaces adhead displacing the characters behing them.

    You need to make your string areas fixed length... like this...
    Code:
    printf("| %24s|     A     |       %4d      |  10  |\n", ns1, newskill1);
    The width specifiers will make things line up, unless your string is longer than 24 characters or your number is longer than 4 digits.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1
    Code:
    fflush(stdin);
    Read: Why fflush(stdin) is wrong

    Your need to use this comes from the mixing of scanf and fgets. All of your scanf calls leave stray newline characters in the input buffer which messes up any subsequent calls to fgets. You can deal with this several ways, one of which would be to have the scanf consume and throw away the newlines before you get to the fgets function calls. One simple way to do this is to change this (as an example):
    Code:
    scanf("%d", &mode);
    To this:
    Code:
    scanf("%d%*c", &mode);
    This will throw away the newline at the end of your input and make the fgets calls work as they should without having to resort to an undefined call to fflush(stdin).





    #2
    Code:
    char newname[21];
    
    ...
    
    fgets(newname, sizeof(char) * 21, stdin);
    sizeof(char) is guaranteed to always be 1. 1 multiplied by Z is just Z, so things like this should really just simply be:
    Code:
    char newname[21];
    
    ...
    
    fgets(newname, 21, stdin);
    That said however, you really need to be using the size of the buffer you are reading into and not a magic number. Therefore it should really be:
    Code:
    char newname[21];
    
    ...
    
    fgets(newname, sizeof(newname), stdin);
    ... and similarly for all those string20 variable you are reading into in other parts of your code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lookup table with different spacing between entries
    By Boksha in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2008, 02:40 PM
  2. Dev C++ Spacing Problem
    By Darklighter137 in forum C++ Programming
    Replies: 7
    Last Post: 11-13-2006, 10:48 AM
  3. Output spacing problem
    By boxsterh in forum C++ Programming
    Replies: 4
    Last Post: 02-16-2006, 04:24 PM
  4. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  5. saving strings to a table
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-14-2002, 11:47 AM