Insert new element to array and how shift char type

Click For Summary
The discussion centers on how to insert a new student element into an array of structs in C++. The main challenge is shifting existing elements to make space for the new entry, particularly handling the copying of character arrays for names. Participants emphasize the use of the `strcpy` function for copying strings, as direct assignment for character arrays is not valid. The solution involves iterating from the end of the array to the desired insertion position, shifting elements accordingly. The thread concludes with the original poster successfully resolving their issue with the provided guidance.
Sumaya
Messages
29
Reaction score
0

Homework Statement



Code:
struct student
{
char name[20];
int id;
float gpa;
};
student a[20];



Homework Equations



insert another element into the array a.

The Attempt at a Solution





Code:
#include<iostream>
using namespace std;
#include <stdio.h>
#include <string.h>

struct student
{
	char name[20];
	int id;
	float gpa;
};
student a[20];
int main()
{
	int item,newKey,posn,newGpa;
	int ID;
	char newName[20];
	const int NUMEL=30;
	student a[NUMEL]={{"Sarah",300,77.5},{"Joerj",600,87.9},{"Steven",400,89},{"Jason",200,65.7},{"Kyle",500,99},
	                   {"Adam",800,69.44},{"Alix",700,76.9},{"Jermey",900,85.8}};        

	int sumSize;
	for(int i=0;a[i].id!=NULL;i++)
	{
		sumSize=i;
	}
	int actualSize=sumSize+1;
	cout<<"The actual size (no. of elements ) of the array is  :  "<<actualSize<<endl<<endl;
	cout<<"The free size in the static array is  :  "<<NUMEL-actualSize<<endl<<endl;
	
	cout<<"the position of the new data will be inserted in the front (first element)"<<endl;
	cout<<" please, enter the positon : "<<endl;
	cin>>posn;
	--posn;  
	//cout<<"Input the name :"<<endl;
	//cin>>newName;

	cout<<" the new id is "<<endl;
	cin>>ID;
	cout<<"enter the new gpa :"<<endl;
	cin>>newGpa;

	cout<<"enter the new name :"<<endl;
	

for(int i=actualSize;i>posn;i--)
{
	 a[i].name=a[i-1].name; \\ it doesn't accept = for char ... why ?? how i can do it?? doing shifting from the end to insert new element ..


    a[i].id=a[i-1].id;
    a[i].gpa=a[i-1].gpa;
	

actualSize++;
}
for(int i=0;i=posn;){

	cin>>a[i].name;
}
for(int i=0;i=posn;)
{
	cout<<a[i].name<<"  "<<endl;
}
	//newName[i]=a[i].name;// here also 
	//a[posn].name=newName;
	//}
		//a[posn].name=newName;
		a[posn].id=ID;
		a[posn].gpa=newGpa;
//cout<<a[posn].id<<endl<<endl;
cout<<"the new array after inserstion  "<<endl;
for(int i=0;i<actualSize;i++)
{
	cout<<a[i].name<<"  "<<a[i].id<<"  "<<a[i].gpa<<endl;
}


	return 0;
}



i want to know how to insert char name into the array and doing shift for whole names ...

thanx a lot ...
 
Physics news on Phys.org
Sumaya said:

Homework Statement



Code:
struct student
{
char name[20];
int id;
float gpa;
};
student a[20];



Homework Equations



insert another element into the array a.

The Attempt at a Solution





Code:
#include<iostream>
using namespace std;
#include <stdio.h>
#include <string.h>

struct student
{
	char name[20];
	int id;
	float gpa;
};
student a[20];
int main()
{
	int item,newKey,posn,newGpa;
	int ID;
	char newName[20];
	const int NUMEL=30;
	student a[NUMEL]={{"Sarah",300,77.5},{"Joerj",600,87.9},{"Steven",400,89},{"Jason",200,65.7},{"Kyle",500,99},
	                   {"Adam",800,69.44},{"Alix",700,76.9},{"Jermey",900,85.8}};        

	int sumSize;
	for(int i=0;a[i].id!=NULL;i++)
	{
		sumSize=i;
	}
	int actualSize=sumSize+1;
	cout<<"The actual size (no. of elements ) of the array is  :  "<<actualSize<<endl<<endl;
	cout<<"The free size in the static array is  :  "<<NUMEL-actualSize<<endl<<endl;
	
	cout<<"the position of the new data will be inserted in the front (first element)"<<endl;
	cout<<" please, enter the positon : "<<endl;
	cin>>posn;
	--posn;  
	//cout<<"Input the name :"<<endl;
	//cin>>newName;

	cout<<" the new id is "<<endl;
	cin>>ID;
	cout<<"enter the new gpa :"<<endl;
	cin>>newGpa;

	cout<<"enter the new name :"<<endl;
	

for(int i=actualSize;i>posn;i--)
{
	 a[i].name=a[i-1].name; \\ it doesn't accept = for char ... why ?? how i can do it?? doing shifting from the end to insert new element ..


    a[i].id=a[i-1].id;
    a[i].gpa=a[i-1].gpa;
	

actualSize++;
}
for(int i=0;i=posn;){

	cin>>a[i].name;
}
for(int i=0;i=posn;)
{
	cout<<a[i].name<<"  "<<endl;
}
	//newName[i]=a[i].name;// here also 
	//a[posn].name=newName;
	//}
		//a[posn].name=newName;
		a[posn].id=ID;
		a[posn].gpa=newGpa;
//cout<<a[posn].id<<endl<<endl;
cout<<"the new array after inserstion  "<<endl;
for(int i=0;i<actualSize;i++)
{
	cout<<a[i].name<<"  "<<a[i].id<<"  "<<a[i].gpa<<endl;
}


	return 0;
}



