2013-12-11 89 views
0

/*这将启动,但给我从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

所以,你有没有尝试在调试器等运行它... – 2013-12-11 16:12:50

回答

4

您正在读入stocks[index + 1]其中溢出阵列尺寸。您从0循环到2包括,这是正确的,但stocks[index + 1]尝试访问stocks[3]i == 2,这是一个“索引超出界限”错误。

我想这是一个复制&粘贴问题,因为您要指示用户输入“#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; 
} 
+0

啊,是的!建议的解决方法非常正确。谢谢。 – user3091857

2

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

stocks[index + 1] 

请记住,数组从[0]开始。在你的情况下,当你在getData函数中输入for循环时,你正在访问数组中的“第二个”元素,而不是通过[index + 1]来访问“第一个”元素。 这意味着第一个元素([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 <>本质上是一个动态数组,但也会为你清除内存,它也会计算你的数组的大小,并且你不必在每个函数中都硬编码大小)3)

希望这会有所帮助!