#include <iostream.h>
#include <string>


void main()
{
    char name[10];
    int num_name;
    int count;


    cout << "How many names do you want to enter?\n";
    cout << "Answer: ";
    cin  >> num_name;


    int j =1;


    cout << "\nEnter names\n";



    for (count=1; count<=num_name; count++)
    {


        cout << j << "." <<" name: ";
        cin >> name[count];

        j = j+1;


    }


    for (count=1; count<=num_name; count++)
    {

        cout << "\n" << count << "." << name[count] ;


    }

    cout << "\n\n";

}

My task is to input names as many as the user wants to input. My problem is how to make this character into string that will accept names and not just character...

hope someone could help me out here. thanks in advance

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Didn't your professor told you about 2dim arrays?

const int MAX_LEN=20;//maximum length of eachname
char names[10][MAX_LEN];//create a 2D array to hold 10 names of length 20
int N=10;//
for( int i=0;i<10;i++){
cin.getline(names[i],MAX_LEN);

}

Reference to the function used in above code:
LIne 2:http://www.cplusplus.com/doc/tutorial/ntcs/
Line 5.http://www.cplusplus.com/reference/iostream/istream/getline/

As I know that you are a strict follower of your professor (I know this because of your thread on 'operator confusion'), I guess I should not tell you that standard C++ have a string class which simplifies the above process by million times.
One should always use c++ string rather than the Cstring(the one which is used in the above code) every time.
If you could divert some of your attention on standard c++ rather than following strictly what your professor told you( which is Ancient C++ actually) you could find the following article useful:http://www.bilmuh.gyte.edu.tr/gokturk/introcpp/icpp_strings1.html

Comments
gives a good references

As I know that you are a strict follower of your professor (I know this because of your thread on 'operator confusion'), I guess I should not tell you that standard C++ have a string class which simplifies the above process by million times.
One should always use c++ string rather than the Cstring(the one which is used in the above code) every time.
If you could divert some of your attention on standard c++ rather than following strictly what your professor told you( which is Ancient C++ actually) you could find the following article useful:http://www.bilmuh.gyte.edu.tr/gokturk/introcpp/icpp_strings1.html

Nope.... :( just teaching the basics :( and i know even my friends tell me it's so pre-historic....

thanks for the links and for the help! :)

This article has been dead for over six months. Start a new discussion instead.