Help!

This is a discussion on Help! within the C Programming forums, part of the General Programming Boards category; Im new to C Programming, and am attempting to write a program to take the names of ten people and ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    1

    Help!

    Im new to C Programming, and am attempting to write a program to take the names of ten people and output the highest length. This is what I've got so far:

    Code:
    #include <stdio.h>
    
    main() {
    
    int Name_Array[10];     /* Creates a variable with 10 indexes */
    char Name [10] [25];        /*Creates a char with up to 25 indexes */
    gets (Name);
    int i=0;
    
    while (i<11)           /* Creates a loop while "i" is less than 11 */
    
              {
    
              i=(i+1)
              printf ("\nEnter name", Name_Array[i]);
              scanf (&#37;s, Name [i] [0-25]);
    
              }
    
    int str_len [10];                   
    
    str_len [1]= strlen (Name_Array [1]);
    str_len [2]= strlen (Name_Array [2]);
    str_len [3]= strlen (Name_Array [3]);
    str_len [4]= strlen (Name_Array [4]);
    str_len [5]= strlen (Name_Array [5]);
    str_len [6]= strlen (Name_Array [6]);
    str_len [7]= strlen (Name_Array [7]);
    str_len [8]= strlen (Name_Array [8]);
    str_len [9]= strlen (Name_Array [9]);
    str_len [10]= strlen (Name_Array [10]);
    
    }
    I need to:

    a)compress the strlen portion into a few lines
    b)get the program to take the str_len array with the highest value and print it....

    Thanks!
    Last edited by Callumagus; 05-21-2008 at 12:21 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Please edit your post and remove the excess width inside your code tags.

    That's called "breaking the forum tables", and genuinely hated, since it's a constant aggravation to scan back and forth to read your post.

    Thanks.

    You don't need this large an array at all. You need two things: the current name being entered, and the longest name which has been entered, so far.

    That's it. Every name that is entered is checked inside the while loop, and if it is longer than the previously longest name, then it replaces it in that role.
    Last edited by Adak; 05-20-2008 at 03:22 PM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >while (i<11)           /* Creates a loop while "i" is less than 11 */
    >
    >          {
    >
    >          i=(i+1)
    >          printf ("\nEnter name", Name_Array[i]);
    >          scanf (%s, Name [i] [0-25]);
    >
    >          }
    for-loops are nice for this purpose:
    Code:
    #include <stdio.h>
    #include <string.h>
    .
    .
    for (i=0; i<10; i++)           /* Creates a loop while "i" is less than 10 */
    {
        printf ("Enter name: ");
        fflush(stdout);
        scanf ("%25s", Name[i]);
    }
    Now compile, correct any errors, and run. Then add another loop to print out the names. Once you have that working, keep adding code until you have a working program.

Popular pages Recent additions subscribe to a feed

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