ISO C&x2B+;禁止可变长度数组';向上';[-Wvla]在我的代码中 ISOC++禁止可变长度数组“UP”[Wvla ] < /强>
此代码出现此错误。此代码是关于商店的帐单生成器的。它接受100种商品的单价和数量,返回小计,并且必须手动输入折扣。我使用函数中的数组来编写此程序。但最终发生了此错误ISO C&x2B+;禁止可变长度数组';向上';[-Wvla]在我的代码中 ISOC++禁止可变长度数组“UP”[Wvla ] < /强> ,c++,arrays,C++,Arrays,此代码出现此错误。此代码是关于商店的帐单生成器的。它接受100种商品的单价和数量,返回小计,并且必须手动输入折扣。我使用函数中的数组来编写此程序。但最终发生了此错误 #include <iostream> using namespace std; int myVar=100; void myinput (int up[],int qty[], int dr[]){ cout<<"******************The Bill*************
#include <iostream>
using namespace std;
int myVar=100;
void myinput (int up[],int qty[], int dr[]){
cout<<"******************The Bill******************"<<endl;
for (int i=0;i<myVar;i++)
{
cout<<"____________________________________________"<<endl;
cout<<"\nInput UNIT PRICE of the *"<<(i+1)<<"* item :";
cin>>up[i];
cout<<"Input QUANTITY of the *"<<(i+1)<<"* item :";
cin>>qty[i];
cout<<"Input DISCOUNT RATE of the *"<<(i+1)<<"* item :";
cin>>dr[i];
cout<<"____________________________________________"<<endl;
cout<<""<<endl;
}
}
void mytot(int up[],int qty[], int tot[]){
for (int t=0;t<myVar;t++)
{
tot[t]=up[t]*qty[t];
}
}
void mydis(int tot[],int dr[], int dis[]){
for (int a=0;a<myVar;a++)
{
dis[a]=tot[a]*dr[a]/100.0;
}
}
void myst(int subtot[],int tot[],int dis[]){
for(int g=0;g<myVar;g++)
{
subtot[g]=tot[g]-dis[g];
}
}
int mygt(int subtot[]){
int gtot=0;
for(int g=0;g<myVar;g++)
{
gtot=gtot+subtot[g];
}
return gtot;
}
void myout(int subtot[],int gtot){
for(int o=0;o<myVar;o++)
{ cout<<"____________________________________________"<<endl;
cout<<"ITEM "<<o+1<<" SUB TOTAL IS - "<<subtot[o]<<" "<<endl;
}
cout<<"____________________________________________"<<endl;
cout<<"GRAND TOTAL IS * "<<gtot<<"*";
cout<<"\n============================================"<<endl;
}
int main(){
int up[myVar];
int qty[myVar];
int dr[myVar];
int dis[myVar];
int tot[myVar];
int subtot[myVar];
int gtot=0;
myinput(up,qty,dr);
mytot(up,qty,tot);
mydis(tot,dr,dis);
myst(subtot,tot,dis);
gtot= mygt(subtot);
myout(subtot,gtot);
cout<<"\n\n******************Thank You,Come Again******************"<<endl;
}
#包括
使用名称空间std;
int-myVar=100;
无效myinput(整数上升[],整数数量[],整数下降[]){
cout问题在于myVar
是一个非常量变量,您不能在堆栈上定义长度非常量的数组。对此,有几种解决方案:
- 将
myVar
声明为const int myVar=100;
以使其成为一个常量,这适合您的代码示例
- 在堆上分配数组,例如,
int*up=newint[myVar];
- 使用
std::vector
代替数组。vector up=new vector(myVar)
只使用<代码> STD::vector使用C数组,只需制作<代码> MyVa< /Cord>代码> const int /COD>,这样数组大小将是编译时常数,只需初始化某个值就不足以使其作为数组大小使用。<代码> MyVa<代码>是一个变量,不是编译时常数。C++中不存在。ror错误很好地解释了这一点
函数您有静态数组,其大小无法在编译时确定。解决此问题的一种方法是使用std::vector
,这可能也会让您免于其他一些麻烦,或者您可以使myVar
成为const int
严格来说没有什么可解决的,您根本无法使用stati具有动态大小的c数组。请改用std::vector