程序在开始输入第三个信息后停止

时间:2013-12-11 16:11:18

标签: c++

/ *这开始但是从Windows给出一条错误消息,说它停止工作并且找不到答案,尝试的运行看起来像:

Enter your stock information.
Company name #1: target
Company symbol #1: tgt
Price per share #1$ 63.39
Shares purchased #1: 456
Company name #2: walmart
Company symbol #2: wmt
Price per share #2$ 73.40
Shares purchased #2: 254
Company name #3: kroger

--------------------------------
Process exited with return value 255
Press any key to continue . . .

我不知道为什么它不起作用,感谢帮助。 * /

#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;

struct StockInfo{
    string compName;
    string compSymbol;
    double pricePer;
    int shares;
};

void getData(StockInfo *);
void displayData(StockInfo *);

int main() {

    StockInfo *stocks;
    stocks = new StockInfo[3];

    getData(stocks);

    displayData(stocks);

    delete [] stocks;
    system("PAUSE");
    return 0;
}
void getData(StockInfo *stocks){

    cout<< "Enter your stock information."<< endl;

    for(int index = 0; index < 3; index++){
        cout<< "Company name #"<< index + 1<< ": ";
        cin.ignore();
        cin.clear();
        cin>> stocks[index + 1].compName;
        cout<< "Company symbol #"<< index + 1<< ": ";
        cin.ignore();
        cin.clear();
        cin>> stocks[index + 1].compSymbol;
        cout<< "Price per share #"<< index + 1<< "$ ";
        cin.ignore();
        cin.clear();
        cin>> stocks[index + 1].pricePer;
        cout<< "Shares purchased #"<< index + 1<< ": ";
        cin.ignore();
        cin.clear();
        cin>> stocks[index + 1].shares;
    }
    system("pause");
    system("cls");
}

void displayData(StockInfo *stocks){

    cout<< "***********************************************\n";
    cout<< "*************My Stock Information**************\n";

    for(int count = 1; count <= 3; count++){

        double tot = 0.00;

        tot = stocks[count].pricePer * stocks[count].shares;

        cout<< setprecision(2)<< endl;
        cout<< setw(20)<< left<< "Company Name:"<< setw(20)<< right<<                     stocks[count].compName<< endl;
        cout<< setw(20)<< left<< "Company Symbol:"<< setw(20)<< right<< stocks[count].compSymbol<< endl;
        cout<< setw(20)<< left<< "Share Price:"<<" $"<< setw(20)<< right<< stocks[count].pricePer<< endl;
        cout<< setw(20)<< left<< "Shares:"<< setw(20)<< right<<     stocks[count].shares<< endl;
        cout<< setw(20)<< left<< "Total:"<<" $"<< setw(20)<< right<< tot<< endl;
    }
}

2 个答案:

答案 0 :(得分:4)

您正在阅读溢出数组维度的stocks[index + 1]。您从0循环到2,包括,这是正确的,但stocks[index + 1]尝试在stocks[3]时尝试访问i == 2,这是“索引越界”错误。

我想这是副本&amp;粘贴问题,因为您要指示用户输入“#1”,“#2”和“#3”的数据。

试试这个:

for(int index = 0; index < 3; index++){
    cout<< "Company name #"<< index + 1<< ": ";
    cin.ignore();
    cin.clear();
    cin>> stocks[index].compName;
    cout<< "Company symbol #"<< index + 1<< ": ";
    cin.ignore();
    cin.clear();
    cin>> stocks[index].compSymbol;
    cout<< "Price per share #"<< index + 1<< "$ ";
    cin.ignore();
    cin.clear();
    cin>> stocks[index].pricePer;
    cout<< "Shares purchased #"<< index + 1<< ": ";
    cin.ignore();
    cin.clear();
    cin>> stocks[index].shares;
}

答案 1 :(得分:0)

代码中的主要错误是因为您试图访问数组中不存在的元素。

stocks[index + 1]

请记住,数组从[0]开始。在您的情况下,当您在getData函数中输入for循环时,您正在访问数组中的“second”元素,而不是通过[index + 1]访问“first”元素。 这意味着第一个元素([0])被初始化为垃圾值,当循环进入最终循环时,您实际上是在尝试访问不存在的数组元素。

以下是您的工作示例:

#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>

using namespace std;

struct StockInfo
{
    string compName;
    string compSymbol;
    double pricePer;
    int shares;
};


void getData(StockInfo *stocks)
{
    cout<< "Enter your stock information."<< endl;

    for(int index = 0; index < 3; index++)
    {
        cout<< "Company name #"<< index + 1<< ": ";
        cin.clear();
        cin>> stocks[index].compName;
        cout<< "Company symbol #"<< index + 1<< ": ";
        cin.clear();
        cin>> stocks[index].compSymbol;
        cout<< "Price per share #"<< index + 1<< "$ ";
        cin.clear();
        cin>> stocks[index].pricePer;
        cout<< "Shares purchased #"<< index + 1<< ": ";
        cin.clear();
        cin>> stocks[index].shares;
    }
}

void displayData(StockInfo *stocks)
{
    cout<< "***********************************************\n";
    cout<< "*************My Stock Information**************\n";

    for(int count = 0; count < 3; count++)
    {
        double tot = 0.00;

        tot = stocks[count].pricePer * stocks[count].shares;

        cout<< setprecision(2)<< endl;
        cout<< setw(20)<< left<< "Company Name:"    << setw(20) << right    << stocks[count].compName   << endl;
        cout<< setw(20)<< left<< "Company Symbol:"  << setw(20) << right    << stocks[count].compSymbol << endl;
        cout<< setw(20)<< left<< "Share Price:"     << " $"     << setw(20) << right                    << stocks[count].pricePer<< endl;
        cout<< setw(20)<< left<< "Shares:"          << setw(20) << right    << stocks[count].shares     << endl;
        cout<< setw(20)<< left<< "Total:"           <<" $"      << setw(20) << right<< tot              << endl;
    }
}

int main() 
{
    StockInfo *stocks = new StockInfo[3];

    getData(stocks);

    displayData(stocks);

    delete [] stocks;
}

请注意,在使用命名空间时,如果您不小心,可以并且将使用具有相似名称的其他函数创建碰撞错误。我建议你使用std :: string, std :: cout等等。它更安全!

另外,您是否考虑过使用模板容器?您的示例很简单,您可以使用数组,但仍然不会伤害您以了解更好的方法。 (std :: vector&lt;&gt;本质上是一个动态数组,但也会为你清除内存。它还会计算你的数组的大小,你不必在每个函数ex.3中硬编码大小)< / p>

希望这有帮助!