Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why doesn't this line print after I replace this?

The original code prints the msg in the brackets, but the second code prints just Hey there. Why is this? I started C++ a few days ago. Have experience in Python and some C#.
Original code:-

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
   vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ 
extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

This is the new code:-

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ 
extension!"};

    cout << "Hey there" << endl;

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In the first code, you are populating a vector with string elements, and then using a range-for loop to iterate through that vector printing out its elements.

In the second code, you are still populating the vector, but you are not iterating through the vector at all, you are just printing out a separate message instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading