Need A little bir of help with arrays

Hey guys this is my first post here so sorry for any screw-ups I might make.

In an assignment I have I've am asked to read from a file named "values.txt" which in it contains 20 different grades and store them in an array. now I understand how to read from a file, but saving in into an array is a mystery to me. Another problem I think I may bee having is printing the contents of the array to the screen. Thanks in advanced. Any help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include<iostream>
#include<fstream>
using namespace std;

int main()
	{
		int grades[19];

		ifstream file("values.txt");
		if(file.is_open())
		{
			int grades[19];

			for(int i = 0;i < 19;i++)
			{
				file>>grades[i];
			}
		}
		
	}
Well if you have 20 grades, you need an array of size 20. You've declared an array of size 19. You also declared your grades array twice, which won't work. You will want to remove int grades[19]; from inside your if statement.

The code you have should work fine if the only separation between grades are whitespace/newline characters (\t, \n, space).

To print out the grades you simply have another loop in which you have: cout << grades[i];
Topic archived. No new replies allowed.