Hey guys.
Is there any reason why I have created an infinite loop here if input?
I have stated I only wish 5 names to be entered and I have set the loop
condition to the size of the vector using the function.
And I push_back() the name each iteration to add it to the vector, but
when I run the program it will not stop asking for input even when it goes
past the 4th input ( which is the fifth ).
I am sure it is such a tiny mistake - if anyone can point me in the right
direction I would greatly appriciate it.
Code:
#include <iostream>
#include <string>
#include <vector>
// main function - begin program execution
int main ( void ) {
const int SIZE = 5;
std::string name = "";
std::vector < std::string > myVec( SIZE, "" );
for ( unsigned int counter = 0; counter < myVec.size(); counter++ ) {
std::cout << "Enter name " << counter << ": ";
std::getline( std::cin, name );
myVec.push_back( name );
}
std::cout << "\n\nYou entered these five names:\n\n";
for ( unsigned int counter = 0; counter < myVec.size(); counter++ ) {
std::cout << myVec[ counter ] << std::endl;
}
std::cin.get(); // freeze console window
std::cin.ignore();
return 0; // indicate sucessful termination
}