1

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
  • 2
    You have shown a program that doesn't read ANY input. In any case, simply read the words one at a time in a loop. If the input is stated to be on one line, use `std::getline` and then read the words from a `std::istringstream`. Oh, and I recommend you drop the habit of including `` immediately. – paddy Oct 07 '20 at 05:03
  • 1
    Careful with `#include `. That sucker pulls in pretty much the entire C++ standard library and that's a lot of overhead when all you needed was three headers. At runtime you probably won't see a thing, but while debugging, that will slow down your build times by close to an order of magnitude, eating up whatever time you saved not typing in the few headers you needed after a compile or two. – user4581301 Oct 07 '20 at 05:06
  • 1
    Focus on input or output. Succeed at getting a single input (or output) before attempting multiple. – JaMiT Oct 07 '20 at 05:07
  • Side note: learning to program in C++ through competitive programming is a tough road. For the early fundamentals, like reading information from streams, you may be better off with [a conventional text](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user4581301 Oct 07 '20 at 05:19
  • On a side note: `if (soundex[j] == soundex[j+1])` goes out of bounds on the last loop iteration. And the loop will skip an element every time `erase()` is called. – Remy Lebeau Oct 07 '20 at 05:34

1 Answers1

0

Use while(cin >> word){ ... your code ... } to read until EOF (End Of File) in case every line only contains a word (no spaces allowed). You can keep the output as it is.

Eloy Pérez Torres
  • 1,050
  • 4
  • 14
  • Note that in the real world of console input there often is no end of file and the user will sit around for a few minutes of the program doing nothing before killing the program and either reporting the bug or finding another program. – user4581301 Oct 07 '20 at 05:10
  • @user4581301 Note also that he is preparing for a competitive programming event. He is reading a "Competitive Programming Book". That is a usual input extraction in Online Judges. – Eloy Pérez Torres Oct 07 '20 at 05:11
  • Agreed, but the asker isn't the only person who'll read this question seeking answers. – user4581301 Oct 07 '20 at 05:17
  • @user4581301 Also true. Nevertheless the asker is giving some context and that should (must) be used in order to give a helpful and meaningful answer according to his/her question. – Eloy Pérez Torres Oct 07 '20 at 05:20
  • Thank you so much! I've gotten it to work properly on the sample test cases but when I submitted it, it got a verdict of runtime error :/ Have to debug on that, thanks again! –  Oct 07 '20 at 05:34