用VS2010 C++时,出现的一些问题,求高手,回答好了加分

2024-12-19 14:49:22
推荐回答(1个)
回答1:

1.endl是end of line的简写,是换行符,使光标自动到下一行
2.c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.为了与c语言兼容,不用在意
3.定义的那几个整型变量,类型全部改成double(或float型),否则你算出来的都是整数。是否初始化为0无所谓,反正后面还有赋值的。
4.每个while上面都要加一个total=0;i=0; 这只是重新赋值,不是重新定义,不要加类型int
5.for跟while实质相同,形式见下边代码。
6.最后改成system("pause");即可

#include "stdafx.h"
#include< iostream >
#include< string >
using namespace std;
struct student_info
{
string strName;
double maths;
double chinese;
double english;

};
student_info student[2];
int main()
{
int i=0;
char a[15];
for( i=0;i<2;i++)
{
cout<<"Enter the "< cout<<"Enter the name:"< cin>>a;
student[i].strName=a;
cout<<"Enter chinese performance"< cin>>student[i].chinese;
cout<<"Enter maths performance"< cin>>student[i].maths;
cout<<"Enter english performance"< cin>>student[i].english;
}

for( i=0;i<2;i++)
{
cout<<"The "< cout<<"Name:"<
cout<<"Maths:"< cout<<"chinese:"< cout<<"engligh:"< }

double total=0;
double avgMaths;
double avgChinese;
double avgEnglish;

i=0;
for(i=0;i<2;i++)
total+=student[i].maths;
avgMaths=total/2;

i=0;
total=0;
while(i<2)
{
total+=student[i].chinese;
i++;
}
avgChinese=total/2;

i=0;
total=0;
while(i<2)
{
total+=student[i].english;
i++;
}
avgEnglish=total/2;

cout<<"Enter 1 for look up the average of Maths.\n"
<<"Enter 2 for look up the average of Chinese.\n"
<<"Enter 3 for look up the average of English.\n";
cin>>i;
switch(i)
{
case 1:
cout<<"Avgrage of maths is :"< break;
case 2:
cout<<"Avgrage of chinese is :"< break;
case 3:
cout<<"Avgrage of english is :"< break;
default:
cout<<"please enter 1,2 or 3;"< }
system("pause");
}