I have a project for school that i am working on, and i cant figure out how to store names in an array. i have it declared as

char firstname[25];
char lastname[25];

but i was told that just stored one string and when i go to print out the names that are input, it just prints the last one i put in in all the slots. So I was wondering what the easiest way to store all the names in the arrays and print them out. The only thing i could find on the internet was to make an array of string arrays , but im not sure how to do that.

Thanks.

what you need is an array of strings so that the program can keep all the strings until ready to print them all out

const int MAXSTRINGS = 10; // hold max of 10 strings
const int MAXLENGTH = 25; // maximum of 40 characters per string
char firstname[MAXSTRINGS][MAXLENGTH];
char lastname[MAXSTRINGS][MAXLENGTH];

how you will have to index into those arrays

int i;
for(i = 0; i < MAXSTRINGS; i++)
{
    printf("Enter a first name\n");
    fgets( firstname[i], MAXLENGTH, stdin);
}

I have a project for school that i am working on, and i cant figure out how to store names in an array. i have it declared as

char firstname[25];
char lastname[25];

but i was told that just stored one string and when i go to print out the names that are input, it just prints the last one i put in in all the slots. So I was wondering what the easiest way to store all the names in the arrays and print them out. The only thing i could find on the internet was to make an array of string arrays , but im not sure how to do that.

Thanks.

Look at it in a normal way. When you are assigning an array to hold a name as firstname[25] it can hold only one string of characters. So if you want to hold more names declare it as firstname[25][25]

With input taken as:

for(i=0;i<25;i++)
gets(char[i]);

When there is one single space for just a name and you go on writing many names in that space it will obviously print the last written name.

>gets(char);
FYI, booze in fact does not improve your ability to write code. You'd have to be quite confused to write char when you meant firstname, and any experienced C programmer would have to be drunk to use gets.

>gets(char);
FYI, booze in fact does not improve your ability to write code. You'd have to be quite confused to write char when you meant firstname, and any experienced C programmer would have to be drunk to use gets.

Ya that was firstname. Sorry mis-representation. Call it a "Slip of keys". ;)

So if i were to print an my array of names, say for a roster of passengers on an airplane would this code be correct?

