-1

I have to use pointers in this program. I am trying to make 2 array's where one pulls data from a txt file and the other array copies the first array and fills in the rest of the space with 0's. I also need these to be happening in functions and not in main

So far my code is this: (I'd put less but I don't know where the problem is)

#include <iostream>
#include <fstream>

using namespace std;



void pullData(int* array, int fSIZE);

void doubledArray(int* array, int fSIZE, int aSIZE);

void arrayPrint(int* array, int fSIZE);

void inputCommand(int &userInput);



int main(){
    int userInput = 51;
    inputCommand(userInput);
    const int SIZE = userInput;
    const int SIZE2 = userInput * 2;


    int array2[SIZE2];
    int array[SIZE];

    pullData(array, SIZE);

    arrayPrint(array, SIZE);

    cout << endl << endl;

    doubledArray(array2, SIZE2, SIZE);

    arrayPrint(array2, SIZE2);

    return 0;
}



void pullData(int* array, int fSIZE){



    string inFileName = "data.txt";
    ifstream inFile;
    inFile.open(inFileName.c_str());

    if(inFile.is_open()){
        for(int counter = 0; counter < fSIZE; counter++){
            inFile >> array[counter];
        }
    }

}

void doubledArray(int* array, int fSIZE, int aSIZE){



    string inFileName = "data.txt";
    ifstream inFile;
    inFile.open(inFileName.c_str());

    if(inFile.is_open()){
        for(int counter = 0; counter < aSIZE; counter++){
            inFile >> array[counter];
        }
    }

    for(int counter = 0; counter < fSIZE; counter++){
        array[counter] = 0;
    }
}

void arrayPrint(int* array, int fSIZE){
    for(int counter = 0; counter < fSIZE; counter++){
        cout << array[counter] << "\n";
    }
}

void inputCommand(int &userInput){
    while(userInput < 0 || userInput > 50){
        cout << "Enter a number between 0-50: \n";
        cin >> userInput;
        if(userInput < 0 || userInput > 50){
            cout << "Invalid input, please try again.\n\n";
        }
    }
}

I want to be getting:

0
1
2
3
4


0
1
2
3
4
0
0
0
0
0

but I'm currently getting:

0
1
2
3
4


0
0
0
0
0
0
0
0
0
0
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Everything you want to do becomes *very* easy with standard C++ containers like `std::array` or `std::vector`. – Some programmer dude Mar 26 '23 at 15:46
  • Also, your code isn't valid C++ anyway, since you use variable-length arrays and those are [not part of standard C++](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Making your sizes `const` isn't enough, that merely make them read.only. You need proper compile-time constant. – Some programmer dude Mar 26 '23 at 15:47
  • 1
    Leaving aside usage of variable length arrays ... your doubledArray() clearly fills the array with zeroes. It sounds that it is not what you wanted your code to do. Use your debugger to check what your code does if you do not see it by reading. – Öö Tiib Mar 26 '23 at 15:48
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173) This seems to be a continuation of [this question](https://stackoverflow.com/questions/75844985/i-need-help-turning-this-into-pointers-and-fixing-array2#comment133783595_75844985). You fixed one bug and added a new bug (you are now writing `0` to every element of `array2`). – Drew Dormann Mar 26 '23 at 15:57

1 Answers1

0

If I didn't get your approach wrong, you want to initialize the second array with zeros and overwrite the first array's values at the same indexes in second array.

But your code seems to do the initialization after reading the first array's values, so you lost them by filling the array with zeros at the end! Here I just rearranged your code blocks, this might work.

    void doubledArray(int* array, int fSIZE, int aSIZE){
    
        string inFileName = "data.txt";
        ifstream inFile;
        inFile.open(inFileName.c_str());

        for(int counter = 0; counter < fSIZE; counter++){
            array[counter] = 0;
        }
    
        if(inFile.is_open()){
            for(int counter = 0; counter < aSIZE; counter++){
                inFile >> array[counter];
            }
        }
    }

Since you wrote that you have to use pointers I would do it this way:

#include <iostream>
using namespace std;
void *doubledArray(int *arr1, int *arr2, int size1)
{
    int *p = arr2;
    int i = 0;
    if(arr1 != NULL && p != NULL)
    {
        while(i < size1)
        {
            *p = *(arr1+i);
            i++;
            p++;
        }
    }
}

int main() {

    int arr1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int arr2[20] = {0}; //this statement initializes the array by filling it with 0s
    doubledArray(arr1, arr2, 10);
    for(int i = 0; i < 20; i++)
    {
        cout << arr2[i] << endl;
    }
    return 0;
}