-
reading from file
Having a problem reading from an input file. Have two sets of data- account numbers and balances. I set them up in parallel arrays but the book im using does not explain anything about reading from a file. Just curious how I could read the arrays from a function.
-
-
Something is wrong with my functions but I havent been able to find a good resource to help me out. I am getting errors saying my customer and balance array are unrefereneced. Any help would be appreciated
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
void headings(ofstream &outFile);
void readData(ifstream &inFile);
void sort(ifstream &inFile);
int main()
{
const int size = 50;
//declare variables
int customer[size];
double balance[size];
//open files
ifstream inFile;
ofstream outFile;
inFile.open("prog7.txt");
outFile.open("prog7.out");
headings(outFile);
// read from file
double temp = 0;
readData(inFile);
while (!inFile.eof())
{
readData(inFile);
temp ++;
}
sort(inFile);
outFile.close();
inFile.close();
return 0;
}
void headings(ofstream &outFile)
{
outFile <<setw(10)<< "Customer ID"
<<setw(10)<< "Balance"
<<endl;
}
void readData(ifstream &inFile)
{
double temp = 0;
inFile >> customer[temp] >> balance[temp];
}
void sort(ifstream &inFile)
{
for(int j=1; j< size; j++)
{
for(int i=0; i< size-1; i++)
{
if(balance[i] > balance[i+1]
{
temp=balance[i];
balance[i]=balance[i+1];
balance[i+1]=temp;
}
}
}
}
void printResults(ofstream &outFile, ifstream &inFile)
{
outFile << setprecision(2) << fixed << showpoint;
outFile <<setw(10)<< customer[size]
<<setw(10)<< balance[size]
<<endl << endl;
}
-
You need to pass them to the functions. customer is declared in main so only main can see it.
IE:
Code:
void readData(ifstream &inFile, int customer[], double balance[])
{
double temp = 0;
inFile >> customer[temp] >> balance[temp];
}
which then would be called with:
Code:
readData(inFile, customer, balance);
However that function has an error in that temp is a double but you are using it as the index to an array
-
Hi. I get a bunch of compile-time errors with your code. Be careful:
Code:
if(balance[i] > balance[i+1]
You haven't closed the parenthesis of the if conditional.
Correct:
Code:
if(balance[i] > balance[i+1])
{
/*...*/
}
Good Luck,
Steve
-
ok thanks for the help so far. I cleaned up the code and managed to get no errors after debugging, but still am not getting any output. Not sure how to add reference variables to arrays while in a function. Any help on how to do this or what my problem might be would be greatly appreciated.
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#define size 50
using namespace std;
void headings(ofstream &outFile);
void readData(ifstream &inFile, int customer[], float balance[]);
void sort(ifstream &inFile, float balance[]);
void printResults(ofstream &outFile, ifstream &inFile, int customer[], float balance[]);
int main()
{
//declare variables
int customer[size];
float balance[size];
//open files
ifstream inFile;
ofstream outFile;
inFile.open("prog7.txt");
outFile.open("prog7.out");
// funtion to print headings
headings(outFile);
// read from file
float temp = 0;
readData(inFile, customer, balance);
// loop
while (!inFile.eof())
{
readData(inFile, customer, balance);
temp ++;
}
// function to sort in descending order
sort(inFile, balance);
// function to print results
printResults(outFile, inFile, customer, balance);
outFile.close();
inFile.close();
return 0;
}
void headings(ofstream &outFile)
{
outFile <<setw(10)<< "Customer ID"
<<setw(10)<< "Balance"
<<endl;
}
void readData(ifstream &inFile, int customer[], float balance[])
{
static int i = 0;
static int j = 0;
inFile >> customer[j] >> balance[i];
}
void sort(ifstream &inFile, float balance[])
{
float temp = 0;
for(int j=1; j< size; j++)
{
for(int i=0; i< size-1; i++)
{
if(balance[i] > balance[i+1])
{
temp=balance[i];
balance[i]=balance[i+1];
balance[i+1]=temp;
}
}
}
}
void printResults(ofstream &outFile, ifstream &inFile, int customer[], float balance[])
{
outFile << setprecision(2) << fixed << showpoint;
outFile <<setw(10)<< customer
<<setw(10)<< balance
<<endl << endl;
}