array of strings

This is a discussion on array of strings within the C++ Programming forums, part of the General Programming Boards category; I think i have a problem ..with having the user enter an array of strings ...I know how to compare ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    6

    array of strings

    I think i have a problem ..with having the user enter an array of strings ...I know how to compare the strings and sort them...i just can't grasp the concept



    practice makes perfect..

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    So, your question is?

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >I know how to compare the strings and sort them...i just can't
    >grasp the concept

    If you don't get the concept, then how do you know to compare and sort strings??

    I suggest you write another post to tell us your problem, since
    >practice makes perfect..

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Please try harder on the board before sending private requests for assistance! Ok?




    Can you help me with something
    How do I make this program more user
    friendly..I want the USER to enter the names as oppossed to names already entered...


    #include<iostream.h>
    #include<string.h>
    #include<stdio.h>
    void SortApplicants( char*[]);
    //SORTING STRINGS ARE HARD
    main()
    {
    //I have to have the user enter the input
    const int size = 80;
    char sentence[size];
    cin.getline( sentence, size);
    //for ( int i = 0; i < 10; i++ )




    SortApplicants(sentence);//Sort routine called
    //ERROR BECAUSE???
    return 0;
    }


    //********************************
    void SortApplicants(char *alpha[])
    {

    char *temp;
    int pass, k, row;

    for(pass = 0; pass < 10; pass++)
    { for(k = pass; k >=1; k--)
    { if(strcmp(alpha[k], alpha[k-1]) > 0)
    { temp = alpha[k];
    alpha[k] = alpha[k-1];
    alpha[k-1] = temp;
    }
    }
    }




    for(row = 0; row < 5; row ++)
    { printf("%s", alpha[row]);
    printf("\n");
    }

    return;}

    //end program

    __________________
    I guess no ones perfect





    Code:
    #include<iostream.h> 
    #include<string.h> 
    #include<stdio.h> 
    #include<conio.h> 
    #include<conio.c> 
    
    
    void Print(char *str)
    {
     printf("%s", str);
     getch();
    }
    
    
    bool SortApplicants(char applicants[][80], int number_of_applicants);
    
    
    //SORTING STRINGS ARE HARD 
    
    
    int main(void) 
    { 
    
    //I have to have the user enter the input 
    
    int number_of_applicants = 0;
    const int size = 80; 
    
    
    cout << "\nHow many applicants?\n" << endl;
    
    cin >> number_of_applicants;
    
    cout << "\nYou May Begin:\n" << endl;
    
    char applicants[ number_of_applicants ][size];
    
    
    for ( int i = 0; i < number_of_applicants; i++ ) 
    fgets( applicants[i], size, stdin );              //...cin.getline() is buggy! Don't use it!
    
    
    bool necessary = SortApplicants(applicants, number_of_applicants);
    
    
    if( !necessary )                            //...just for fun
     Print("\nThe Names Were Already In Order!");
    
    
    cout << "\n And Here Are The Results:\n" << endl;
    
    
    for ( int i = 0; i < number_of_applicants; i++ ) 
    Print(applicants[i]);
    
    Print("Done!");
    
    
    //SortApplicants(sentence); 
    //...No. "sentence" is an array of 80 chars...
    //..."applicants" is an array of arrays of 80 chars, see? - (80 X number_of_applicants)
     
    
    return 0; 
    } 
    
    
    
    
    bool SortApplicants(char applicants[][80], int number_of_applicants)
    {
    int pass;
    
    bool done = false;
    
    bool already_sorted = true;
    
    char temp[80];
    
    
    while( !done )
     {
    
      done = true; //...reset so we can break out when done...
    
      for(pass = number_of_applicants -1; pass > 0 ; pass--)
      {
    
        if(strcmp(applicants[pass], applicants[pass - 1]) < 0)
         {
          strcpy(temp,applicants[pass - 1]);
          strcpy(applicants[pass - 1],applicants[pass]);
          strcpy(applicants[pass],temp);
          done = false;               //...keep going till this doesn't happen in a full pass...
          already_sorted = false;
         }
    
       }
     }
    
    if(already_sorted)
     return false;
    else
     return true;
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 04:03 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

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