【问题标题】:How to add any symbol instead another symbol in string C++?如何在字符串 C++ 中添加任何符号而不是另一个符号?
【发布时间】: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++


【解决方案1】:

您不能替换字符串中的字符。你应该做的是使用 std::string 作为 std::vector (因为它真的很相似)。

您可以将字母分配到单词中的某个位置:

word[2] = yourLetter;

你甚至不需要知道那个地方以前有什么,所以你不需要替换它 - 分配更容易。

【讨论】:

  • 非常感谢。我想要更多像你这样的人。
猜你喜欢
  • 1970-01-01
  • 2020-04-15
  • 2017-10-04
  • 1970-01-01
  • 2013-08-07
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
  • 2013-05-13
相关资源
最近更新 更多