简体   繁体   English

如何在 C++ 中打印多个输入和输出?

[英]How do I print multiple inputs and outputs in C++?

I'm trying to solve a problem on a competitive programming book where the output only appears after entering in the last input.我正在尝试解决竞争性编程书籍中的一个问题,该问题仅在输入最后一个输入后才会出现输出。 I seem to have gotten the logic down but I'm still confuse as to how to do the input/output portion.我似乎已经搞定了逻辑,但我仍然对如何进行输入/输出部分感到困惑。

Here is the code:这是代码:

#include <bits/stdc++.h>

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    std::vector<int>soundex;
    std::string word;

    for(int i = 0; i < word.length(); i++)
    {
        if (word[i] == 'B'|| word[i] == 'F' || word[i] == 'P' || word[i] == 'V')
        {
            soundex.push_back(1);
        }

        if (word[i] == 'C' || word[i] == 'G' || word[i] == 'J' || word[i] == 'K' || word[i] == 'Q' || word[i] == 'S' || word[i] == 'X' || word[i] == 'Z')
        {
            soundex.push_back(2);
        }

        if (word[i] == 'D' || word[i] == 'T')
        {
            soundex.push_back(3);
        }

        if (word[i] == 'L')
        {
            soundex.push_back(4);
        }

        if (word[i] == 'M' || word[i] == 'N')
        {
            soundex.push_back(5);
        }

        if (word[i] == 'R')
        {
            soundex.push_back(6);
        }
    }

    for (int j = 0; j < soundex.size(); j++)
    {
        if (soundex[j] == soundex[j+1])
        {
            soundex.erase(soundex.begin() + 1);
        }
        std::cout << soundex[j];
    }
    std::cout << "\n";

    return 0;
}

It behaves like this:它的行为是这样的:

Input:输入:
KHAWN
Output:输出:
25

Input:输入:
PFISTER
Output:输出:
1236

Input:输入:
BOBBY
Output:输出:
11

But I need it to behave like this, per the instructions of the problem:但是根据问题的说明,我需要它表现得像这样:

Input:输入:

KHAWN 
PFISTER  
BOBBY

Output:输出:

25  
1236  
11

Use while(cin >> word){ ... your code ... } to read until EOF (End Of File) in case every line only contains a word (no spaces allowed).使用while(cin >> word){ ... your code ... }读取直到 EOF(文件结束),以防每行只包含一个单词(不允许空格)。 You can keep the output as it is.您可以保持输出原样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM