C++ 当程序运行时,数组和给出了错误的答案

C++ 当程序运行时,数组和给出了错误的答案,c++,C++,程序运行平稳,编译时没有错误或警告,只是当它得到最终结果时,不管我输入什么,我都会得到一堆随机的字母和数字 代码如下: #include <iostream> #include <string> using namespace std; int main() { int hold; int n; int * result = new int; int * price = new int; std::string ite

程序运行平稳,编译时没有错误或警告,只是当它得到最终结果时,不管我输入什么,我都会得到一堆随机的字母和数字

代码如下:

#include <iostream>
#include <string>

using namespace std;

int main()
{
     int hold;
     int n;
     int * result = new int;
     int * price = new int;
     std::string items[6];

        for (n=0; n<6; n++)
        {
            cout << "Item#" << n+1 << ": ";
            cin >> items[n];
        }
        cout <<  "\nYou Entered: ";
        for (int n=0; n<6; n++)
            cout << items[n] << ", ";

    for (n=0; n<6; n++)
    {
        if (items[n] == "ab"){
        price[n] = 2650;
        }

        else if (items[n] == "ae"){
        price[n] = 1925;
        }

        else if (items[n] == "ie"){
        price[n] = 3850;
        }

        else if (items[n] == "bt"){
        price[n] = 3000;
        }

        else if (items[n] == "pd"){
        price[n] = 2850;
        }

        else if (items[n] == "ga"){
        price[n] = 2600;
        }

    }

    for (n=0; n<6; n++)
    {
     result += price[n];
    }

    cout << "\nTotal gold for this build: " << result;
    cin >> hold;
    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
int hold;
int n;
int*结果=新的int;
整数*价格=新整数;
std::字符串项[6];
对于(n=0;n
及

分别分配一个
int
。您的意思可能是
newint[6]

但同样,您应该使用
std::vector

我真的很失望,你没有听取别人的建议--如果你听取了,你现在就不会有这个问题了。这不是一个好的学习方法

分别分配一个
int
。您的意思可能是
newint[6]

但同样,您应该使用
std::vector


我真的很失望,你没有听取任何建议--如果你有,你现在就不会有这个问题了。这不是一个好的学习方法。

有了这个声明:
int*price=new int;
你只为一个
int
分配空间,但是你继续使用
price
作为
int
的数组

要声明数组,请使用:
int*price=newint[5];


对于
result
,您也将其声明为指向
int
的指针,但随后将其用作
int
result+=price[n]
。无需将
result
作为指针。还要注意的是,您需要显式初始化变量:在开始使用
result
之前,将其设置为零。

使用此声明:
int*price=new int;
您只为单个
int
分配空间,但您继续使用
price
int
的数组

要声明数组,请使用:
int*price=newint[5];


对于
result
,您也将其声明为指向
int
的指针,但随后将其用作
int
result+=price[n]
。无需将
result
作为指针。还要注意的是,您需要显式初始化变量:在开始使用
result
之前,将其设置为零。

嗯,
result
是一个
int*
。这种变量通常存储另一个整数变量的地址,您可以通过
new i>获得该地址nt
在这种特定情况下。但是

 result += price[n];
您将修改该地址,如果实际从
*result
写入/读取,则会导致分段错误。这也是您输出奇怪的原因:

cout << "\nTotal gold for this build: " << result;

cout好的,
result
是一个
int*
。这种变量通常存储另一个整数变量的地址,在这种特定情况下,您可以使用
new int
获得该地址。但是

 result += price[n];
您将修改该地址,如果实际从
*result
写入/读取,则会导致分段错误。这也是您输出奇怪的原因:

cout << "\nTotal gold for this build: " << result;
cout更改行:

cout << "\nTotal gold for this build: " << result;
前面的声明声明了变量,而不是数组更改行:

cout << "\nTotal gold for this build: " << result;

前面的声明声明声明了一个变量,而不是数组

请给出一些注释:

  • 新运算符应与delete一起使用
  • 您声明的“int*result”是指向int的点,因此您应该取消对该点的引用以获得所需的结果
  • 应考虑例外情况,如果输入字母不在给定列表中怎么办

  • 请给出一些评论:

  • 新运算符应与delete一起使用
  • 您声明的“int*result”是指向int的点,因此您应该取消对该点的引用以获得所需的结果
  • 应考虑例外情况,如果输入字母不在给定列表中怎么办

  • 好的,我将
    int*result=new int;
    更改为
    int result;
    ,现在我得到了一个数字作为我的结果,但它仍然是错误的数字,即使我已经使用了6次“ae”,它也没有将我在if语句中输入的任何值相加。@user1742497:真正的罪魁祸首是
    result+=price[n]
    。但这段代码中还有许多其他缺陷,包括Luchian已经展示的缺陷。好吧,我将
    int*result=new int;
    更改为
    int result;
    现在我得到了一个数字作为结果,但它仍然是错误的数字,它没有将我在if语句中输入的任何值相加,即使我使用了“ae”例如6次。@user1742497:真正的罪魁祸首是
    result+=price[n]
    。但是这段代码中还有很多其他的缺陷,包括Luchian已经展示的缺陷。感谢我的回复,我正在研究它。我对这东西是新的,所以我必须通读如何在我不知道它是什么的情况下使用它。感谢我在研究它的回复,我对这东西是新的,所以我必须通读如何在我不知道它是什么的情况下使用它我不知道是什么。
    int *price = new int[6];