PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 17, 2022

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

 August 17, 2022     c++, input, output     No comments   

Issue

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

Solution

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.



Answered By - Eloy Pérez Torres
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing