Question

14.14 Structure - read and print structure Assuming you have the following structure struct Student {...

14.14 Structure - read and print structure

Assuming you have the following structure

struct Student {
    int id;
    string name;
    float GPA;
};

You are required to read students' information from an input file whose filename is provided by standard input; Please use std::cout to output students' name who have a GPA > 3.0 in the reversed order.

input

id=1234567 name=Peter GPA=3.6
id=1234568 name=Adina GPA=3.8
id=1234569 name=Mike GPA=3.0
id=1234569 name=Gina GPA=2.9

Note: input file may be empty.

output

Adina
Peter

step 1, count the numbers

step 2, create a dynamic array

step 3, read information into the created dynamic array

step 4, writer students' names in the reversed order to the screen

Homework Answers

Answer #1

Code :

Instruction:

Count of data is done using getline.

Then dynamic array created and data stored in that array.

Then data retrieved for GPA > 3.0 and for reverse order that is accessed from last index.

Input the file name with extension as shown in output image.

---------------------------------------------------------------------------------------------------------------------------------------------------

#include <bits/stdc++.h>

using namespace std;

struct Student {
int id;
string name;
float GPA;
};

int main()
{
string inpfile,temp;
cout<<"Enter the file name with extension:"<<endl;
cin>>inpfile;
ifstream fileData;
fileData.open(inpfile);
if(!fileData)
{
cout<<"Error in file opening."<<endl;
return -1;
}
  
int noOfData = 0;
while(getline(fileData, temp))
{
noOfData++;
}
Student* studentData = new Student[noOfData];
  
for(int i=0;i<noOfData;i++)
{
fileData>>studentData[i].id>>studentData[i].name>>studentData[i].GPA;
}
  
for(int i = (noOfData-1); i >=0; i--)
{
if(studentData[i].GPA > 3.0)
{
cout<<studentData[i].name<<endl;
}
}
  
delete [] studentData;

fileData.close();

return 0;
}


Code image 1

Code image 2

Code image 3

Data image

Output image

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
When doing the File IO_struct program, did you notice that the design of the data structure...
When doing the File IO_struct program, did you notice that the design of the data structure can be improved? The problem is that some data are redundant in the struct/array structure. For example, the word “Amy” appears three times in the data structure. In real industry systems, no redundant data is allowed, since data in this kind of systems cannot be easily updated, deleted, added or saved. It is extremely error-prone. Thus in practice this type of designs should be...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read code and all comments carefully): Student.h StudentList.h StudentList.cpp main.cpp This program: Creates a sorted linked list (student name and gpa) . The list is sorted in ascending order by name. Displays the list Read and understand this program, then do the following: finish writing Student.h and other files fix errors in StudentList.cpp Student.h: #ifndef STUDENT_H #define STUDENT_H //using namespace std; //<==== This statement //...
n this lab, you use what you have learned about parallel arrays to complete a partially...
n this lab, you use what you have learned about parallel arrays to complete a partially completed C++ program. The program should: Either print the name and price for a coffee add-in from the Jumpin’ Jive Coffee Shop Or it should print the message Sorry, we do not carry that. Read the problem description carefully before you begin. The file provided for this lab includes the necessary variable declarations and input statements. You need to write the part of the...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...