Hi,
I am currently trying to create a program where I input a file called "exp.txt" that contains two columns: 1) date 2) expenses. There are 100 rows. I'd like for the program to read and load the data into an array, and then sort the expenses in ascending order and produce the average as specified by the user. In the very end, this should all be printed on the a separate output file called "final.txt". I'm having trouble writing the part where the user specifies the beginning and end date and that points to the array. Do I use the void mean() function? Thanks for your help!
#include <iostream>
#include <iomanip>
#include <fstream>
usingnamespace std;
void bubbleSort (int[], constint);
void mean (int[], int);
int main()
{
constint arraySize = 100;
int a[arraySize];
int i, hold;
char date [10];
double price;
ifstream inFile ("spx.txt"); // opens the input file
ofstream outFile ("final.txt");
if (!inFile) // if it is not able to open the file
{
cout << "Unable to open file" << endl;
exit (1);
}
while (date >> price)
{
for (i=0; i < arraySize; i++) // loop
cout << setw (10) << a[i];
}
bubbleSort (a, arraySize); // sorting the array
for (i=0; i < arraySize; i++)
cout << setw (10) << a[i];
cout << endl;
outFile.open ("final.txt");
outFile.close();
return 0;
}
void bubbleSort (int a[], int size)
{
int hold;
for (int pass = 0; pass <size -1; pass++) // passes
for (int i=0; i < size - 1; i++) // one pass
if (a[i] > a [i+1]) // then compare
{
hold = a[i];
a[i]= a[i+1];
a[i+1]= hold;
}
}
ifstream inFile ("spx.txt", ios::in); doesn't actually read the file. It just opens it. You need to explicitly read your data in.
From your comments, it sounds like the assignment is to read a file containing DATE-EXPENSE pairs and find the average EXPENSE between two specific DATEs. Is that right?
Hi.
I'm sorry - but what do you mean by " blocks please!"?
Yes - you have got that right. I'm very new at this - thanks for your patience. Also I made some changes. However, when I compile it, I get no errors. But when I run it, there are a bunch of numbers that just goes crazy. Please help!
He means edit your post, select your code, then click on the # symbol under format to put code tags around it so it looks like this:
1 2 3 4
if(true) {
//code
}
//blah
And anyway, for your new error, without code I can only really suggest you set a breakpoint (on VC++ you can click to the left of the line, a red dot will appear) and step through your code to find where the problem is.
Ah, so if your compiler/debugger are not integrated, then you probably can't set a breakpoint...you would have to give us your changed code so we can try to find the problem.
i think in line 27 you want
while( date > price)
not while (date >> price)
If someone could post the reason, I'd appreciate it. I'm currently learning C++ from a C background where that is bit shifting but I believe it means something different in C++.
edit: actually I'm confused as to what that while loop is doing. You never change date or price so that's an infinite loop unless I'm missing something.