-1

I am trying to write a small program in C++ to test the performance of insertion sort and other sorting algorithms. I would like to store a very large numbers in a txt file and let my program read it first and store each number into a vector. So the sorting algorithms could easily handle such vector.

But I came across an issue, that I don't know how to call the vector(num1) in my insertion sort part. As the vector was initialized in a while loop prior to the sorting.The compiler cannot identify it so my program can not proceed. So I would appreciate anyone to give me some advice to solve this issue or maybe give your thoughts about my codes. Thanks very much!

#include <iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;

int main() {
    //To read file: 
ifstream num("test.txt");
char num_arry[1000000];
if (!num)
{
    cout << "File load error,please check if file exist" << endl;
}
while (!num.eof())
{
    num >> num_arry;
    int number = stoi(num_arry); // convert char to int     
    vector<int> num1;  //set a new vector to store file data numbers
    num1.push_back(number); // push int in the vector

}

// Insertion sort start:
for (int i = 1; i < num1.size(); i++) {
    int element = num1[i];
    int j = i;
    while (num1[j - 1] > element) {
        num1[j] = num1[j - 1];
        j = j - 1;
        num1[j] = element;
    }
}

for (int i = 0; i < num1.size(); i++) {
    cout << num1[i] << " ";
}

return 0;
}
leon365
  • 127
  • 1
  • 7
  • 1
    Since `num1` is defined local to the `while` loop, it will be re-initialized and decayed per loop round – duong_dajgja Jul 10 '17 at 01:22
  • off topic: watch out for `while (!num.eof())`. It doesn't work quite the way you'd like. More here: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user4581301 Jul 10 '17 at 02:09
  • Actually, I am debugging this issue, thanks for letting me know this! – leon365 Jul 10 '17 at 02:23

1 Answers1

3

Simply move the vector<int> num1 to before the while loop. That way, it exists beyond that loop, specifically into the region of code below where you want to use it.

What you have wouldn't work in any case even if the scope survived the end of the loop, since the vector is created anew in every single iteration of said loop - it would end up being a vector with just the last element in it.

In other words this (simplified form):

while (!num.eof()) {
    vector<int> num1;
    num1.push_back(something);
}
// Cannot see num1 here.

would become:

vector<int> num1;
while (!num.eof()) {
    num1.push_back(something);
}
// num1 is usable here.

You may also want to reconsider loading your number into a character array then calling stoi on it (unless you have a specific reason for doing so). The C++ stream stuff is perfectly capable of reading directly into a non-character data type, such as with:

vector<int> numArray;
{
    int number;
    while (numInputStream >> number)
        numArry.push_back(number);
}
paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
  • Thanks for your help! Moving ahead the initialization works!! I also tried your advice, however, if I don't use char array the stream will not work properly. – leon365 Jul 10 '17 at 01:53