cin和CTRL + Z的C ++问题 [英] C++ Issue with cin and CTRL + Z

查看:100
本文介绍了cin和CTRL + Z的C ++问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在读5号c ++入门书,我在练习中遇到了一点问题:


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


从cin读取单词序列并将值存储为向量。在
之后,您已经阅读了所有单词,处理矢量并将每个单词更改为
大写。将转换后的元素(八个单词)打印到一行。

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


我在写随机单词^ Z

I am writing a random words ^Z

并按 Enter 不会发生任何事情。我按下 Enter 后必须重写^ Z,例如:

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


我正在写随机词

^ Z

I am writing a random words
^Z

您能解释一下为什么吗?谢谢!

Can you expain me why? Thanks!

PS:之所以这样说是因为在我以前的程序中,在同一行中编写^ Z可以正常工作。像下面的代码一样:

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;
}

我不知道为什么:(

推荐答案

必须首先使^ Z,Windows才能将其视为 Ctrl + Z ,否则将被视为无意义的字符。

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
}

编辑:在第二个示例中有效,因为当您读取整数时,c ++知道将给定的数字字符串除以空格(如果输入数字,则为 ENTER 在每行中分别)以分别读取每个数字,因此,如果您输入以下内容:

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

它将显示123,然后是2323,...然后是^ Z,因此它看起来像它在分开的行,但是当您读取字符串时,它不能做到这一点,因为字符串包含每个符号,因此它将输入的 ENTER 中的输入分开,并说明了第二个输入起作用的原因

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

这篇关于cin和CTRL + Z的C ++问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