Character string help

This is a discussion on Character string help within the C Programming forums, part of the General Programming Boards category; I cant seem to get this to work. Keep getting weird output. No clue how to fix it. Can someone ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    13

    Character string help

    I cant seem to get this to work. Keep getting weird output. No clue how to fix it. Can someone help me please?


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    main()
    {
          char name[10];
          char name1;
          int j,score,sum,avg,ducknum,abvavg;
          
         
          int a[5];
          abvavg = 0;
          ducknum = 0;
          sum = 0;
          for(j = 1; j < 3; j++)
          {
              printf("Please enter the name\n");
              scanf("%s",&name1);
              printf("Enter score\n");
              scanf("%d",&score);
              name[j] = name1;
              a[j] = score;
              sum = sum + score;
          }
          avg = sum / 4;
          for(j = 1; j < 5; j++)
              {
              
               if(a[j] > avg)
               {
                abvavg = abvavg + 1;
               }
    		   if(a[j] == 0)
    		   {
                ducknum = ducknum + 1;
               }        
               if(a[j] > 50)
               {
                printf("%c hit above 50 with a score of %d\n",name[j],a[j]);
                } 
              }
            printf("The amount of batsmen who made a duck is:%d\n",ducknum);
            printf("The amount of batsmen who scored above the team average is:%d\n",abvavg);
            printf("The average is:%d\n",avg);
            getch();
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    name1 is just a char, not a char array[]. You can't fit an entire name into a one char variable. The printf() for name1 should be %s, after you make name1, into name1[20].

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Also note that you are performing integer arithmetic here:
    Code:
     avg = sum / 4;
    so it is a certain you will get an integer result.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Also remember that arrays start at position '0'. In your code, a[j] (and the names when you fix them) will skip the first place and start at the second position (i.e. '1'). Just worth bearing it in mind.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by key4life View Post
    I cant seem to get this to work. Keep getting weird output. No clue how to fix it. Can someone help me please?


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    main()
    {
          char name[10];
          char name1;
          int j,score,sum,avg,ducknum,abvavg;
          
         
          int a[5];
          abvavg = 0;
          ducknum = 0;
          sum = 0;
          for(j = 1; j < 3; j++)
          {
              printf("Please enter the name\n");
              scanf("%s",&name1);
              printf("Enter score\n");
              scanf("%d",&score);
              name[j] = name1;
              a[j] = score;
              sum = sum + score;
          }
          avg = sum / 4;
          for(j = 1; j < 5; j++)
              {
              
               if(a[j] > avg)
               {
                abvavg = abvavg + 1;
               }
    		   if(a[j] == 0)
    		   {
                ducknum = ducknum + 1;
               }        
               if(a[j] > 50)
               {
                printf("%c hit above 50 with a score of %d\n",name[j],a[j]);
                } 
              }
            printf("The amount of batsmen who made a duck is:%d\n",ducknum);
            printf("The amount of batsmen who scored above the team average is:%d\n",abvavg);
            printf("The average is:%d\n",avg);
            getch();
    }
    main should be written like
    Code:
    int main(void)
    {
    //do anything
    return 0;
    }
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    13

    More Help

    Thanks alot guys. You helped me to correctly output the names. But now the program seems to output the name that was entered last with the correct score even if they're not associated with each other.

    Example, I could input the following:

    John - 54
    Max - 32
    Luke - 12
    Dan - 23

    The program will output
    Dan scored above 50 with a score of 54.

    John should have been outputted. Can someone help with this please?

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    int main(void)
    {
          char name[10];
          char name1[20];
          
          int j,score,sum,avg,ducknum,abvavg;        
          int a[5];
          
          abvavg = 0;
          ducknum = 0;
          sum = 0;
          
          for(j = 0; j < 3; j++)
          
          {
              printf("Please enter the name\n");
              scanf("%s",&name1);
              
              printf("Enter score\n");
              scanf("%d",&score);
              name[j] = name1;
              a[j] = score;
              sum = sum + score;
          }
          
          avg = sum / 4;
          for(j = 0; j < 3; j++)
              {
              
               if(a[j] > avg)
               {
                abvavg = abvavg + 1;
                }
    		   if(a[j] == 0)
    		   {
                ducknum = ducknum + 1;
                }       
               if(a[j] > 50)
               {
                printf("%s hit above 50 with a score of %d\n",name1,a[j]);
                }
              }
            printf("The amount of batsmen who made a duck is:%d\n",ducknum);
            printf("The amount of batsmen who scored above the team average is:%d\n",abvavg);
            printf("The average is:%d\n",avg);
            getch();
            return 0;
    }

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    This statement
    Code:
    name[j] = name1;
    is in error. You need to use strcpy().
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Also, you need a 2 dimensional array for names.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    Doesn't this give you a compiler error?
    Code:
    name[j] = name1;
    Compiler warnings and errors are very helpful--even if they seem unintelligible at first.

    I always try to solve the first one first, because sometimes a single error can confuse the compiler and result in 50 error messages.

    Also, sometimes you get an error message and the program doesn't even compile. But if it compiled previously, you may have the previous executable still present. So if you ignore the errors, you may be executing the old executable. That can be very confusing, since it will continue to fail the same way regardless of what changes you make in the code.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    13

    more

    Ok so I implemented strcpy. Can someone show me how to do the two dimensional array for the names?

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    int main(void)
    {
          char name[10];
          char name1[20];
          
          int j,score,sum,avg,ducknum,abvavg;        
          int a[5];
          
          abvavg = 0;
          ducknum = 0;
          sum = 0;
          
          for(j = 0; j < 3; j++)
          
          {
              printf("Please enter the name\n");
              scanf("%s",&name);
              
              printf("Enter score\n");
              scanf("%d",&score);
              
              a[j] = score;
              strcpy(name1,name);
              sum = sum + score;
              
              }
              
              
          
          
          avg = sum / 4;
          for(j = 0; j < 3; j++)
              {
              
               if(a[j] > avg)
               {
                abvavg = abvavg + 1;
                }
    		   if(a[j] == 0)
    		   {
                		ducknum = ducknum + 1;
                	   }    
    			if(a[j] > 50)
               		{
                			printf("%s hit above 50 with a score of %d\n",name1,a[j]);
                		}   
               
              }
            printf("The amount of batsmen who made a duck is:%d\n",ducknum);
            printf("The amount of batsmen who scored above the team average is:%d\n",abvavg);
            printf("The average is:%d\n",avg);
            getch();
            return 0;
    }

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by key4life View Post
    Ok so I implemented strcpy. Can someone show me how to do the two dimensional array for the names?

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    int main(void)
    {
          char name[10];
          char name1[20];
          
          int j,score,sum,avg,ducknum,abvavg;        
          int a[5];
          
          abvavg = 0;
          ducknum = 0;
          sum = 0;
          
          for(j = 0; j < 3; j++)
          
          {
              printf("Please enter the name\n");
              scanf("%s",&name); // no & required
              
              printf("Enter score\n");
              scanf("%d",&score);
              
              a[j] = score;
              strcpy(name1,name);
              sum = sum + score;
              
              }
              
              
          
          
          avg = sum / 4;
          for(j = 0; j < 3; j++)
              {
              
               if(a[j] > avg)
               {
                abvavg = abvavg + 1;
                }
    		   if(a[j] == 0)
    		   {
                		ducknum = ducknum + 1;
                	   }    
    			if(a[j] > 50)
               		{
                			printf("%s hit above 50 with a score of %d\n",name1,a[j]);
                		}   
               
              }
            printf("The amount of batsmen who made a duck is:%d\n",ducknum);
            printf("The amount of batsmen who scored above the team average is:%d\n",abvavg);
            printf("The average is:%d\n",avg);
            getch();
            return 0;
    }
    Two dimensional array:
    Code:
    char name1[3][20]; // 3 names with at most 20chars each
    for(j=0;j<3;j++)
    scanf("%s",name[j]); // input all the 3 names using for loop, name[0] is the base address of first name and so on
    for(j=0;j<3;j++)
    printf("%s\n",name[j]); // print those 3 names
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    Ok thanks ALOT guys. It works good now. LAST problem i promise. I really dont know how to read strings with a space. Like if i enter John Brown. It messes up. Can only store One string at a time. The white space causes problems. Advice on getting around this?

  13. #13
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    Quote Originally Posted by key4life View Post
    LAST problem i promise.
    There's never a last problem. Really.

    Quote Originally Posted by key4life View Post
    I really dont know how to read strings with a space. Like if i enter John Brown. It messes up. Can only store One string at a time. The white space causes problems. Advice on getting around this?
    scanf() stops at a space, but fgets() doesn't.

    If you don't mind risking a crash on a buffer overrun, you could alternately use gets().

  14. #14
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by key4life View Post
    Ok thanks ALOT guys. It works good now. LAST problem i promise. I really dont know how to read strings with a space. Like if i enter John Brown. It messes up. Can only store One string at a time. The white space causes problems. Advice on getting around this?
    Using scanf:
    Code:
    char str[100];
    scanf("%[^\n]s",str);
    Using fgets
    Code:
    fgets(str,sizeof(str),stdin);
    So, you can see why using fgets is beneficial.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  15. #15
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    Well when i run this now, after the first input of name it works fine. then after i input a score it jus skips the next name scan (outputs the prompt tho) then prompts for the next score.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #define array_SIZE 3
    #define str_SIZE 20
    int main(void)
    {
             
          char name[SIZE][str_SIZE];     
          int j,score,sum,avg,ducknum,abvavg;        
          int a[SIZE];
          
          abvavg = 0;
          ducknum = 0;
          sum = 0;
          
          for(j = 0; j < SIZE; j++)
          
          {
              printf("Please enter the name\n");
              fgets(name[j],str_SIZE,stdin);
              
              
              printf("Enter score\n");
              
              scanf("%d",&score);
              
              a[j] = score;
              sum = sum + score;
              
          }
             
             
          avg = sum / SIZE;
          for(j = 0; j < SIZE; j++)
              {
              
               if(a[j] > avg)
               {
                abvavg = abvavg + 1;
                }
    		   if(a[j] == 0)
    		   {
                		ducknum = ducknum + 1;
                	   }    
    			if(a[j] > 50)
               		{
                			printf("%s hit above 50 with a score of %d\n",name[j],a[j]);
                		}   
               
              }
            printf("The amount of batsmen who made a duck is:%d\n",ducknum);
            printf("The amount of batsmen who scored above the team average is:%d\n",abvavg);
            printf("The average is:%d\n",avg);
            getch();
            return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21