Thread: Strange problem during runtime (Program has stopped working...)

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    19

    Strange problem during runtime (Program has stopped working...)

    Hey guys, I just wrote a simple program that takes 10 integers into a vector, and the goal of the program is to output the ten numbers, all multiplied by two, using iterators. Well, I wrote the code and it seems fine, and also compiles fine, but when I enter in the first ten numbers into the vector, it says "Project1.exe has stopped working..." I'm currently using Vista Home Basic. Can anyone tell me what's going on? Thanks for anything in advance.

    Here's the code:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main(){
        
        vector<int> str(10);
        int num, counter;
        
        while (cin >> num){
                 str.push_back(num);
        }
        
        for (vector<int>::iterator iter = str.begin(); iter != str.end(); ++iter){
            *iter = str[counter] * 2;
            ++counter;
            cout << *iter << endl;
    }
            
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    1. You create a vector of 10 ints, then add a couple more to the back of it. You probably want to create an empty vector instead.

    2. counter is not initialised and thus holds garbage.

    3. Since you want to use iterators, use them instead of an index, e.g., *iter += *iter.
    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

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Thanks. After doing what you suggested, the program now works, but what do you mean by "add a couple more to the back of it"? Cause, if I only enter 10 things for my while statement, it would fit in the vector, wouldn't it?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    After doing what you suggested, the program now works, but what do you mean by "add a couple more to the back of it"? Cause, if I only enter 10 things for my while statement, it would fit in the vector, wouldn't it?
    Actually, you could enter a thousands ints and it will (most probably) still fit into the vector since you are using push_back() to add elements to the vector. However:
    Code:
    vector<int> str(10);
    creates a vector of 10 ints, all zero initialised. You probably do not want these extra elements.
    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

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Oh I see, alright, thanks for helping.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  3. Launching an application within program runtime
    By dav_mt in forum C++ Programming
    Replies: 6
    Last Post: 04-15-2008, 05:46 PM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  5. help with strange problem
    By creon in forum C Programming
    Replies: 3
    Last Post: 09-13-2006, 05:44 AM