简体   繁体   English

当程序根据用户输入再次循环时,它会导致分段错误:11

[英]When program is expected to loop through again based on user input, it instead results in Segmentation Fault: 11

I have made a simple rock-paper-scissors program in C++ and have tried to implement the main.cpp file in such a way that after playing a game, the user is asked for input if they want to play again. 我用C ++制作了一个简单的摇滚剪刀程序,并尝试以这样一种方式实现main.cpp文件,即在玩游戏后,如果用户想要再次播放,则会要求用户输入。 A response of 'y' should run through the program again allowing the user to enter another name and move choice, however the program will only run successfully once and then result in: RUN FINISHED: segmentation fault : 11. 'y'的响应应该再次通过程序,允许用户输入另一个名称并移动选择,但程序只能成功运行一次然后导致:RUN FINISHED:分段错误:11。

Code: 码:

main.cpp main.cpp中

#include <iostream>
#include <string>
#include "rock-paper-scissorss.h"
using namespace std;

void whoWon(string player_move, string computer_move){
    if(player_move == "rock"){
        if(computer_move == "paper")
            cout<<"You chose rock and the computer chose paper, the computer wins!"<<endl;

        else if(computer_move == "scissors")
            cout<<"You chose rock and the computer chose scissors, you win!"<<endl;

        else
            cout<<"You chose rock and the computer chose rock, it's a tie!"<<endl;
    }

    else if(player_move == "paper"){
        if(computer_move == "scissors")
            cout<<"You chose paper and the computer chose scissors, the computer wins!"<<endl;

        else if(computer_move == "rock")
            cout<<"You chose paper and the computer chose rock, you win!"<<endl;

        else
            cout<< "You chose paper and the computer chose paper, it's a tie!"<<endl;
    }

    else{
        if(computer_move == "rock")
            cout<<"You chose scissors and the computer chose rock, the computer wins!"<<endl;

        else if(computer_move == "paper")
            cout<<"You chose scissors and the computer chose paper, you win!"<<endl;

        else
            cout<<"You chose scissors and the computer chose scissors, it's a tie!"<<endl;
    }
}

int main(int argc, char** argv) {

  char play_again = 'y';

  while(play_again == 'y'){
    cout<<"Please enter your name: "<<endl;
    string name;
    getline(cin,name);
    cout<<"Hello "<<name<<", welcome to Rock-Paper-Scissors!"<<endl;

    Player player_1(name);

    cout<<"Please enter the move you wish to make: "<<endl;
    string move;
    getline(cin,move);
    while(1){
        if((move == "rock") || (move == "paper") || (move == "scissors")){

            player_1.setMoveChoice(move);
            break;
        }

        else{
            cout<<"Invalid move choice! Please enter rock, paper, or scissors"<<endl;
            getline(cin,move);
        }
    } 


    Computer com_1;
    com_1.setMoveChoice();

   string p_move = player_1.getMoveChoice();
   string c_move = com_1.getMoveChoice();


   whoWon(p_move,c_move);



   cout<<"Would you like to play again?(y/n)"<<endl;

   cin>>play_again;
   cout<<"your response was: "<<play_again<<endl;



    } 



 return 0;
}

rock-paper-scissors.cpp: 石头纸scissors.cpp:

#include "rock-paper-scissorss.h"
#include <cstdlib>

Computer :: Computer(){
    num_generated = rand() % 3 + 1;
}

void Computer ::  setMoveChoice(){
   if(num_generated == 1)
       move_selected = "rock";

   else if(num_generated == 2)
       move_selected = "paper";

   else
       move_selected = "scissors";

}

string Computer :: getMoveChoice(){
    return move_selected;
}

Computer :: ~Computer(){

    delete this;
}


Player :: Player(string name){
    player_name = name;

}

void Player :: setMoveChoice(string choice){
    move_selected = choice;
}

string Player :: getMoveChoice(){
    return move_selected;
}

Player :: ~Player(){
    delete this;
}

rock-paper-scissorss.h: 石头纸scissorss.h:

#ifndef ROCK_PAPER_SCISSORSS_H
#define ROCK_PAPER_SCISSORSS_H

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



class Computer{

private:
    int num_generated;
    string move_selected;

public:
    Computer();
    void setMoveChoice();
    string getMoveChoice();
    ~Computer();
};

class Player{

private:
    string move_selected;
    string player_name;

public:
    Player(string);
    void setMoveChoice(string move);
    string getMoveChoice();
    ~Player();
};

#endif /* ROCK_PAPER_SCISSORSS_H */

Your class does not need a destructor, as it never explicitly allocates any resources. 您的类不需要析构函数,因为它从不显式分配任何资源。 Remove: 去掉:

  Computer :: ~Computer(){
      delete this;
  }

and all similar functions. 和所有类似的功能。

And be aware that even if your class did need a destructor, the destructor should not call: 请注意,即使您的类确实需要析构函数,析构函数也不应该调用:

delete this;

as this will effectively be performed for you by code emitted by the compiler. 因为这将由编译器发出的代码有效地执行。 The destructor should only delete resources you explicitly allocated, within the object being destroyed, with new . 析构函数应该只delete您明确分配的资源,在对象中被摧毁, new

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

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