- #1
Sumaya
- 29
- 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 ...