C++: write a structure called student that has three members called id(int), gpa
ID: 3858835 • Letter: C
Question
C++: write a structure called student that has three members called id(int), gpa(double), name(name[size]). In main declare an array of pointers of type student. The size of this array should be 4. Create 2 variables of type student and place the address of each in location 0 and 1 of this array. Create two other structures dynamically and place the address in the other two locations of this array. Pass this array to a function to read values for each item. Write a display function to display all values in the array.#include <iosstream> using namespace std;
below is what I currently have but it isn't working properly. Thank you.
#include <iostream>
using namespace std;
struct student{ int id; char name[30]; double gpa; };
string readValue(student *ptr[]){
for(int i=1; i<4; i++){ cout<<"Enter student"<<i<<"'s id: ";
cin>>ptr[i]->id; cout<<"Enter student"<<i<<"'s name: "; cin.ignore(); cin.getline(ptr[i]->name, 30); cout<<"Enter student"<<i<<"'s GPA: "; cin.ignore(); cin>>ptr[i]->gpa; }
}
void displayValue(student *ptr[]){
for(int i=1; i<4; i++){
cout<<"Student"<<i<<"'s id: "<<ptr[i]->id<<endl; cout<<"Student"<<i<<"'s name: "<<ptr[i]->name<<endl; cout<<"Student"<<i<<"'s GPA: "<<ptr[i]->gpa<<endl;
}
}
int main(){ student data; student *ptr[4];
ptr[0]=new student; ptr[1]=&data;
student data2; student data3;
ptr[2]=&data2; ptr[3]=&data3;
readValue(ptr); displayValue(ptr);
return 0; } C++: write a structure called student that has three members called id(int), gpa(double), name(name[size]). In main declare an array of pointers of type student. The size of this array should be 4. Create 2 variables of type student and place the address of each in location 0 and 1 of this array. Create two other structures dynamically and place the address in the other two locations of this array. Pass this array to a function to read values for each item. Write a display function to display all values in the array.
#include <iosstream> using namespace std;
below is what I currently have but it isn't working properly. Thank you.
#include <iostream>
using namespace std;
struct student{ int id; char name[30]; double gpa; };
string readValue(student *ptr[]){
for(int i=1; i<4; i++){ cout<<"Enter student"<<i<<"'s id: ";
cin>>ptr[i]->id; cout<<"Enter student"<<i<<"'s name: "; cin.ignore(); cin.getline(ptr[i]->name, 30); cout<<"Enter student"<<i<<"'s GPA: "; cin.ignore(); cin>>ptr[i]->gpa; }
}
void displayValue(student *ptr[]){
for(int i=1; i<4; i++){
cout<<"Student"<<i<<"'s id: "<<ptr[i]->id<<endl; cout<<"Student"<<i<<"'s name: "<<ptr[i]->name<<endl; cout<<"Student"<<i<<"'s GPA: "<<ptr[i]->gpa<<endl;
}
}
int main(){ student data; student *ptr[4];
ptr[0]=new student; ptr[1]=&data;
student data2; student data3;
ptr[2]=&data2; ptr[3]=&data3;
readValue(ptr); displayValue(ptr);
return 0; }
#include <iosstream> using namespace std;
below is what I currently have but it isn't working properly. Thank you.
#include <iostream>
using namespace std;
struct student{ int id; char name[30]; double gpa; };
string readValue(student *ptr[]){
for(int i=1; i<4; i++){ cout<<"Enter student"<<i<<"'s id: ";
cin>>ptr[i]->id; cout<<"Enter student"<<i<<"'s name: "; cin.ignore(); cin.getline(ptr[i]->name, 30); cout<<"Enter student"<<i<<"'s GPA: "; cin.ignore(); cin>>ptr[i]->gpa; }
}
void displayValue(student *ptr[]){
for(int i=1; i<4; i++){
cout<<"Student"<<i<<"'s id: "<<ptr[i]->id<<endl; cout<<"Student"<<i<<"'s name: "<<ptr[i]->name<<endl; cout<<"Student"<<i<<"'s GPA: "<<ptr[i]->gpa<<endl;
}
}
int main(){ student data; student *ptr[4];
ptr[0]=new student; ptr[1]=&data;
student data2; student data3;
ptr[2]=&data2; ptr[3]=&data3;
readValue(ptr); displayValue(ptr);
return 0; } #include <iostream>
using namespace std;
struct student{ int id; char name[30]; double gpa; };
string readValue(student *ptr[]){
for(int i=1; i<4; i++){ cout<<"Enter student"<<i<<"'s id: ";
cin>>ptr[i]->id; cout<<"Enter student"<<i<<"'s name: "; cin.ignore(); cin.getline(ptr[i]->name, 30); cout<<"Enter student"<<i<<"'s GPA: "; cin.ignore(); cin>>ptr[i]->gpa; }
}
void displayValue(student *ptr[]){
for(int i=1; i<4; i++){
cout<<"Student"<<i<<"'s id: "<<ptr[i]->id<<endl; cout<<"Student"<<i<<"'s name: "<<ptr[i]->name<<endl; cout<<"Student"<<i<<"'s GPA: "<<ptr[i]->gpa<<endl;
}
}
int main(){ student data; student *ptr[4];
ptr[0]=new student; ptr[1]=&data;
student data2; student data3;
ptr[2]=&data2; ptr[3]=&data3;
readValue(ptr); displayValue(ptr);
return 0; }
Explanation / Answer
The modified program is as follows:
The modifications are:
1. readValue has return value changed from string to void
2. cin.ignore() has been removed before cin>>ptr[i]->gpa; in readValue.
3. Two student objects are assigned to ptr 0 and 1
4. Two dynamically created students are assigned to ptr2 and ptr3.
5. Loop variable i is now initialized to 0 instead of 1
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
struct student{
int id;
char name[30];
double gpa;
};
void readValue(student *ptr[]){
for(int i=0; i<4; i++){
cout<<"Enter student"<<i+1<<"'s id: ";
cin>>ptr[i]->id;
cout<<"Enter student"<<i+1<<"'s name: ";
cin.ignore();
cin.getline(ptr[i]->name, 30);
cout<<"Enter student"<<i+1<<"'s GPA: ";
cin>>ptr[i]->gpa;
}
}
void displayValue(student *ptr[]){
for(int i=0; i<4; i++){
The modified program is as follows:
The modifications are:
1. readValue has return value changed from string to void
2. cin.ignore() has been removed before cin>>ptr[i]->gpa; in readValue.
3. Two student objects are assigned to ptr 0 and 1
4. Two dynamically created students are assigned to ptr2 and ptr3.
5. Loop variable i is now initialized to 0 instead of 1
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
struct student{
int id;
char name[30];
double gpa;
};
void readValue(student *ptr[]){
for(int i=0; i<4; i++){
cout<<"Enter student"<<i+1<<"'s id: ";
cin>>ptr[i]->id;
cout<<"Enter student"<<i+1<<"'s name: ";
cin.ignore();
cin.getline(ptr[i]->name, 30);
cout<<"Enter student"<<i+1<<"'s GPA: ";
cin>>ptr[i]->gpa;
}
}
void displayValue(student *ptr[]){
for(int i=0; i<4; i++){
cout<<"Student"<<i+1<<"'s id: "<<ptr[i]->id<<endl;
cout<<"Student"<<i+1<<"'s name: "<<ptr[i]->name<<endl;
cout<<"Student"<<i+1<<"'s GPA: "<<ptr[i]->gpa<<endl;
}
}
int main(){
student data1;
student data2;
student *ptr[4];
ptr[0] = &data1;
ptr[1] = &data2;
ptr[2]= new student;
ptr[3]= new student;
readValue(ptr);
displayValue(ptr);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.