简体   繁体   English

运行时错误:程序已停止响应

[英]Runtime error: program has stopped responding

My program is to calculate the price of resources based on the resources that their neighbors have. 我的程序是根据邻居拥有的资源来计算资源的价格。

It compiles, but at run-time, it breaks and says that the program has stopped responding. 它可以编译,但是在运行时会中断并表示程序已停止响应。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

#include <iostream>
#include <fstream>

using namespace std;

class Resource{
    string resName;
    int amount;
    int price;
    public:
        void set_values(string resName, int amount, int price){resName=resName; amount=amount; price=price;};
        string get_name(){return resName;};
        int get_amount(){return amount;};
        int get_price(){return price;}; 
};

class State{
    string stName;
    Resource resources[5];
    public:
        void set_values(string stName){
            stName=stName;
            Resource r; 
            r.set_values(" ", 0, 0);
            for (int i=0; i<5; i++){
                resources[i] = r;
            }
        };
        string get_name(){return stName;};
        void addResource(string name, int amount, int price){
            Resource r;
            r.set_values(name, amount, price);
            for (int i=0; i<5; i++){
                if(resources[i].get_name() == " "){
                    resources[i] = r;
                }
            }
        };
        Resource get_resource(string resource){
            for(int i=0; i<5; i++){
                if (resources[i].get_name() == resource){
                    return resources[i];
                }
            }
        };
        bool checkForResource(string resource){
            for(int i=0; i<5; i++){
                if (resources[i].get_name() == resource){
                    return true;
                }
            }
        }

};

class Area{
    State allStates[10][10];
    public:
        void initialize(){
            for (int i=0; i<10; i++){
                for (int j=0; j<10; j++){
                    allStates[i][j].set_values(" ");    
                }
            }   
        };
        void addState(string stateName){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == " ")
                        allStates[row][col].set_values(stateName);
                }   
            }   
        };
        State get_state(string name){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == name)
                        return allStates[row][col];
                }   
            }
        };
        void deleteState(string name){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == name)
                        allStates[row][col].set_values(" ");
                }   
            }
        };
        void moveRowsSouth(){
            for(int z=0; z<10; z++){
                for(int y=10; y>=1; y--){
                    allStates[y][z] = allStates[y-1][z];
                }
                allStates[0][z].set_values(" ");
            }
        };
        void moveColsEast(){
            for(int z=0; z<10; z++){
                for(int y=10; y>=1; y--){
                    allStates[z][y] = allStates[z][y-1];
                }
                allStates[z][0].set_values(" ");
            }
        };
        void addNeighbor(string s1, string s2, string direction){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == s1)
                        if (direction == "North"){
                            if (row-1 < 0){
                                moveRowsSouth();
                            }
                            else {
                                if (allStates[row-1][col].get_name() == " "){
                                    allStates[row-1][col] = get_state(s2);
                                    deleteState(s2);
                                }
                                else
                                    cout << "Unable to add this neighbor. North is occupied.";
                            }
                        }
                        else if (direction == "South"){
                            if (allStates[row+1][col].get_name() == " "){
                                allStates[row+1][col] = get_state(s2);
                                deleteState(s2);
                            }
                            else
                                cout << "Unable to add this neighbor. South is occupied.";

                        }
                        else if (direction == "East"){
                            if (allStates[row][col+1].get_name() == " "){
                                allStates[row][col+1] = get_state(s2);
                                deleteState(s2);
                            }
                            else
                                cout << "Unable to add this neighbor. East is occupied.";
                        }
                        else if (direction == "West"){
                            if (col-1 < 0){
                                moveColsEast();
                            }
                            else {
                                if (allStates[row][col-1].get_name() == " "){
                                    allStates[row][col-1] = get_state(s2);
                                    deleteState(s2);
                                }
                                else
                                    cout << "Unable to add this neighbor. West is occupied.";
                            }
                        }
                }   
            }
        };
        State getNeighborN(string s1){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == s1)
                        return allStates[row-1][col];
                }   
            }
        };
        State getNeighborS(string s1){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == s1)
                        return allStates[row+1][col];
                }   
            }
        };
        State getNeighborE(string s1){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == s1)
                        return allStates[row][col+1];
                }   
            }
        };
        State getNeighborW(string s1){
            for (int row=0; row<10; row++){
                for (int col=0; col<10; col++){
                    if (allStates[row][col].get_name() == s1)
                        return allStates[row][col-1];
                }   
            }
        };  
};

int main()
{
    Area a;
    a.initialize();
    ifstream infile;
    string command;
    infile.open ("proj2.txt", ifstream::in);
    while(!infile.eof()){
        infile >> command;
        if (command == "addState"){
            string stateName;
            infile >> stateName;
            a.addState(stateName);
        }
        else if (command == "addNeighbor"){
            string state1;
            string state2;
            string direction;
            infile >> state1;
            infile >> state2;
            infile >> direction;
            a.addNeighbor(state1, state2, direction);
        }
        else if (command == "addResource"){
            string state;
            string name;
            int amount;
            int price;
            infile >> state;
            infile >> name;
            infile >> amount;
            infile >> price;
            a.get_state(state).addResource(name, amount, price);
        }
        else if (command == "getPrice"){
            string state;
            string resource;
            int amount;
            infile >> state;
            infile >> resource;
            infile >> amount;
            State N = a.getNeighborN(state);
            State S = a.getNeighborS(state);
            State E = a.getNeighborE(state);
            State W = a.getNeighborW(state);
            int countOfNeigh = 0;
            bool stateHas = false;
            if (N.checkForResource(resource) == true) {
                countOfNeigh++;
            }
            if (S.checkForResource(resource) == true) {
                countOfNeigh++;
            }
            if (E.checkForResource(resource) == true) {
                countOfNeigh++;
            }
            if (W.checkForResource(resource) == true) {
                countOfNeigh++;
            }
            if (a.get_state(state).checkForResource(resource) == true) {
                stateHas = true;
            }
            int price = 0;
            if ((stateHas = false && countOfNeigh == 1) ||
                 (stateHas = true && countOfNeigh == 0)){
                price = a.get_state(state).get_resource(resource).get_price();
                price = price+(price*.25);
            }
            else if (stateHas = true && (countOfNeigh == 2 || countOfNeigh == 3)){
                price = a.get_state(state).get_resource(resource).get_price();
            }
            else if (stateHas = true && countOfNeigh == 4){
                price = a.get_state(state).get_resource(resource).get_price();
                price = price-(price*.1);
            }
            else if (stateHas = false && countOfNeigh > 1){
                price = a.get_state(state).get_resource(resource).get_price();
                price = price+(price*.1);
            }
            cout << "The price for " << amount << " units of " << resource << " is " << price << ".";
        }
    }
}

You're failing to check whether the file open succeeds - http://www.cplusplus.com/reference/iostream/ifstream/open/ 您无法检查文件打开是否成功-http: //www.cplusplus.com/reference/iostream/ifstream/open/

In case it does, your program goes in an infinite loop. 如果确实如此,您的程序将陷入无限循环。

There's also no other kind of error checking. 也没有其他类型的错误检查。 When dealing with reading from files, especially data that you expect to be formatted in some way, that's a shortcut to disaster. 当处理从文件中读取数据时,尤其是您希望以某种方式格式化的数据时,这是灾难的捷径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM