Thread: Printing an array of inputted characters

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    25

    Question Printing an array of inputted characters

    Greetings everyone,

    I cannot seem to output an array of strings after inputting it.
    The user should input Names in each cell and then the names should be outputted.
    The output should give the inputted Name as well as the Points. I've done it with Points, but the names I cannot output.

    Code:
      char name[15];
      int cnum;
      int points[15];
      int cont;
      int i;
          cnum=0;
          for (i=0; i<15; i++)
    	{
    	  cnum++;
        printf("1. ");
          printf("Insert the name of contest number %i : ", i+1);
        scanf("%s", &name[i]);
     
       printf("3. ");
      printf("Insert points [between 0 e 100]: ");
        scanf("%d", &points[i]);
       
     printf("Continue for other participants? 1=yes, 0=no:\n"); 
     scanf("%d", &cont);
     {
     if(cont==0)
       break;
     }
    	}
    
          for(i=0; i<cnum; i++)
    	{
    	  printf("Contestant numb %i info:\n Points %i\n Name %s\n", i+1, points[i], name);
    	}
    Any help would be appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your name array needs to be a 2D array of chars, if you're expecting to store strings.

    char names[15][MAX_NAME_LENGTH];

    Your indentation could be better as well.
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    That was stupid of me, thanks Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Single line user inputted string to Array?
    By Sparrowhawk in forum C Programming
    Replies: 11
    Last Post: 12-14-2008, 08:10 PM
  3. copying the first 10 characters to an array?
    By arya6000 in forum C Programming
    Replies: 3
    Last Post: 09-20-2008, 10:39 PM
  4. Characters in an Array
    By Xenmordoc in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 11:19 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM