Problem inputting numbers into vector from file

This is a discussion on Problem inputting numbers into vector from file within the C++ Programming forums, part of the General Programming Boards category; Hello! so, I'm implementing quicksort and selection sort using vectors. I have the following code. To run I use redirection. ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    28

    Question Problem inputting numbers into vector from file

    Hello! so, I'm implementing quicksort and selection sort using vectors.
    I have the following code. To run I use redirection. i.e. ./program.out <datafile

    Code:
    int main()
    {
      int number;
      vector<int> list;
      cout << "QuickSort..." << endl;
      while (cin >> number)
        {
          list.push_back(number);
        }
      int left = 0;
      int size = list.size();
      int right = size-1;
      print(list); // testing sort
      quickSort(list, left, right);
      print(list); // testing sort
      cout << "Done" << endl;
    
      return 0;
    }
    But my vector seems to be empty?
    I've changed the while loop to
    Code:
    cin >> number;
    while (!cin.eof())
    {
      list.push_back(number);
      cin >> number;
    }
    That gives me a bad_alloc error though.

    Any tips? My sorts work with small sets, but I haven't been able to test larger 2000 and above sets yet because of this. :(

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    I'll reply to myself. I ALSO tried:

    Code:
     
    do {
        cin >> number;
        list.push_back(number);
      } while (number);
    and I STILL get CPU time limit exceeded :'(

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    6,825
    Would you please post your data file?
    Quote Originally Posted by phantomotap
    Can you write code while blindfolded only with the blind covering your brain? Can you code while brainfolded?

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    The data file contains a series of random numbers using a different program that writes to a data.txt file.
    I use srand(time(0)); cout << rand();

    So the file looks like this:

    1485738376
    1444384990
    1816004519
    1128556181
    1946001519
    671306856
    902458063
    1329651005
    371347021
    2063196661
    1678323328
    1176164953

    times a lot. Im trying to time quicksort by having it sort these files of 2000, 4000, 6000, etc elements.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    6,825
    I can't really reproduce your problem on my end. If the file name is somehow not named correctly (such as a missing extension) the redirection of cin will not be effective, though.
    Quote Originally Posted by phantomotap
    Can you write code while blindfolded only with the blind covering your brain? Can you code while brainfolded?

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Well what would be causing me to get a bad_alloc error? And/or CPU time limit exceeded?

    I add print statements after everything and the error seems to be only with adding ints to the vector with the while loop.
    If i print out the capacity and size, it says zero for both, even after the push_back stuff.

    I don't understand

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read numbers from file to std::vector
    By Petike in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2010, 03:33 AM
  2. strange error in inputting numbers from .txt file
    By angelica in forum C++ Programming
    Replies: 8
    Last Post: 04-10-2008, 11:28 PM
  3. Inputting strings from a file
    By Mystic_Skies in forum C Programming
    Replies: 7
    Last Post: 11-17-2004, 03:08 PM
  4. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM
  5. Inputting Strings from a file
    By Drealoth in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2003, 10:11 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21