Program runs in dev-c++5.5.3 version
Use of the structure body
Assign a value to the structure, print out the structure of the student's name, score, average
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string.h>//don't have to. h then maybe the strcpy below won't work.
#include <iterator>
using namespace Std;
int main ()
{
struct Student//declaring struct type Student
{
Char name[20];
float Score1;
float Score2;
float Score3;
}student1, Student2; Defines a variable of two struct type student Student1,student2
strcpy (Student1.name, "Xiao Yang");
student1.score1=60;
student1.score2=70;
student1.score3=80;
strcpy (Student2.name, "Xiao Li");
student2.score1=96;
student2.score2=97;
student2.score3=98;
int a=0;
A= (Student1.score1+student1.score2+student1.score3)/3;
cout<< "Student 1 Name:" << student1.name<<endl;
cout<< "Account 1:" << student1.score1<<endl;
cout<< "Account 2:" << student1.score2<<endl;
cout<< "Account 3:" << student1.score3<<endl;
cout<< "account sharing:" << a<<endl;
A= (Student2.score1+student2.score2+student2.score3)/3;
cout<< "Student 2 Name:" << student2.name<<endl;
cout<< "Account 1:" << student2.score1<<endl;
cout<< "Account 2:" << student2.score2<<endl;
cout<< "Account 3:" << student2.score3<<endl;
cout<< "account sharing:" << a<<endl;
return 0;
}
-----------------------------------------------------------------------------------------------
C + + finishes reading the. txt file, printing everything to the screen:
#include <iostream>
#include <fstream>
#include <string>
using namespace Std;
int main ()
{
Ifstream in;
In.open ("D:\\haha.txt");//file must be separated by a double slash between each layer
if (!in)
{
cout<< "Wrong" <<endl;
return 1;
}
Char ch;
while (!in.eof ())
{
In.read (&ch,1);
Get (a)
Cout<<ch;}
}
In.close ();
return 0;
}
-----------------------------------------------------------------------------------------------
Read. txt files students ' names, scores, average scores
TXT file only need to have a space, the program can be distinguished, TXT file content is as follows {
Lamb 65 69 68
Xiao Li 94 89 98
}
#include <iostream>
#include <fstream>
using namespace Std;
struct Student
{
Char name[20];
int S1;
int S2;
int S3;
int average;
};
const int n=2;
int main ()
{
int I, stunum=0;
Student Stu[n];
Ifstream infile ("Haha.txt", ios::in);
if (!infile)
{
cout<< "Open error!" <<endl;
return 0;
}
i=0;
while (!infile.eof ())
{
infile>>stu[i].name>>stu[i].s1>>stu[i].s2>>stu[i].s3;
Stu[i].average= (STU[I].S1+STU[I].S2+STU[I].S3)/3;
++stunum;
++i;
}
Infile.close ();
Display
for (i=0; i<stunum; ++i)
{
cout<<stu[i].name<<endl;
cout<< "Guangzi are:" <<stu[i].s1<< "" <<stu[i].s2<< "<<stu[i].s3<<" "<< Endl
cout<< "Average score:" <<stu[i].average<<endl;
}
return 0;
}
C + + Starter Program Job 2