void prFlight(char firstname[][LENGTH], char lastname[][LENGTH], int seatnumber,int seat[])
{
	int cnt=0;
	int i;
	int j;

	printHeading();
	
	//sortName(*lastname);

	for(i=0;i<FCLASS;i++){

		printf("\n%s %5s%10d",firstname[i],lastname[i],seatnumber);
	}
	printf("Number of people in first class is %d:",seatnumber);
		
	
	for(j=0;j<SIZE;j++){
		if (seat[j]==1)
			cnt++;
		printf("\n%s %5s%10d",firstname[i],lastname[i],seatnumber);
	}
	printf("Number of people in economy class is %d:",cnt);

This function is supposed to print the seat nos. and names of all the people on the flight.

>>So if i were to print an my array of names, say for a roster of passengers on an airplane would this code be correct?

Did you run that code? Did it work the way you wanted it to? Answer those two questions "YES" and you will have the answer to your question.

Yes i ran it and it printed just a bunch of line with the name in between them
.

Here's my full code for this project if it helps.

#include <stdio.h>
#include <string.h>

#define SIZE 40
#define FCLASS 10
#define ECONOMY 30
#define FIRST 40
#define LAST 40
#define LENGTH 25

void askFlight(int,int[],char[][LENGTH],char[][LENGTH]);
void askName(int,char[][LENGTH],char[][LENGTH]);
void printBPass(char[][LENGTH],char[][LENGTH],int);
void printHeading(void);
void chkFClass(int,int,int,int,int[],char[][LENGTH],char[][LENGTH]);
void chkEClass(int,int,int,int,int[],char[][LENGTH],char[][LENGTH]);
void swap(char **, char **);
//void sortName(char *);
void prFlight(char[][LENGTH],char[][LENGTH],int,int[]);

int main(void)
{
	int seatnumber=0;
	int seat[SIZE]={0};
	char choice2;
	char firstname[FIRST][LENGTH];
	char lastname[LAST][LENGTH];
	int choice=0;

	do{
		

	askFlight(choice,seat,firstname,lastname);
	
	
	






	puts("\nWould you like to board another flight?");
	scanf("%*c%c", &choice2);



	}while (choice2 == 'y' || choice2 == 'Y');

	if(choice2=='n' || choice2=='N')
		prFlight(firstname,lastname,seatnumber,seat);



  
  return 0;


	

}
void askFlight(int choice,int seat[],char firstname[][LENGTH],char lastname[][LENGTH])
{
	 
	
	int seatnumber=0;
	char economyChoice=0;
	char fclassChoice=0;
	
  

  printf("Welcome to the Airline Reservations System\n\nWould you like to fly First class or economy?\n");
  printf("Please type 1 for first class\nplease type 2 for economy.");
  scanf("%d", &choice);

  chkFClass(seatnumber,fclassChoice,economyChoice,choice,seat,firstname,lastname);
  chkEClass(seatnumber,fclassChoice,economyChoice,choice,seat,firstname,lastname);

 


}

void askName(int seatnumber,char firstname[][LENGTH], char lastname[][LENGTH])
{
	int i;
	int j;

	for(i=0;i<SIZE;i++){

	printf("Please enter your first name:\n");
	scanf("%s", firstname[i]);
	

	

	printf("Please enter your last name:\n");
	scanf("%s", lastname[i]);
	break;
	

	}
	printHeading();
	printBPass(firstname,lastname,seatnumber);
	
}





void printBPass(char firstname[][LENGTH], char lastname[][LENGTH], int seatnumber)
{
	printf("\n%s %5s%10d",firstname,lastname,seatnumber);

}
void printHeading(void)
{
	printf("%s%15s", "Customer Name", "Seat Number");
}
void chkFClass(int fclassChoice,int economyChoice,int seatnumber,int choice,int seat[],char firstname[][LENGTH],char lastname[][LENGTH])
{
	if ( choice==1)
   {
     for( seatnumber=1 ; seatnumber<=FCLASS ; seatnumber++)
         {
           if(seat[seatnumber]==0)
              {
               seat[seatnumber]=1; 
               askName(seatnumber,firstname,lastname);
			   break;
		   

		   }
		
		 
	 }
			   
		   
}

	    if (seatnumber>FCLASS)
	 {
		 printf("I am sorry, but the first class is full. Would you like like to fly economy?\n");
		 scanf("%*c%c", &economyChoice);

		 switch(economyChoice){
			 case 'y':
			 case 'Y':
				 choice=2;
				 break;
			 case 'n':
			 case 'N':

				 printf("Next flight leaves in three hours.");
				 break;

				 }
		 chkEClass(seatnumber,fclassChoice,economyChoice,choice,seat,firstname,lastname);

}
   

}
void chkEClass(int seatnumber,int fclassChoice,int economyChoice,int choice,int seat[],char firstname[],char lastname[])
{
	if(choice==2)

 {
     for( seatnumber=11 ; seatnumber<SIZE; seatnumber++)
         {
           if(seat[seatnumber]==0)
              {
               seat[seatnumber]=1;
               
               askName(seatnumber,firstname,lastname);
               break;
                 
                                  
               } 
        }
  
   
}
  if (seatnumber>SIZE)
	 {
		 printf("I am sorry, but the economy class is full. Would you like like to fly first class?\n");
		 scanf("%*c%c", &fclassChoice);

		 switch(fclassChoice){
			 case 'y':
			 case 'Y':
				 choice=1;
				 break;
			 case 'n':
			 case 'N':

				 printf("Next flight leaves in three hours.");
				 break;
		 }
		 chkFClass(seatnumber,fclassChoice,economyChoice,choice,seat,firstname,lastname);
  }
		 		 
}
/*void sortName(char *lastname[]) {
 int i = 0;
 int j = 0;

 for(i = 0; i < SIZE; ++i)
  for(j = i + 1; j < SIZE; ++j)
   if(strcmp(lastname[i], lastname[j]) > 0)
    swap(&lastname[i], &lastname[j]);
}
*/
void swap(char **p, char **q) {
 char *tmp;

 tmp = *p;
 *p = *q;
 *q = tmp;
}
void prFlight(char firstname[][LENGTH], char lastname[][LENGTH], int seatnumber,int seat[])
{
	int cnt=0;
	int i;
	int j;

	printHeading();
	
	//sortName(*lastname);

	for(i=0;i<FCLASS;i++){

		printf("\n%s %5s%10d",firstname[i],lastname[i],seatnumber);
	}
	printf("Number of people in first class is %d:",seatnumber);
		
	
	for(j=0;j<SIZE;j++){
		if (seat[j]==1)
			cnt++;
		printf("\n%s %5s%10d",firstname[i],lastname[i],seatnumber);
	}
	printf("Number of people in economy class is %d:",cnt);
		
		

	
	
	

}

I am just really pressed for time and i have everything else working except for this. Its like that feeling when you are about to win a race but your engine blows up. Its frustrating:(

Hey there are few errors which you can work on:

1)

void chkEClass(.........................)
{
}

Has an error in first line as:

void chkEClass( , , , ,char firstname[],char lastname[])

this should be

void chkEClass( , , , ,char firstname[][LENGTH],char lastname[][LENGTH])

2)There exists an error in the looping concept while taking the input which exits the program even when the user hasn't finished his choices.

