Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

windows - C++ Issue with cin and CTRL + Z


I'm reading c++ primer 5th and I have a little problem with an exercise:

Read a sequence of words from cin and store the values a vector. After you’ve read all the words, process the vector and change each word to uppercase. Print the transformed elements, eight words to a line.

My code is this:

#include <iostream>
#include <vector>
#include <string>
#include <cctype>

using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;

int main(){

    vector<string> words;
    string wordBuffer;
    vector<string> output(1);

    while (cin >> wordBuffer){
        words.push_back(wordBuffer);
    }

    for (string &word : words){
        for (char &letter : word){
            letter = toupper(letter);
        }
    }

    unsigned currentLine = 0;
    for (decltype(words.size())index = 0; index < words.size(); ++index){

        output[currentLine] += words[index] + " ";

        if ((index+1) % 8 == 0){
            ++currentLine;
            output.push_back("");
        }

    }

    for (string s : output){
        s[s.size() - 1] = 0; //removing the whitespace
        cout << s << endl;
    }

    system("pause");
    return 0;
}

Now, everything works well, but i have an issue with the input of the words by console.
If I write

I am writing a random words ^Z

and press Enter nothing happens. I have to rewrite the ^Z after I have pressed the Enter, like here:

I am writing a random words
^Z

Can you expain me why? Thanks!

PS: I'm saying that because in my previous programs writing ^Z in the same line worked fine. Like in this code:

#include <iostream>;


int main(){
    int currval = 0,val = 0;

        int count = 1;
        while (std::cin >> val){
            if (currval == val){
                ++count;
            }
            else {
                std::cout << "The number " << currval << " appears " << count << " times" << std::endl;
                currval = val;
                count = 1;
            }
        }
        std::cout << "The number " << currval << " appears " << count << " times" << std::endl;

    system("pause");

    return 0;
}

I can't figure out why :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The ^Z has to be first in order for Windows to treat it as Ctrl+Z, otherwise it is just treated as meaningless characters.

If you would like it to work like you wrote i'd suggest:

String wordBuffer("")
while (strcmp(wordBuffer[strlen(wordBuffer)-3], "^Z") != 0){
    words.push_back(wordBuffer);
    cin >> wordBuffer
}

EDIT: in your second example it works because when you read integers c++ knows to divide the given string of numbers in the space (or ENTER if the numbers are entered separately in every line) to read every number separately so if you'll enter:

123 2323 4545 43 ^Z

It will read 123, then 2323, ... and then ^Z and so it will be as though it got it in a separate line but when you read string, it cant do that because a string contain every symbol and so it separate the input in the ENTER pressed and that why the second one works


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...