为什么我的c ++程序终止于Eclipse中没有输出但在Visual Studio中有效?

时间:2012-11-16 18:05:27

标签: c++ eclipse

我的程序在Windows上的visual studio中运行,但是当我尝试在Ubuntu上使用它时,它会在我尝试运行时终止,而根本没有显示任何输出。

Eclipse正在使用Linux GCC工具包。

我没有编译器错误或警告或任何东西,控制台只是显示它已终止然后空白。

我进入调试并想出了

购物[1188] [核心:1]
    线程[1] 1188 [核心:1](暂停:信号:SIGSEGV:分段故障)         0x7ffff7b78bca
        std :: string :: assign()位于0x7ffff7b79ff6         basic_string.h中的operator =():542 0x402f00
        shop.cpp上的fillInventory():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;
}

1 个答案:

答案 0 :(得分:6)

正如"No Console Output (MinGW, CDT)"的人指出的那样,这可能(或者至少有时)因为Eclipse在启动可执行文件时没有将PATH添加到MINGW \ bin中,所以:

在“环境”标签中,按“新建”,将其设置为:

  • “姓名:PATH”
  • “值:C:\ MinGW的\ BIN”