我在波纹管代码中有问题 [英] I have problem in bellow code
本文介绍了我在波纹管代码中有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
它表示需要持续表达
3个arraays有什么问题?
我尝试了什么:
it says constant expression required
what is the problem with 3 arraays?
What I have tried:
#include<iostream.h>
#include<conio.h>
int main()
{
int n,i=0,m=0,unit[n];
float grade[n];
char name[n];
cout<<"How many lessons did you take?";
cin>>n;
for(i;i<n;i++)
{
cout<<"\n Enter the name of your lesson"<<": \t";
cin>>name[i];
cout<<"\n Enter the units"<<": \t";
cin>>unit[i];
cout<<"\n Enter the grade"<<": \t";
cin>>grade[i];
}
cout<<"\n your resaults:";
cout<<"\n Lesson \t"<<"Units \t"<<"\t Grade \n";
for(m;m<n;m++)
{
cout<<name[m]<<"\t"<<unit[m]<<"\t"<<"\t"<<grade[m];
}
getch();
return 0;
}
推荐答案
嘿,C ++
不是<$ ç$ C> C 。尝试
Hey man,C++
is notC
. Try
using namespace std;
struct Lesson
{
string name;
int unit;
double grade;
};
ostream & operator << ( ostream & os, const Lesson & lesson);
istream & getLesson( istream & is, ostream & os, Lesson & lesson);
int main()
{
size_t n;
vector <Lesson> vl;
cout << "how many lessons did you take?\n";
cin >> n;
for (size_t l = 0; l<n && cin; ++l)
{
Lesson lesson;
if ( getLesson(cin, cout, lesson) )
vl.push_back(lesson);
}
for (const auto & lesson : vl)
cout << lesson << "\n";
}
ostream & operator << ( ostream & os, const Lesson & lesson)
{
os << lesson.name << "\t" << lesson.unit << "\t" << lesson.grade;
return os;
}
istream & getLesson( istream & is, ostream & os, Lesson & lesson)
{
os << "enter the name:\t";
is >> lesson.name;
os << "enter the unit:\t";
is >> lesson.unit;
os << "enter the grade:\t";
is >> lesson.grade;
return is;
};
无论您使用什么版本的C ++,都无法声明动态大小的数组n
在知道n
之前。
Whatever the version of C++ you use, you can not declare arrays of dynamic sizen
before knowingn
.
int n,i=0,m=0;
cout<<"How many lessons did you take?";
cin>>n;
int unit[n];
float grade[n];
char name[n];
如果这不起作用,则需要查看分配动态数组的语法
if this don't work, you will need to see syntax to allocate dynamic arrays
这篇关于我在波纹管代码中有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文