【发布时间】:2015-02-12 13:39:32
【问题描述】:
我正在编写一个“Hangman”游戏机应用程序,我想在字符串中添加一个符号而不是另一个符号。
我尝试了擦除、删除和替换功能,但它对我不起作用。因为我在 const char* 中有一个符号,我无法将其转换为 char。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
string conceivedword;
string hint;
string choice;
string first_player_word;
string characterstring;
int counter = 0;
void first_player() {
cout << "Welcome to Hangman! //Firstgamerzone " << endl;
cout << "Please conceive the word" << endl;
getline(cin, conceivedword);
cout << "Type the hint" << endl;
getline(cin, hint);
system("cls");
cout << " The hint is: " << hint << endl;
}
void second_player() {
while (0 == 0) {
cout << "Are you inputting word or character?" << endl;
//while (0 == 0) {
getline(cin, choice);
if (choice == "word" || choice == "sitkva" || choice == "sityva") {
cin >> first_player_word;
if (first_player_word == conceivedword) {
cout << "Congrats! You're winner! shen moige gazqura :D " << endl;
break;
}
else {
counter += 1;
if (counter > 5) {
cout << "Bad luck. You lost. You had only 5 attempts." << endl;
break;
}
cout << "Bad luck. Try again" << endl;
//second_player();
}
}
else if (choice == "character" || choice == "aso") {
cin >> characterstring;
if (characterstring.length() > 1) cout << "Hey,Your Lier! You lost." << endl;
else {
counter += 1;
const char *character = characterstring.c_str();
int exists = conceivedword.find(character);
//cout << exists << endl;
int index = conceivedword.find(character);
if (exists > 0) {
if (counter > 5) {
cout << "You lost. You had only 5 attempts" << endl;
break;
}
cout << "Right. You rock: " << endl;
int length = conceivedword.length();
for (int i = 0; i < conceivedword.length(); i++) {
if (i == index) {
cout << character;
//replace(conceivedword.begin(), conceivedword.end(), '_', character);
}
//cout << conceivedword.length() << endl;
//if (i == index) {
//replace(conceivedword.begin(), conceivedword.end(), '_', '*');
//}
cout << "_" << " ";
//cout << endl;
}
second_player();
cout << endl;
}
if(exists <= 0) {
cout << "Not right. Try again" << endl;
second_player();
}
}
break;
}
}
}
int main() {
system("Color 3");
first_player();
second_player();
system("pause");
return 0;
}
replace 功能目前不起作用。
【问题讨论】:
-
std::string::replace也许?很难确切地说出你想要什么。请发布您的非工作代码的MCVE 和明确的问题陈述(提示:示例输入和输出帮助)。 -
总有一段代码可以让帮助变得更容易。
-
你应该发布 MCVE
-
我不知道什么是MCVE。
-
@GùrìCôppérfîéld 是的,我做到了,而你之前没有发布 MCVE,在它不可编译之前(不完整),现在它不是最小的。查看您的代码很难说那里有什么问题,而说什么是对的更容易。
标签: c++