Thread: Stuck in a vector size loop

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Stuck in a vector size loop

    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
    }
    Double Helix STL

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    push_back grows the vector. You leave the first 5 items empty and keep adding to the end.

    Simply loop 5 times and don't size the vector to begin with.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    At this line, you create a vector of 5 strings:
    Code:
    std::vector < std::string > myVec( SIZE, "" );
    Here, you keep looping as long as the counter is less than the size of the vector:
    Code:
    for ( unsigned int counter = 0; counter < myVec.size(); counter++ ) {
    Now, you increase the size of the vector by 1:
    Code:
    myVec.push_back( name );
    Therefore, when you next compare the counter with the size of the vector, you compare 1 with 6. On the next iteration, you will compare 2 with 7. In general, the comparison is always x < x + 5, which is always true, hence the loop is an infinite loop.

    One solution is to simply not use push_back, e.g.,
    Code:
    myVec[counter] = name;
    Another solution is to use push_back, but create an empty vector instead, changing the loop condition to counter < SIZE.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thank you so much for the advice guys

    Very imformative - I will take your advice on board.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. Help! Stuck in a loop!
    By raell in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2003, 10:47 AM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM