I'm trying to create an array that will hold the first name of 100 users

I tried

char fname[100]

but I realized that only creates an array that hold 100 char.
how do you create an array to hold 100 entries that has a max of 50 chars in each entry?

thanks

Make a 2D array.

char fname[100][50];
fname[0] = "First Name1";
fname[1] = "First Name2";
... and so on

>char fname[100][50];
That should be 51 if you're allowing 50 content characters. You need to make room for the null character at the end.

Or use structure within arrays...

#include <iostream>
#include <conio>

struct name
{
   char n[10];
   void firstName()
	{
   	cout << "Enter name : ";
   	cin >> n;

   }
};

void main ()
{
	name nam[3];
   nam[1].firstName();
   nam[2].firstName();
   nam[3].firstName();

	getch();
}

use loop for calling structure 100 times..
(array index 1 has one name of 10 characters)

why not use the C++ STL string?

E.g.

#include <iostream>

using namespace std;

int main()
{
    string fname[100];
    
    for(int i=0; i<100; ++i)
    {
        cout << "Enter name: ";
        cin >> name;
    }

    //cout << endl << "You entered: " << name << endl;
    //system("pause");
    
    return 0;
}

The best thing you ca try is a vector of strings

#include <vector>
#include <string>

using namespace std;

int main()
{
       vector<string> vFName;
       string strFName;
       for ( int i = 0; i < 100 ; i++ )
       {
             //Get First name and store in strFName;
             strFName = "First Name";
             vFName.push_back( strFName );       
       }
       return 1;
}

Make a 2D array.

char fname[100][50];
fname[0] = "First Name1";
fname[1] = "First Name2";
... and so on

you have to use strcpy() to set those char arrays

strcpy( fname[0], "First Name1");
strcpy( fname[1], "First Name2");
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.