-
no output
I am having problems getting output on this program. I want to be able to display the contents of two parrallel arrays read from a file. It is reading my headings alright but can't read from the arrays. Any help would be welcomed.
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;
}
-
I would suggest going over the chapter on arrays in your book again. readData isn't filling either array, it's reading two values and placing one in the first element of each array. The problem really isn't in the reading two values part, it's in the assigning to the first element at all times part. Your printResults function seems confused as to what types you're working with.
-
Yea that is my problem, the crappy book I have explains nothing about reading from a file, much less creating a function to read an array from file. It has been all guesswork at this point so far.
-
Any good websites that might be able to show me how to do this?
-
To read:
Code:
// Fill the array with values from a stream
// Return the number of values read
int readFile(istream& in, int array[], int size)
{
int i;
for (i = 0; i < size; i++) {
if (!(in>> array[i]))
break;
}
return i;
}
To write:
Code:
void writeFile(ostream& out, int array[], int size)
{
for (int i = 0; i < size; i++)
out<< array[i];
}
To use them:
Code:
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream in("somefile");
if (!in) {
cerr<<"Error opening input file"<<endl;
exit(EXIT_FAILURE);
}
int a[50];
int n;
n = readFile(in, a, 50);
writeFile(cout, a, n);
}
-
Ok I get that, but what im confused on is how to read in 2 columns of data and put each column in a different array.
-
>but what im confused on is how to read in 2 columns of data and put each column in a different array.
That's actually the easy part. Assuming equal length columns:
Code:
int readFile(istream& in, int a[], int b[], int size)
{
int i;
for (i = 0; i < size; i++) {
if (!(in>> a[i] >> b[i]))
break;
}
return i;
}
-
awesome it should be smooth sailing from here. Thank you so much.