Go on through the code stage by stage as though you are compiling it you will find out the errors more clearly.

Edited 3 Years Ago by happygeek: fixed formatting

Comments
You were helpful. All it took was for me to take take to stop and look at my code line by line

I have been workiing on getting the names to print at the very end for 3 days now and i just can't figure it out.

#include <stdio.h>
#include <string.h>

#define SIZE 40
#define FCLASS 10
#define ECONOMY 30
#define FIRST 40
#define LAST 40
#define LENGTH 25
/*function prototypes*/
void askFlight(int,int[],char[][LENGTH],char[][LENGTH]);
void askName(int,char[][LENGTH],char[][LENGTH]);
void printBPass(char[][LENGTH],char[][LENGTH],int);
void printHeading(void);
void chkFClass(int,int,int,int,int[],char[][LENGTH],char[][LENGTH]);
void chkEClass(int,int,int,int,int[],char[][LENGTH],char[][LENGTH]);
void prFlight(char[][LENGTH],char[][LENGTH],int,int[]);

int main(void)
{
	int seatnumber=0;/*passenger seat number*/
	int seat[SIZE]={0};/*array to verify seats availible*/
	char choice2;
	char firstname[SIZE][LENGTH]={" "};/*array to store user names*/
	char lastname[SIZE][LENGTH]={" "};/*array to store user names*/
	int choice=0;

	do{
		

	askFlight(choice,seat,firstname,lastname);/*prompt user to choose flight*/
	
	
	






	puts("\nWould you like to board another flight?");/*prompt user to choose to board flight*/
	scanf("%*c%c", &choice2);



	}while (choice2 == 'y' || choice2 == 'Y');

	if(choice2=='n' || choice2=='N')
		prFlight(firstname,lastname,seatnumber,seat);



  
  return 0;


	

}
void askFlight(int choice,int seat[],char firstname[][LENGTH],char lastname[][LENGTH])
{
	 
	
	int seatnumber=0;
	char economyChoice=0;
	char fclassChoice=0;
	
  

  printf("Welcome to the Airline Reservations System\n\nWould you like to fly First class or economy?\n");
  printf("Please type 1 for first class\nplease type 2 for economy.");
  scanf("%d", &choice);

  chkFClass(seatnumber,fclassChoice,economyChoice,choice,seat,firstname,lastname);
  chkEClass(seatnumber,fclassChoice,economyChoice,choice,seat,firstname,lastname);

 


}

void askName(int seatnumber,char firstname[][LENGTH], char lastname[][LENGTH])
{
	int i;
	

	for(i=0;i<SIZE;++i){

	printf("Please enter your first name:\n");
	scanf("%s", firstname[i]);
	

	

	printf("Please enter your last name:\n");
	scanf("%s", lastname[i]);

	break;
	
	

	}
	printHeading();
	printBPass(firstname,lastname,seatnumber);
	
}





