Question

Write in c++ please Read and write 5 students information such as, Student ID, first name,...

Write in c++ please

Read and write 5 students information such as, Student ID, first name, last name, GPA and Student Major from the txt file using Structure.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Student{ //defining the structure to store the data given in the data file
   int id;
   char first[10];
   char last[10];
   float GPA;
   char major[5];
};
int main () {
string in;
Student stud[5];
ifstream inFile("data.txt"); //reding the file
if (!inFile){
cerr << "File can't be opened! " << endl; //display error if the file can't be opened
system("PAUSE");
exit(1);
}
for(int i=0; i<5; i++){
   inFile>>stud[i].id>>stud[i].first>>stud[i].last>>stud[i].GPA>>stud[i].major; //inserting the values of the file in the student record
}
for(int i=0; i<5; i++){
   cout<<stud[i].id<<endl; //Displaying the output to the screen
   cout<<stud[i].first<<endl;
   cout<<stud[i].last<<endl;
   cout<<stud[i].GPA<<endl;
   cout<<stud[i].major<<endl;
}
return 0;
}

data.txt:

Output:

Please give it a thumbs up if this helped you, also provide your valuable feedback.

Add a comment
Know the answer?
Add Answer to:
Write in c++ please Read and write 5 students information such as, Student ID, first name,...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT