Insert new element to array and how shift char type

Click For Summary

Discussion Overview

The discussion revolves around the problem of inserting a new element into an array of structures in C++, specifically focusing on how to properly shift existing elements and handle character arrays for names within the structures. The context includes homework-related queries and attempts at coding solutions.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • Participants discuss the need to insert a new student struct into an array and the challenges associated with shifting existing elements to accommodate the new entry.
  • Some participants express confusion about how to copy character arrays (strings) in C++, noting that direct assignment does not work for arrays.
  • One participant suggests using the `strcpy` function from the C standard library to copy strings, while another participant questions how to implement this in their code.
  • There are multiple attempts to shift elements in the array, with varying methods proposed, including directly assigning the entire struct and using loops to manage individual fields.
  • Participants share snippets of code to illustrate their attempts and seek clarification on specific lines, particularly around the handling of character arrays for names.

Areas of Agreement / Disagreement

There is no consensus on the best method to handle the insertion and shifting of elements, as participants present different approaches and express varying levels of understanding regarding string manipulation in C++. The discussion remains unresolved with multiple competing views on how to achieve the desired functionality.

Contextual Notes

Participants note limitations in their current understanding of C++ string handling and array manipulation, particularly regarding the differences between character arrays and strings, as well as the implications of static array size on data insertion.

Who May Find This Useful

This discussion may be useful for students learning C++ who are dealing with arrays of structures, particularly in the context of data manipulation and string handling within those structures.

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
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
6K