void printBPass(char firstname[][LENGTH], char lastname[][LENGTH], int seatnumber)
{
	printf("\n%s %5s%10d",firstname,lastname,seatnumber);

}
void printHeading(void)
{
	printf("%s%15s", "Customer Name", "Seat Number");
}
void chkFClass(int fclassChoice,int economyChoice,int seatnumber,int choice,int seat[],char firstname[][LENGTH],char lastname[][LENGTH])
{
	if ( choice==1)
   {
     for( seatnumber=1 ; seatnumber<=FCLASS ; seatnumber++)
         {
           if(seat[seatnumber]==0)
              {
               seat[seatnumber]=1; 
               askName(seatnumber,firstname,lastname);
			   break;
		   

		   }
		
		 
	 }
			   
		   
}

	    if (seatnumber>FCLASS)
	 {
		 printf("I am sorry, but the first class is full. Would you like like to fly economy?\n");
		 scanf("%*c%c", &economyChoice);

		 switch(economyChoice){
			 case 'y':
			 case 'Y':
				 choice=2;
				 break;
			 case 'n':
			 case 'N':

				 printf("Next flight leaves in three hours.");
				 break;

				 }
		 chkEClass(seatnumber,fclassChoice,economyChoice,choice,seat,firstname,lastname);

}
   

}
void chkEClass(int seatnumber,int fclassChoice,int economyChoice,int choice,int seat[],char firstname[][LENGTH],char lastname[][LENGTH])
{
	if(choice==2)

 {
     for( seatnumber=11 ; seatnumber<SIZE; seatnumber++)
         {
           if(seat[seatnumber]==0)
              {
               seat[seatnumber]=1;
               
               askName(seatnumber,firstname,lastname);
               break;
                 
                                  
               } 
        }
  
   
}
  if (seatnumber>SIZE)
	 {
		 printf("I am sorry, but the economy class is full. Would you like like to fly first class?\n");
		 scanf("%*c%c", &fclassChoice);

		 switch(fclassChoice){
			 case 'y':
			 case 'Y':
				 choice=1;
				 break;
			 case 'n':
			 case 'N':

				 printf("Next flight leaves in three hours.");
				 break;
		 }
		 chkFClass(seatnumber,fclassChoice,economyChoice,choice,seat,firstname,lastname);
  }
		 		 
}

void prFlight(char firstname[][LENGTH], char lastname[][LENGTH], int seatnumber,int seat[])
{
	int cnt=0;
	int i;
	int j;

	printf("\n********FIRST CLASS**********\n");

	printHeading();
	
	

	

	for(i=0;i<=FCLASS;i++){

		printf("\n%s %5s%10d",firstname[i],lastname[i],seatnumber);
	}
	printf("\n********FIRST CLASS**********");
	printf("\nNumber of people in first class is %d:",seatnumber);
		
	
	for(j=11;j<SIZE;j++){
		if (seat[j]==1)
			cnt++;
		printf("\n%s %5s%10d",firstname[i],lastname[i],seatnumber);
	}

	printf("\nNumber of people in economy class is %d:",cnt);
		
		

	
	
	

}

Any suggestions?

this is also my problem.. but is it possible to store names in an array using strcpy?

MY problem was in this block of code

int i;
	

	for(i=0;i<SIZE;++i){

	printf("Please enter your first name:\n");
	scanf("%s", firstname[i]);
	

	

	printf("Please enter your last name:\n");
	scanf("%s", lastname[i]);

	break;
	
	

	}
	printHeading();
	printBPass(firstname,lastname,seatnumber);
	
}

The for loop would go once and i would type the names to store in the array. Then it exits the loop. so the next time it goes through it just stores the next name in that same spot erasing the last name.

i fixed it this morning and i was so happy to figure it out:)

I know this is a very old thread, but how did you end up fixing the issue? I'm having almost the same problem, and I noticed you said you fixed it, but best I can tell you didn't say how you fixed it. I would love to see what you did so I can get past this issue in my own program.

This question has already been answered. Start a new discussion instead.