Read data to struct Array

Source: Internet
Author: User
/* Reading data from the 2-to-2 project to the student score.txt (paste the self-built data from this article) is the student ID, name, C ++, high number, and English score of some students. It defines an array of student scores, the members include student ID (char num [12]), name (name), score (grade), and total score (score )). (1) read data from the file and store it in your defined struct array; (2) output records of all students in the struct array. */# Include <iostream> # include <cstdlib> # include <fstream> using namespace std; struct student {char num [20]; char name [10]; int cpp; int math; int english; int grade ;}; const int n = 200; int main () {int I, stunum = 0; student stu [n]; ifstream infile ("score.txt", ios: in); if (! Infile) {cerr <"open error" <endl; exit (1) ;} I = 0; while (! Infile. eof () {infile> stu [I]. num> stu [I]. name> stu [I]. cpp> stu [I]. math> stu [I]. english; stu [I]. grade = stu [I]. cpp + stu [I]. english + stu [I]. math; stunum ++; I ++;} infile. close (); for (I = 0; I <stunum; I ++) {cout <stu [I]. num <"\ t" <stu [I]. name <"\ t" <stu [I]. cpp <"\ t" <stu [I]. math <"\ t" <stu [I]. english <"\ t" <stu [I]. grade <endl;} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.