当程序预期根据用户输入再次循环时,它会导致分段错误:11
When program is expected to loop through again based on user input, it instead results in Segmentation Fault: 11
我在C++中制作了一个简单的石头剪刀布程序,并尝试以这样一种方式实现主.cpp文件,即在玩游戏后,如果用户想再次玩,就会被要求输入。响应"y"应再次通过程序运行,允许用户输入另一个名称和移动选项,但是程序只会成功运行一次,然后导致: 运行完成: 分段错误: 11.
法典:
主.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;
}
石头剪刀布.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;
}
石头剪刀布.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 */
您的类不需要析构函数,因为它从不显式分配任何资源。删除:
Computer :: ~Computer(){
delete this;
}
和所有类似的功能。
请注意,即使您的类确实需要析构函数,析构函数也不应调用:
delete this;
因为这将通过编译器发出的代码有效地为您执行。析构函数应仅delete
在要销毁的对象中显式分配的资源,并使用new
.
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?