从1开始代码而不是0?

时间:2013-05-22 00:56:37

标签: c++

#include<iostream>

using namespace std;
int main()

int quantity = 0;
int a = 0;
int b = 0;
int x = 0;
int y = 0;
int z;
double region[5][6];
int shipping = 20.00;
region[0][0] = 0;


for (x=0;x<5;x++) {

    for (y=0; y<6; y++) {
        z=x;
       if(y == 0)
       {region[x][y] = y + 1 + z;}
       else if(y == 1)
       {region[x][y] =  region[x][y-1] * 2;}
           else if(y == 2)
           {region[x][y] = region[x][y-1]  + .50;}
               else if(y == 3)
                   {region[x][y] = region[x][y-1] + .50;}
                   else if(y == 4)
                   {region[x][y] = region[x][y-1] + .250;}
                       else 
                       {region[x][y] = region[x][y-1] + .250;}
    }

}
cout<<"Please input the region"<<endl;
cin>>a;
cout<<"Enter quantity"<<endl;
cin>>quantity;
if (quantity > 6)
{b = 5;}
else
{b = quantity;}

cout<<"The price of shipping comes out to: "<<region[a][b] + shipping<<" Since $20 is added for the shipping cost"<<endl;

return 0 ;

}

所以现在我必须键入0,0表示区域和数量以获得第一个单元格。但由于我从区域1和数量1开始,我将使用户输入1,1。我将如何使用C ++进行此操作。谢谢!

2 个答案:

答案 0 :(得分:1)

用户进入区域后验证所有值都大于0.如果他们从中减去一个并像处理现在那样处理数据。

if(a < 1) a = 1;
if(quantity < 1) quantity = 1;

// Subtract 1 from the values
a--;
quantity--;

if (quantity > 6)
{b = 5;}
else
{b = quantity;}
cout<<"The price of shipping comes out to: "<<region[a][b] + shipping<<" Since $20 is added for the shipping cost"<<endl;

答案 1 :(得分:0)

如何为每个值减1?

cout<<"Please input the region"<<endl;
cin>>a;
cout<<"Enter quantity"<<endl;
cin>>quantity;

--a;
--quantity;