Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read data from a file into an array - C++

Tags:

c++

file-io

I want to read the data from my input file

70 95 62 88 90 85 75 79 50 80 82 88 81 93 75 78 62 55 89 94 73 82

and store each value in an array. There's more to this particular problem (the other functions are commented out for now) but this is what's really giving me trouble. I looked for hours at the previous question about data and arrays but I can't find where I'm making the mistake.

Here's my code:

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

using namespace std;

const int SIZE = 22;
int grades[SIZE];

void readData() {

    int i = 0;
    int grades[i];

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

    if (inFile.is_open())  
    {
        for (i = 0; i < SIZE; i++) 
        {
            inFile >> grades[i];
            cout << grades[i] << " ";
        }

        inFile.close(); // CLose input file
    }
    else { //Error message
        cerr << "Can't find input file " << inFileName << endl;
    }
}
/*
    double getAverage() {

        return 0;
    }

    void printGradesTable() {

    }

    void printGradesInRow() {

    }


    void min () {
        int pos = 0;
        int minimum = grades[pos];

        cout << "Minimum " << minimum << " at position " << pos << endl;
    }

    void max () {
        int pos = 0;
        int maximum = grades[pos];

        cout << "Maximum " << maximum << " at position " << pos << endl;
    }

    void sort() {

    }
*/


int main ()
{
    readData();
    return 0;
}

and here's my output:

 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

Thank you for your time.

like image 825
dev_P Avatar asked Jul 16 '26 06:07

dev_P


2 Answers

The issue is that you're declaring a local grades array with a size of 1, hiding the global grades array. Not only that, you are now accessing the array beyond the bounds, since the local grades array can only hold 1 item.

So get rid of the line:

int grades[i];

However, it needs to be mentioned that this:

int i = 0;
int grades[i];

is not valid C++ syntax. You just stumbled into this by mistake, but that code would not compile if compiled using a strict ANSI C++ compiler.

An array in C++ must be declared using a constant expression to denote the number of entries in the array, not a variable. You, by accident, are using a non-standard compiler extension called Variable Length Arrays or VLA's for short.

If this is for a school assignment, do not declare arrays this way (even if you meant to do it), as it is not officially C++. If you want to declare a dynamic array, that is what std::vector is for.

like image 80
PaulMcKenzie Avatar answered Jul 17 '26 19:07

PaulMcKenzie


I don't see any issue in reading the file , you have just confused the global vs local variable of grades

You don't need this

int i = 0;
int grades[];

Inside the function readData

#include <string>

using namespace std;

const int SIZE = 22;
int grades[SIZE];

void readData() {


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

    if (inFile.is_open())
    {
        for (int i = 0; i < SIZE; i++)
        {
            inFile >> grades[i];
            cout << grades[i] << " ";
        }

        inFile.close(); // CLose input file
    }
    else { //Error message
        cerr << "Can't find input file " << inFileName << endl;
    }
}

int main()
{
    readData();
    return 0;
}

Output

like image 20
Hariom Singh Avatar answered Jul 17 '26 18:07

Hariom Singh