C++ 如何将string::find与integer一起使用?C++;
好吧,我现在正在写一个小控制台程序,遇到了一个小问题:我正在构建一个程序,其中一个用户可以想到一个单词并将其翻译成下划线(单词=________;),另一个用户必须猜测字母(用户猜测W;程序先擦除W,然后插入W“W__________所以现在我的代码看起来像这样:C++ 如何将string::find与integer一起使用?C++;,c++,string,insert,C++,String,Insert,好吧,我现在正在写一个小控制台程序,遇到了一个小问题:我正在构建一个程序,其中一个用户可以想到一个单词并将其翻译成下划线(单词=________;),另一个用户必须猜测字母(用户猜测W;程序先擦除W,然后插入W“W__________所以现在我的代码看起来像这样: #include "stdafx.h" #include <iostream> #include <string> using namespace std; int main() { string
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string wort;
cout << "Bitte gebe ein Wort ein: ";
cin >> wort;
string gesucht = "";
if (wort.length() == 0 || wort.length() > 63) {
cout << "Bitte gebe ein gueltiges Wort ein.\n";
}
else {
for (unsigned int a = 1; a <= wort.length(); a++) {
gesucht.insert(0, "_");
}
}
cout << "Folgendes Wort wird gesucht: " << gesucht << endl;
int versuche = 11;
char eingabe;
cin >> eingabe;
if (wort.find(eingabe) == string::npos) {
versuche--;
cout << "Folgendes Wort wird gesucht: " << gesucht << ", du hast noch " << versuche << " Fehlversuche.\n";
}
else {
gesucht.erase(wort.find(eingabe));
gesucht.insert(wort.find(eingabe), eingabe);
cout << gesucht << endl;
}
return 0;
}
else {
size_t pos = wort.find(eingabe);
gesucht.erase(pos, 1);
gesucht.insert(pos, 1, eingabe);
cout << gesucht << endl;
}
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
int main()
{
线状麦汁;
库特>麦汁;
字符串gesucht=“”;
如果(麦汁长度()==0 | |麦汁长度()>63){
cout导致问题的部分应如下所示:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string wort;
cout << "Bitte gebe ein Wort ein: ";
cin >> wort;
string gesucht = "";
if (wort.length() == 0 || wort.length() > 63) {
cout << "Bitte gebe ein gueltiges Wort ein.\n";
}
else {
for (unsigned int a = 1; a <= wort.length(); a++) {
gesucht.insert(0, "_");
}
}
cout << "Folgendes Wort wird gesucht: " << gesucht << endl;
int versuche = 11;
char eingabe;
cin >> eingabe;
if (wort.find(eingabe) == string::npos) {
versuche--;
cout << "Folgendes Wort wird gesucht: " << gesucht << ", du hast noch " << versuche << " Fehlversuche.\n";
}
else {
gesucht.erase(wort.find(eingabe));
gesucht.insert(wort.find(eingabe), eingabe);
cout << gesucht << endl;
}
return 0;
}
else {
size_t pos = wort.find(eingabe);
gesucht.erase(pos, 1);
gesucht.insert(pos, 1, eingabe);
cout << gesucht << endl;
}
else{
尺寸位置=找到的麦汁(艾因加贝);
gesucht.擦除(位置1);
gesucht.插页(位置1,eingabe);
好了,伙计们,我解决了我的问题,我只添加了一个1,这样它就知道应该添加多少个字符,因为eingabe是一个字符。
这是工作代码的外观:
else {
gesucht.erase(wort.find(eingabe), 1);
gesucht.insert(wort.find(eingabe), 1, eingabe);
cout << gesucht << endl;
}
else{
gesucht.擦除(麦汁查找(eingabe),1);
gesucht.插入物(麦汁查找(eingabe),1,eingabe);
无法使用编辑字符串(eingable)
。很抱歉,如果我的手机上没有代码格式,我不会像你这样删除。请将真实单词保留在字符串中,并保留一个包含所有下划线的字符串。猜测字母时,请索引机密字符串,如果字母匹配,请将该位置的下划线更改为字母。当这两个字符串匹配时,单词已被猜测d、 find
已返回一个整数。您是否尝试过gesuch.erase(wort.find(eingabe),1);
谢谢,帮我解决了这个问题!