Thread: help in c++

  1. #1
    drf716 drf716's Avatar
    Join Date
    Mar 2005
    Posts
    1

    help in c++

    the directions was to:
    1. enter name of student into array of string; size is
    60
    -enter size of array
    -surname first name, middle initial
    2. display names
    3.sort names alphabetically
    4. exit program

    this is what i did;
    Code:
    # include <iostream.h>
    # include <string.h>
    # include <conio.h>
    
      struct names{
        char name[20];
    	
    };
    
    void bubble();
    
    
        int main(){
        bubble();
        return 0;
    }
    
    
        void bubble(){
        int n;
        char temp[20];
        cout<<"How many names are you going to
    insert?"<<endl;
        cin>>n;
        names * array=new names[n];
    
    
            for(int i=0;i<n;i++) {
            cout<<"Give me the name"<<endl;
            cin>>array[i].name;
    
    
        }
    
        for (int x=1 ; x<n ; x++)
        for (int y=0 ; y<n-x ; y++)
    
    
            if ( strcmp ( array[y].name, array[y+1].name)
    > 0){
            strcpy( temp,array[y].name);
            strcpy( array[y].name, array[y+1].name );
            strcpy( array[y+1].name, temp );
        }
    
    
            for(int a=0;a<n;a++){
            cout<<"The sorted names are:
    "<<array[a].name<<endl;
        }
    
        getch();
    }
    my proffesor told me:

    i should be able to type the name, middle name, & surname into an array that has the size of 60
    i used cin, i should just use c++
    i should follow directions
    he said all i did was 1 & 3

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    11
    you can use array inside another array for the 1st name and for the last name or u can use cin.getline

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    You're on the right track. With a little work, that struct will do it for you. I'll give you a hint.
    Code:
    struct names
    {
        char first_name[20];
        //add more fields
    };
    Of course, then your sort will be a little different. You'll either need an overloaded = operator or a lot of strcpy().

    Any particular reason you're using char arrays (C-strings) instead of strings?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I'll give you a hint.
    char first_name[20];
    the directions was to:
    1. enter name of student into array of string; size is
    60
    =====
    Any particular reason you're using char arrays (C-strings) instead of strings?
    the directions was to:
    1. enter name of student into array of string; size is
    60
    my proffesor told me:
    i should be able to type the name, middle name, & surname into an array that has the size of 60
    ====

    This was your instruction:
    1. enter name of student into array of string; size is
    60
    ..and you did this:
    Code:
    struct names{
        char name[20];
    	
    };
    Are you supposed to use a struct? I don't think that makes sense given the instructions. It sounds like you are supposed to use a char array of size 60 to hold all three names, but then your instructions say the user is supposed to enter the size of the array? Which is it?

    i used cin, i should just use c++
    cin is c++, do what does that mean?
    Last edited by 7stud; 03-07-2005 at 01:46 PM.

Popular pages Recent additions subscribe to a feed