0

posting again due to change in formatting and code

I'm trying to parse a csv file and search it to find values that match my randomly generated array.

I've tried a few methods but im getting lost, I need to access the entire file, search for these values and output the index column that it belongs to.

my code for my random array generator as well as my search csv function:

reproducible example:

#include <fstream>
#include <iostream>
#include <string>
#include <random>

using namespace std;

//fills array of size 6 with random numbers between 1-50
void random_array(int arr[])
{

    for (int i = 0; i < 6; i++) {
        int num = rand() % 51;
        arr[i] = num;
    }
}

int main()
{
    string line;
    fstream file;

    int size = 6;
    int numbers[size];
    int found;

    //gets random array from random_array func
    srand(time(NULL));
    random_array(numbers);

    //opens file to search
    file.open("CSV directory here");
    if (file.is_open()) {
        //print random array values
        for (int i = 0; i < size; i++) {
            cout << numbers[i] << " ";
        }
        cout << endl;

        //get file contents and search if contents of array exist in file
        while (getline(file, line)) {

            for (int i = 0; i < size; i++) {
                found = line.find(std::to_string(numbers[i]));

                if (found != string::npos) {
                    //print found numbers
                    cout << found << " ";
                }
            }
        }
    }

    return 0;
}

CSV sample format:

,Date,1,2,3,4,5,6,7
0,Wed 03 January 2001,5,6,16,17,19,22,40
1,Sat 06 January 2001,6,16,23,34,35,40,37

my output:

array generated:

35 35 38 37 16 31

numbers found (from entire csv not just sample above):

26 33 33 39 24 31 34 33 24 6 28 31 33 33 30 0 32 34 33 30 27 38 26 29
29 33 7 24 29 26 26 26 24 24 0 30 30 36 0 0 23 27 0 0 7 36 36 27 30 30
27 27 26 26 32 29 23 38 32 32 28 28 7 25 29 37 25 29 29 26 23 34 31 28
25 31 28 28 34 32 32 35 38 40 25 37 37 35 40 40 30 30 42 42 42 36 35
28 31 25 37 7 27 36 33 36 33 29 39 29 35 34 34 40 40 43 31

Obviously you can see these numbers aren't correct and are almost completely random. The numbers dont match what's within my array, am I doing something wrong with my file parsing?

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
sdh1998
  • 41
  • 1
  • 4
  • 1
    Technically your program is invalid, as [C++ doesn't have variable-length arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). – Some programmer dude Apr 12 '22 at 13:28
  • 2
    As for your problem, this seems like the perfect time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. With a *debugger*, step through the code statement by statement while monitoring variables and their values to see what happens, and figure out when and where things starts to go wrong. And for debugging and testing, use hard-coded values that you know should exhibit a certain behavior. – Some programmer dude Apr 12 '22 at 13:31
  • 3
    Pop quiz: if `numbers[i]` has the value 3, what do you think you'll get from `line.find(std::to_string(numbers[i]));` if line contains just "7834"? `find()`, in this case, does not do what you think it does. – Sam Varshavchik Apr 12 '22 at 13:36
  • Side note: About [`using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Apr 12 '22 at 13:46
  • About good practice: If you pass arrays by pointer you usually have another parameter indicating the array size – interesting alternative: `template void ra(int(&array)[N])`, which would accept array only and deduce its size from (but you cannot pass raw pointers to). Even better would be using a `std::vector` or, if size is fix at compile time, a `std::array`, both accepted by reference. – Aconcagua Apr 12 '22 at 13:49
  • 1
    Follow-up to @Someprogrammerdude : `int size = 6; int numbers[size];` is illegal C++ as VLA are not part of the C++ standard; solely *some* compilers accept it as an extension. `size` needs to be a compile-time constant, so you need to declare it `const` or `constexpr`. Actually correct type for specifying array sizes is `size_t`, not `int`, though it doesn't matter much in *this* case... – Aconcagua Apr 12 '22 at 13:52
  • 1
    And as a follow up to @Aconcagua, if you make it a habit to use `std::array` (as in `std::array numbers;`) then all compilers will require a compile-time constant. It also comes with the added benefit of being a standard container which makes it easier to pass around (by reference is my recommendation) and return, as well as having all the standard member functions available for a standard container. – Some programmer dude Apr 12 '22 at 16:41

0 Answers0