【发布时间】:2022-01-18 01:58:49
【问题描述】:
我正在尝试制作一个程序,在用户确定的点将一个短语插入另一个短语。但是,当我尝试为每个参数、两个短语以及需要插入另一个短语的位置获取输入时,我只能为所询问的第一个参数提供输入,然后剩下的代码是在没有输入其他两个参数的情况下执行,我不确定为什么会在我的代码中发生这种情况。我的代码附在下面。
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
string mystr; // original statement
string substrToBeInserted; // statement to be put into the original statement
int positionToInsertAfter; //interger value in the string array for where the other statement needs to be put
cout << endl << "Please enter your statement" << endl;
cin >> mystr;
cout << endl << "Please enter your statement to be inserted" << endl;
cin >> substrToBeInserted;
cout << endl << "Please enter where your statement is going to be inserted" << endl;
cin >> positionToInsertAfter;
mystr = mystr + mystr[positionToInsertAfter] + substrToBeInserted;
cout << mystr;
return 0;
}
非常感谢您的帮助! :)
【问题讨论】:
-
输入的短语中是否有空格?
std::cin >> mystr;会将输入复制到第一个空格。所以输入singleword会如你所愿,但输入single word会将single放入mystr并将word放入substrToBeInserted。