#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

Recommended Answers

All 3 Replies

use a char *names
memset names after the entry from user

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

commented: gives a good references +3

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! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.