Unused variable: Arrays and Output text

I guess its that time of year everyones working on arrays and file outputs. So here is my problem. Eclipse is telling me that I have unused variable "i" in line 9. This program is intended to take an input file of 100 numbers and order them. There is a file output in my directory, out.txt which does it. But no file appears in terminal. Maybe call it again? I don't know, this is driving me nuts. Thanks forums. you are great.
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;

void read_data (int A[], int size)
{
	int x, i=0;
	char name[20];
	ifstream fin;

	cout<<"This program limits file names to a maximum of 19 characters\n"
		<<"Enter filename: ";
	cin>>name;

	fin.open(name, ios::in);

	if (fin.fail())
	{
		cerr<<"File could not be opened.\n";
		cin.get();
		exit(1);
	}
	for (int i=0; i<size; i++)
	{
		fin>>x;
		A[i]=x;
	}
	cout<<endl;
	fin.close();
}

void print_data (int A[], int size)
{
	ofstream fout;
	fout.open("out.txt", ios::out);

	for (int i=0; i<size; i++)
	{
		fout<<setw(7)<<A[i];
	}
}

void bubble_sort (int A[], int size)
{
	double temp;
	for (int i=0; i<size-1; i++)
	for (int j=0; j<size-1; j++)
	{
		if (A[j]>A[j+1])
		{
			temp=A[j];
			A[j]=A[j+1];
			A[j+1]=temp;
		}
	}
}

int main()
{
	const int size=100;
	int A[size]={0};
	read_data(A, size);
	cout<<"Unsorted data: \n";
	print_data(A, size);
	bubble_sort(A, size);
	cout<<"\nSorted data: \n";
	print_data(A, size);
	return 0;
}

I think there is very little wrong here.

The unused variable isn't a big deal, but you could simply delete the unwanted 'i' to get rid of the warning message.
As for the output appearing in the terminal. Well, apart from a couple of messages, all the output is sent to the output file "out.txt" in function print_data().

The main problem (if you want to consider it a problem) is that the unsorted data is output at line 66. Then at line 68 that same file is used to output the sorted data. Thus, unless you pause the program after line 66, you won't ever see the unsorted data as it gets overwritten by the sorted version.

In any case, the original input file contained the unsorted data. But if you want to keep both outputs, use two different file names (maybe pass the filename as a parameter to the function print_data).
Topic archived. No new replies allowed.