高手帮我看一下这个C++编程错误

#include<iostream>
using namespace std;
int main()
{
char k='f';
int i=0,j=0,s=0;
while(k!='n')
{
double a[5],b[1];
cout<<"请输入学生的学号:";
cin>>b[j];
cout<<endl;
j++;
cout<<"请输入"<<a[j]<<"学生的高数成绩:";
cin>>a[i];
cout<<endl;
s=s+a[i];
i++;
cout<<"请输入"<<a[j]<<"学生的英语成绩:";
cin>>a[i];
cout<<endl;
s=s+a[i];
i++;
cout<<"请输入"<<a[j]<<"学生的c++成绩:";
cin>>a[i];
cout<<endl;
s=s+a[i];
i++;
a[i]=s/3;
i++;
cout<<"如果要结束请按n,按其它键继续:";
cin>>k;
}
while(j>0)
{
while(i>=0)
{
cout<<a[i]; //error C2065: 'a' : undeclared identifier//error C2109: subscript requires array or pointer type
i=i-1;
}
cout<<endl;
j=j-1;
}
return 0;
}
几位高手说的都很对!那个错误我已改正,但问题还没解决!请各位高手在帮忙看一下,不胜感激!首先,我要表达的效果(则在显示器显示的)是:
请输入学生的学号:1
请输入1学生的高数成绩:80
请输入1学生的英语成绩:90
请输入1学生的C++成绩:100
如果要结束请按n,按其它键继续:f
请输入学生的学号:2
请输入2生的高数成绩:80
请输入2生的英语成绩:80
请输入2学生的C++成绩:80
如果要结束请按n,按其它键继续:n
学号 高数 英语 C++ 平均分
1 80 90 100 90
2 80 80 80 80
谢谢cout<<"请输入"<<b[j]<<"学生的高数成绩:";才是对的!
循环体之前声明数组a也是对的,不过出了另外一个问题:循环过后,定义的数组就不够了。

lz
注意这个循环体
你的数组a是在这个循环中声明的
所以是局部变量只能起作用到这个循环体结束.
while(k!='n')
{
double a[5],b[1];
cout<<"请输入学生的学号:";
cin>>b[j];
cout<<endl;
j++;
cout<<"请输入"<<a[j]<<"学生的高数成绩:";
cin>>a[i];
cout<<endl;
s=s+a[i];
i++;
cout<<"请输入"<<a[j]<<"学生的英语成绩:";
cin>>a[i];
cout<<endl;
s=s+a[i];
i++;
cout<<"请输入"<<a[j]<<"学生的c++成绩:";
cin>>a[i];
cout<<endl;
s=s+a[i];
i++;
a[i]=s/3;
i++;
cout<<"如果要结束请按n,按其它键继续:";
cin>>k;
}
此以后就相当于没有声明过数组a
所以改进方法就是放到上面的循环体之前声明a
提到函数main中
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-05-06
#include<iostream>
using namespace std;
int main()
{
char k='f';
int i=0,j=0,s=0;
double a[5],b[1]; //注意把这一行的位置提前!!!!!
while(k!='n')
{
cout<<"请输入学生的学号:";
cin>>b[j];
cout<<endl;
j++;
cout<<"请输入"<<a[j]<<"学生的高数成绩:";
cin>>a[i];
cout<<endl;
s=s+a[i];
i++;
cout<<"请输入"<<a[j]<<"学生的英语成绩:";
cin>>a[i];
cout<<endl;
s=s+a[i];
i++;
cout<<"请输入"<<a[j]<<"学生的c++成绩:";
cin>>a[i];
cout<<endl;
s=s+a[i];
i++;
a[i]=s/3;
i++;
cout<<"如果要结束请按n,按其它键继续:";
cin>>k;
}
while(j>0)
{
while(i>=0)
{
cout<<a[i]; //error C2065: 'a' : undeclared identifier//error C2109: subscript requires array or pointer type
i=i-1;
}
cout<<endl;
j=j-1;
}
return 0;
}
第2个回答  2010-05-06
数组a[]的生命周期超限
改用下面的代码:
#include<iostream>
using namespace std;

int main()
{
char k='f';
int i=0,j=0,s=0;
double a[5],b[1]; //注意:数组在此定义
while(k!='n')
{

cout<<"请输入学生的学号:";
cin>>b[j];
cout<<endl;
j++;
cout<<"请输入"<<a[j]<<"学生的高数成绩:";
cin>>a[i];
cout<<endl;
s=s+a[i];
i++;
cout<<"请输入"<<a[j]<<"学生的英语成绩:";
cin>>a[i];
cout<<endl;
s=s+a[i];
i++;
cout<<"请输入"<<a[j]<<"学生的c++成绩:";
cin>>a[i];
cout<<endl;
s=s+a[i];
i++;
a[i]=s/3;
i++;
cout<<"如果要结束请按n,按其它键继续:";
cin>>k;
}
while(j>0)
{
while(i>=0)
{
cout<<a[i]; //error C2065: 'a' : undeclared identifier//error C2109: subscript requires array or pointer type
i=i-1;
}
cout<<endl;
j=j-1;
}
return 0;
}
第3个回答  2010-05-07
这一句话
j++;
cout<<"请输入"<<a[j]<<"学生的高数成绩:";

// 你这里干嘛 j++ ?
// cout<<a[j] 输出 的是很长的数字
// 应该 cout<<b[j]; 才对吧
第4个回答  2010-05-06
请说出没有解决的问题。
相似回答