Array and input files.

I never quite figured out how to use input a file so the program uses it (or really anything files) so I don't know what I am missing or what im doing wrong. The program is supposed to read numbers from a file and then place them in the array. Then the numbers are supposed to print in reverse.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream> 
#include <fstream> 
#include <iomanip>
using namespace std;

const int MAX = 10; //array size
int main() {
	int numbers[MAX]; //array numbers with 10 limit
	ifstream inData; //input file stram object
	int count = 0;
	int value; 
	int index;
	inData.open("reverse.txt"); 
	for (index = 0; index < MAX; index++) {
		//store in array
		while (count < MAX && inData >> numbers[count]) {
			count++;
		}
		//close file
		inData.close();
	}
	for (index = MAX - 1; index >= 0; index--) {
		cout << numbers[count] << " ";
	}
		inData.close();
		system("PAUSE");
return 0; 
	}


The numbers are supposed to be 12 34 76 82 10 55 76 88 33 65

Also, just making sure, when one is using Microsoft Visual Studios to do the whole file thing, the file you are using is supposed to be opened just like opening a new item under Source Files, only that it is existing item right?
Last edited on
Hello BGA6444, I tweaked your code a little so that it's simpler and it works:

reverse.txt file:
12
34
76
82
10
55
76
88
33
65


Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream> 
#include <fstream> 
#include <iomanip>
using namespace std;

int main() 
{
        const int MAX = 10; //array size

	int numbers[MAX]; //array numbers with 10 limit
	ifstream inData; //input file stram object
	int count = 0;
	
	inData.open("reverse.txt"); 
	
	
	cout << "\nNumbers in array are:\n";
	
	while (inData >> numbers[count])
	{
		cout << numbers[count] << ", ";
		count ++;
	}
	
	cout << "\n\nThe reversed numbers are:\n";
	
	for (count = MAX - 1; count >= 0; count--)
	{
		cout << numbers[count] << ", ";
	}
	
	cout << "\n\n";
		
	inData.close();
		
	system("PAUSE");
		
	return 0; 
}


Program output:

Numbers in array are:
12, 34, 76, 82, 10, 55, 76, 88, 33, 65,

The reversed numbers are:
65, 33, 88, 76, 55, 10, 82, 76, 34, 12,

Press any key to continue...


Note that line 14 on your original code, your for loop serves no purpose, the only thing that's doing something is your while loop.
Also in that same loop, if you close the file at the first iteration of the loop, you won't be able to read from it for the next iterations.

Hope this helps,

Regards,

Hugo.


EDIT:
Also, just making sure, when one is using Microsoft Visual Studios to do the whole file thing, the file you are using is supposed to be opened just like opening a new item under Source Files, only that it is existing item right?

Not necessarily, it only needs to be located in the same folder as your project file. Or you can use any file in your computer so long as you use its path when you open it. E.g. inData.open("C:\Users\Username\Desktop\yourFile.txt")
Last edited on
Both of your programs require exactly MAX numbers in the file. Otherwise they print uninitialized data at the end of the array. This works with up to MAX numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>

using namespace std;

int
main()
{
    const int MAX = 10;		//array size

    int numbers[MAX];		//array numbers with 10 limit
    ifstream inData("reverse.txt");	//input file stram object
    int count = 0;

    cout << "\nNumbers in array are:\n";

    while (inData >> numbers[count]) {
	cout << numbers[count] << ", ";
	count++;
    }
    // close the file as soon as you're done with it to
    // all the input code together
    inData.close();

    cout << "\n\nThe reversed numbers are:\n";

    while (count--) {
	cout << numbers[count] << ", ";
    }
    cout << "\n\n";

    system("PAUSE");
    return 0;
}

Topic archived. No new replies allowed.