(C++)小白来提问,程序出错。

#include
using namespace std;

double capital = 0.0;

double cal(int num, double quantity)
{
switch (num)
{
case 0:
return quantity*1.50;
case 1:
return quantity*1.70;
case 2:
return quantity*4.60;
case 3:
return quantity*0.90;
case 4:
return quantity*2.50;
case 5:
return quantity*3.70;
case 6:
return quantity*7.60;
case 7:
return quantity*3.90;
}
}

int main()
{
cout << "提示:本店提供8种水果,如下(左边为水果编号及名称,右边为其单价)" << endl;
cout << "0西瓜 1.50,1木瓜 1.70,2哈密瓜 4.60,3苹果 0.9\n" << "4梨 2.50,5柚子 3.70,6猕猴桃 7.60,7山楂 3.90" << endl;
int num_choice = 0;
while (1)
{
int Y_N;
cout << "请输入你你要购买的种类数量:";
cin >> num_choice;

    if (num_choice == 0)
    {
        cout << "您确定只是看看吗?要不买点吧?继续请按1,退出请按0" << endl;
        cin >> Y_N;
        if (Y_N == 0)
        {
            cout << "退出成功!" << endl;
            break;
        }
        else if (Y_N == 1)
        {
        }
        else
            cout << "不要捣乱,请按规定输入哦,亲" << endl;
    }
    else
    {
        cout << "种类编号分别为:";
        int *choice = new int[num_choice];
        double *quantity_choice = new double[num_choice];
        for (int i = 0; i < num_choice; i++)
            cin >> choice[i];
        cout << "请输入对应购买种类的重量:";
        for (int i = 0; i < num_choice; i++)
            cin >> quantity_choice[i];

        for (int j = 0; j < num_choice; j++)
        {
            capital += cal(choice[j], quantity_choice[j]);
        }

        cout << "继续购物吗?继续请按1,否则请按0,开始结算" << endl;
        cin >> Y_N;
        if (Y_N == 0)
        {
            cout << "开始结算,请付款:" << capital << "元" << endl;
            break;
        }
        else if (Y_N == 1)
        {
        }
        else
            cout << "猴子你又调皮了,按规定输入哦" << endl;
    }
}
delete[] choice;
choice = NULL;
delete[] quantity_choice;
quantity_choice = NULL;
system("pause");
return 0;

}

以上是程序,请无视掉小白的system("pause")
以下是错误提示
error C2065: “choice”: 未声明的标识符
error C2541: “delete”: 不能删除不是指针的对象
error C2065: “choice”: 未声明的标识符
error C2065: “quantity_choice”: 未声明的标识符
error C2541: “delete”: 不能删除不是指针的对象
error C2065: “quantity_choice”: 未声明的标识符

应该是因为自己不熟悉int *XXXXX=new int[XXX]的用法,哪位大咖来给讲一下,谢谢了。

choice
quantity_choice
的作用域是语句组的大括号内,现在不在此范围,编译器不知道这个名字,所以错误
warning C4715: “cal”: 不是所有的控件路径都返回值

你要加上一个 defult :分支,并返回一个值,
或者在函数最后返回一个值

这样才能保证 函数每个路径,有个正确的返回值,
不然必然有一条路径,返回一个不确定的值

你的用法没问题,只是choice 出了大括号就看不见了。应该在else{}里面写delete

int *XXXXX=new int[XXX]

XXXXX是int指针,它指向xxx数组的首地址,访问数组元素可以用xxx,也可以用 XXXXX+index

想了想,在while循环里面声明choice指针可能无法被识别于是在循环之前便声明了
int *choice = new int[num_choice];
double *quantity_choice = new double[num_choice];


可是新的问题就出现了,再次警报:
warning C4715: “cal”: 不是所有的控件路径都返回值

new对象的作用于要比delete的对象作用域要大,如果小了会导致指定对象无法找到。
cal函数之针对了0-7的返回,没有返回0-7之外的条件值!

没有default,switch的选项只有0-7,没有其他的可行值

作用域没对啊,放在大的else里面吧