我的程序在 Windows 上的 Visual Studio 中运行,但是当我尝试在 Ubuntu 上的 Eclipse 中处理它时,它会在我尝试运行时终止,根本不显示任何输出。
Eclipse 正在使用 Linux GCC 工具包。
我没有编译器错误或警告或任何东西,控制台只显示说它终止然后空白的位。
我进入调试并想出了
Shop [1188] [cores: 1]
Thread [1] 1188 [core: 1](暂停:信号:SIGSEGV:Segmentation fault)0x7ffff7b78bca
std::string::assign() at 0x7ffff7b79ff6 operator=() at basic_string.h: 542 0x402f00
fillInventory() at shop.cpp:364 0x402f00
main() at shop.cpp:73 0x401a29
我认为它没有打开输入文件,但我不确定......主要和原型如下:
input file :ball_inventory.txt
output file :ball_reportData.txt
*/
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
//CLASSES and STRUCTURES
struct inventory
{
string name; //name of item
int cost; //cost of item
int stock; //items in stock
int deficit; //items ordered but not provided when out of stock
};
//PROTOTYPES
//Receive and validate input for yes/no prompts.
char yesNo();
//Display shop's logo
void logo();
//Display the low level item menu
void lowLevelMenu(int limit, inventory lowLevelInv[]);
//perform a purchase action for a single individual
int purchase(int limit, inventory lowLevelInv[], int itemTally[]);
//populate the inventory array with name, price, stock, data, initialize deficit to 0.
int fillInventory(ifstream &fin, inventory lowLevelInv[]);
//sort the inventory array by the day's deficit
void sortByDeficit(inventory lowLevelInv[], int limit);
//BODY
/*
* Function Procedure:
* 1. Prompt "customer 1" user for purchases
* 3. Update group total
* 3. Loop until there are no more "customers"
* 4. Output group total
* 5. If no more groups, prompt for password.
* 6. At end of day, output to file, 1 line per item: name, stock, deficit.
*/
int main()
{
const int INV = 20; //sets size of item[] and parallel arrays
char confirm = 'n'; //holds user response to y/n prompts
int groupTot; //Group's total cost
ifstream invSrc; //inventory resource file
ofstream ball_reportData; //output file for report
int limit; //number of populated elements in arrays
string password = "98765"; //sets manager password
string passCheck; //user input to be checked against password
bool exitAllow = 0; //allows program to generate report and exit
int itemTally[INV]; //tally of items ordered by a user
inventory lowLevelInv[INV]; //contains inventory information for the low level inventory
invSrc.open("ball_inventory.txt"); //open inventory file, populate arrays, close inventory file.
/*LINE 73*/ limit = fillInventory(invSrc, lowLevelInv);
invSrc.close();
logo(); //display shop logo
while(!exitAllow) //continue running until password is entered to set exitAllow to true
{
groupTot = 0; //initialize group total due to 0;
groupTot += purchase(limit, lowLevelInv, itemTally); //prompt first customer for purchases and update group total
do
{
cout<<"Are there any more orders for this group? y/n: "; //check for more customers
confirm = yesNo();
if(confirm == 'y') //prompt additional users and update total for each
{
groupTot += purchase(limit, lowLevelInv, itemTally);
}
}while(confirm == 'y');
cout<<"The group's total is: "<<groupTot<<" denarii."; //output total for group
cout<<"\n\nAre you done for the day?"; //confirm for more groups to calculate
confirm = yesNo();
while(confirm == 'y')
{
cout<<"Enter your password:";
cin>>passCheck;
if(password != passCheck)
{
cout<<"Invalid password, try again? ";
confirm = yesNo();
}
else
{
exitAllow = 1;
confirm = 'n';
}
}
}
for(int i = 0; i<limit; i++)
{
cout << lowLevelInv[i].stock << endl;
}
for(int i = 0; i<limit; i++)
{
cout << lowLevelInv[i].deficit << endl;
}
cout << string(20, '-')<<endl;
sortByDeficit(lowLevelInv, limit);
ball_reportData.open("ball_reportData.txt");
for(int i = 0; i<limit; i++)
{
ball_reportData << lowLevelInv[i].name << endl;
ball_reportData << lowLevelInv[i].stock << endl;
ball_reportData << lowLevelInv[i].deficit << endl;
}
cout<<"\n\nPress Enter to finish...";
cin.ignore();
cin.get();
return(0);
}
填充库存:
int fillInventory(ifstream &fin, inventory lowLevelInv[])
{
string name = "junk"; //name to be input to itemArray
string cost;
string stock; //cost to be input to costArray
int i = 0; //incrementer
stringstream convert; //string stream used to input string to int
int max=0; //Number of elements filled in array
while(name != "none") //until "none" in file, fill name/cost arrays
{
getline(fin, name); //get name line
getline(fin, cost); //get cost line
getline(fin, stock);
if(name != "none")
{
/*LINE 364*/ lowLevelInv[i].name = name; //output to name array
convert<<cost; //fill stringstream
convert >> lowLevelInv[i].cost; //output stringstream to cost array
convert.clear(); //clear EOF(?) flag
convert<<stock; //fill stringstream
convert >> lowLevelInv[i].stock; //output stringstream to cost array
convert.clear(); //clear EOF(?) flag
lowLevelInv[i].deficit = 0; //set initial deficit
max++;
}
i++;
}
return max;
}