3

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.

9
  • 2
    x[i] -- I'm looking, but I don't see where x is declared. Which probably means you hastily just typed in code instead of copying and pasting your actual program in the edit window. Commented Nov 22, 2017 at 4:17
  • You are reading the data in $x$, but where did you declear $x$ Commented Nov 22, 2017 at 4:18
  • 2
    Also, int grades[i]; -- what is that line supposed to do? Commented Nov 22, 2017 at 4:20
  • 1
    You are using C++ is there a reason you are not using std::vector? Commented Nov 22, 2017 at 4:21
  • 2
    @dev_P int grades[i]; -- Again, explain what that line does or what you expected it to do. It isn't even valid C++ syntax, let alone i is 0 when the line is executed. Commented Nov 22, 2017 at 4:34

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't know but for what reason most of the colleges or universities are making the student to use old c++ style when there are more better alternatives are available
Thank you PaulMcKenzie. Is there a way use to store data in an array without the use of vectors. Is there a way for you to fix my code? I'm still getting all zeroes if I compile the code that @HariomSingh edited. I tried this on both a Mac and Windows computer
fix your own code @dev_P
2

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

3 Comments

I don't see any issue -- See my answer. There is a big issue.
I mean to say there was no issue in reading the file .As the OP was "Read data from a file into an array - C++"
@dev_P Please do add more information if you are not able to get the desired output
1

Your original global array grades, of size 22, is replaced by the local array with the same name but of size 0.
(It is not overwritten, just any code using the variable grades, inside the scope that the second one was defined, will read the value of the second grades array, as it has a higher precedence.)

Both inFile >> grades[i]; and cout << grades[i] << " "; should return runtime errors as you are reading beyond their size (It appears that you are not using a strict compiler).
[int grades[i]; would return a compile time error normally as you shouldn't / usually can't initialize a fixed array with a variable]

I think what is happening, instead of your program crashing, is that grades[i] is just returning an anonymous instance of a variable with value 0, hence your output.

The simplest fix to your problem would be to just delete int grades[i].
(Also delete one of the int i = 0's as you don't need that to be defined twice)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.