Home > Net >  C vector prints out weird elements
C vector prints out weird elements

Time:11-20

I'm currently in the process of learning C and for that I'm reading the book "C Primer". The book is pretty good so far and I have learned a lot however I experienced weird behaviour using a vector and I'm unsure if this is right or if it's a problem from my side.

The task is:

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."

This is my code:

#include <iostream>
#include <vector>

using namespace::std;

int main()
{
    string input;
    vector<string> svec;

    while (cin >> input)
    {
        svec.push_back(input);

        for (auto& rows : svec)
        {
            for (auto& element : rows)
            {
                element = toupper(element);
            }
        }

        int maxWordsPerLine = 0;

        for (auto word : svec)
        {
            if (maxWordsPerLine >= 8)
            {
                cout << endl;
                cout << word;
                maxWordsPerLine = 1;
            }
            else
            {
                cout << word;
                maxWordsPerLine  ;
            }
        }
    }
}

I believe it does the things described in the task but when I type in:

Hello thanks for helping I dont know whats wrong with this problem lol

The output is:

HELLOHELLOTHANKSHELLOTHANKSFORHELLOTHANKSFORHELPINGHELLOTHANKSFORHELPINGIHELLOTHANKSFORHELPINGIDONTHELLOTHANKSFORHELPINGIDONTKNOWHELLOTHANKSFORHELPINGIDONTKNOWWHATSHELLOTHANKSFORHELPINGIDONTKNOWWHATS
WRONGHELLOTHANKSFORHELPINGIDONTKNOWWHATS
WRONGWITHHELLOTHANKSFORHELPINGIDONTKNOWWHATS
WRONGWITHTHISHELLOTHANKSFORHELPINGIDONTKNOWWHATS
WRONGWITHTHISPROBLEMHELLOTHANKSFORHELPINGIDONTKNOWWHATS
WRONGWITHTHISPROBLEMLOL

I hope someone can explain me why this happens and how I can avoid it in the future.

CodePudding user response:

You need to realize there's two steps.

First step: read all the words and convert each to uppercase

Second steps: print all the words

Second step needs to be done after first step is done. However, you have a single while loop. Didn't run it, but simplest change that looks likely to work is:

string input;

vector<string> svec;

while (cin >> input)
{
    svec.push_back(input);

    for (auto& rows : svec)
    {
        for (auto& element : rows)
        {
            element = toupper(element);
        }
    }
} // extra closing bracket for the while

    int maxWordsPerLine = 0;

    for (auto word : svec)
    {
        if (maxWordsPerLine >= 8)
        {
            cout << endl;

            cout << word << " "; // extra space to separate words

            maxWordsPerLine = 1;
        }
        else
        {
            cout << word;

            maxWordsPerLine  ;
        }
    }

CodePudding user response:

For starters you need to include the header <string>

#include <string>

In this while loop

while (cin >> input)
{
    svec.push_back(input);

    for (auto& rows : svec)
    {
        for (auto& element : rows)
        {
            element = toupper(element);
        }
    }
    //...

you are converting all entered words at given iteration to upper case again and again.

And outputting the vector must be removed from the while loop.

So the while loop can look the following way

while ( cin >> input )
{
    svec.push_back(input);

    for ( auto& element : svec.back() )
    {
        element = toupper(element);
    }
}

After that you may output the vector.

And in this range based for loop

for (auto word : svec)

you should not create a copy of a string stored in the vector. You should declare the variable word as having constant referenced type as

for ( const auto &word : svec)

Also the inner if statement has duplicated code that is not a good style of programming.

        if (maxWordsPerLine >= 8)
        {
            cout << endl;

            cout << word;

            maxWordsPerLine = 1;
        }
        else
        {
            cout << word;

            maxWordsPerLine  ;
        }

Rewrite the if statement within the range based for loop for example the following way

        cout << word << ' ';

        if (   maxWordsPerLine == 8)
        {
            cout << endl;
            maxWordsPerLine = 0;
        }
  • Related