i want to know how to insert char name into the array and doing shift for whole names ...

thanx a lot ...

I think your questions are
1) How to insert a student struct into the array of structs.
2) How to copy a string into an array.

For 1, if you want to insert a student struct into the array at index j, you need to shift the existing elements of the array one element higher in the array. For example, if you want to put a new element at index 4, you need to move all of the elements currently at indexes 4, 5, 6, and so on to indexex 5, 6, 7, and so on. One difficulty is that your array is defined with 20 elements, so if the array is full, shifting all the elements one element higher in the array will cause the loss of the element at index 19 (the highest index in your array).

If you do things this way, you want to start at index 18, and move it to index 19, then move the element at index 17, and move it to index 18, and so on down to the index of the element you want to free up.

For 2, since you are using the C standard library, to copy a string from one place to another, use strcpy. The prototype of this function is in string.h.

The reason what you were trying didn't work is that you can't copy an array of char using assignment.

Code:
newName[i]=a[i].name;
There are two problems with this code.
a) The types on either side are different. newName is type char. a.name is type char [], or character array.
b) The name of an array doesn't represent all of the data stored in the array (a string of characters, in this case). It evaluates to the address of the first element in the array. In other words, name by itself is a kind of pointer, which you might not have studied yet.
 
thanx a lot
Code:
for(int i=actualSize;i>posn;i--)
{
a[i]=a[i-1];

actualSize++;
}

i did this to shift elements and without getting problem with name .

but how i say that a[0].name=newName;
suppose i want to insert in the front of the array .
and how i can use strcpy ?
 
Sumaya said:
thanx a lot


Code:
for(int i=actualSize;i>posn;i--)
{
a[i]=a[i-1];

actualSize++;
}

i did this to shift elements and without getting problem with name .

but how i say that a[0].name=newName;
You can't do this, and I explained why you can't in post #2.
Sumaya said:
suppose i want to insert in the front of the array .
I also explained how you can insert a student structure at any given position.
Sumaya said:
and how i can use strcpy ?
See the documentation - http://www.cplusplus.com/reference/clibrary/cstring/strcpy/
 
hi

Code:
#include<iostream>
using namespace std;
#include <stdio.h>
#include <string.h>

struct student
{
	char name[20];
	int id;
	float gpa;

};
student a[20];
int main()
{
	int item,newKey,posn,newGpa;
	int ID;
	const int NUMEL=30;
	student a[NUMEL]={{"Sarah",300,77.5},{"Joerj",600,87.9},{"Steven",400,89},{"Jason",200,65.7},{"Kyle",500,99},
	                   {"Adam",800,69.44},{"Alix",700,76.9},{"Jermey",900,85.8}};        

	int sumSize;
	for(int i=0;a[i].id!=NULL;i++)
	{
		sumSize=i;
	}
	int actualSize=sumSize+1;
	cout<<"The actual size (no. of elements ) of the array is  :  "<<actualSize<<endl<<endl;
	cout<<"The free size in the static array is  :  "<<NUMEL-actualSize<<endl<<endl;
	
	cout<<"the position of the new data will be inserted in the front (first element)"<<endl;
	cout<<" please, enter the positon : "<<endl;
	cin>>posn;
	--posn;  
	

	

for(int i=actualSize;i>posn;i--)
{
a[i]=a[i-1];
	
actualSize++;
}
cout<<"enter the new name :"<<endl;
	cout<<" the new id is "<<endl;
	cout<<"enter the new gpa :"<<endl;
for(int i=0;i<=posn;i++)
{

	cin>>a[i].name;\\ here was my mistake to insert new values
	cin>>a[i].id;
	cin>>a[i].gpa;

}

cout<<"the new array after inserstion  "<<endl;
for(int i=0;i<actualSize;i++)
{
	cout<<a[i].name<<"  "<<a[i].id<<"  "<<a[i].gpa<<"  "<<endl;
}	return 0;
}

thank you for helping me .. i solved the problem ... thanks a lot ...
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
6K
Replies
5
Views
2K
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 13 ·
Replies
13
Views
3K