0

thank you for reading. I am a new programmer in an introductory programming class, and I only have a month's worth of training in C++. I have tried to fix this code with many approaches, but I don't know why it only prints one word of my input string:

 #include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

void printThetxt(string);
string inputText(string);
    int main() {
        string x;
        printThetxt(inputText(x));
        system("pause");
            return 0;
}
void printThetxt(string y) {
    cout << y << endl;
}
string inputText(string x) {
cout << "Type in your string: " << endl;
    cin >> x;
    return x;
}

Please tell me why this code only prints one piece of the input string? Thank you!!

4

1 回答 1

3

代替:

cin >> x;

和:

std::getline(std::cin, x);

因为格式化输入在空白处停止。

于 2013-10-13T22:52:15.300 回答