学习C++与指针和数组有关的问题❤️😘💕

在学习c++,用的书是primer plus 第六版,做了第七章第二道练习题:要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。请使用3个数组处理函数来分别进行输入、显示和计算平均成绩。 编译环境:vs2022 c++14

我大体的功能都实现了,虽然有些小瑕疵。在这里有一些疑问,望解惑!!
1.在这里最多是10个高尔夫成绩,我却定义了一个11个元素的数组,为了在最后一个元素中放入‘0’这个控制字符,有没有更好的方法,不需要‘0’就能实现;
2.为了防止输入非double的字符,我在程序中加入了一个纠错机制,但是我输入非double字符以后,却发现程序运行出现了 断点调试错误 ,百思不得姐。

#include<iostream>
using namespace std;
double* input();
void output(double*);
double average(double*);
int main()
{
    double* p;
    cout << "Please enter the scores(minus number to end):" << endl;
    p = input();
    if (*p == -1.1)
    {
        http:delete []p;
        return 0;
    }
    output(p);
    cout << average(p);
    goto http;
}
double* input()
{
    double* p = new double[11];
    *(p + 10) = 0;
    double temp;
    int i = 0;
    double error[] = { -1.1 };
    for (i = 0; i < 10; i++)
    {
        cout << "Enter score #" << i + 1 << ": ";
        cin >> temp;
        if (!cin) {
            cin.clear();
            cin.sync();
            while (cin.get() != '\n')
                continue;
            cout << "Bad input; input process terminated." << endl;
            p = error;
            return p;
        }
        else if (temp < 0)
        {
            for (int j = i; j < 10; j++)
                *(p + j) = 0;
            break;
        }
        *(p+i) = temp;
    }
    return p;
}
void output(double* p)
{
    int i = 0;
    while (*(p+i) != 0)
    {
        cout << *(p + i) << " ";
        ++i;
    }
    cout << endl;
}
double average(double* p)
{
    int i = 0;
    double sum = 0, mean = 0;
    while (*(p + i) != 0)
    {
        sum += *(p + i);
        ++i;
    }
    mean = sum / i;
    return mean;
}

img


有这个窗口在的时候,微信和qq的即使截图功能都失效,只能保存截图为文件,而不能复制截图到剪切板。

望da lao点拨啊,头都要秃了!!

\0是对字符串而言的,整形数组不需要


可以看下cpp参考手册中的 c++ 获得指向底层数组的指针-data