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. Essentially I'm trying to read a file containing DATE-EXPENSE pairs and find the average EXPENSE between two specific Dates. Eventually, I will write the results to an output file called "final.txt". Currently, I'm having trouble writing the part where the user specifies the beginning and end date and that points to the array. Can you give me some help on where I can write the code to specify dates? Do I use the void mean() function and call that in int main()? Thanks for your input!

**********************************************************

#include <iostream> 
#include <iomanip>
#include <fstream>
using namespace std;

void bubbleSort (int[], const int); 
void mean (int[], int); 


int main()
{
  const int arraySize = 100;
  int a[arraySize];
  int i, hold;
  char date [10];
  double expense;

  ifstream inFile ("exp.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 >> expense)  // reads the file
  {
    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;
      }
}



void mean (const int answer[], int size)
{
  int total = 0;
  for (int i = 0; i < size; i++)
    total +=  answer[i];
}

Please add code tags.
[CODE] Code Here [/CODE]

Hi. I just added before the beginning and after the end -- the last bracket.
Please let me know if I've misunderstood. Thanks!

You used the code tags correctly ;)

First:
There is a mistake on line 27.

while (date >> price)  
  {
    for (i=0; i < arraySize; i++)  // loop
      cout << setw (10) << a[i];
  }

date has the data type char[10] and price has the type double. What exactly are you trying to do here? If you want the variable date to convert to the variable price, either use stringstream, or a quick solution, atof.

No no. Hi, I made some slight changes, so please use the code above.

And as for the part,

while (date >> expense)

// reads the file

I thought it reads the two columns, one labeled date and the other labeled expense. The book I'm using describes it as sort of a cin reading of data from an input file.

That is still an error, date has the data type char[10], you can only use the >> operator on a class that has that particular operator.

I think I"m confusing you. Sorry.

The date is already in this format: mm/dd/yyyy. So the char[10] would be incorrect usage, rigth?

I think I"m confusing you. Sorry.

The date is already in this format: mm/dd/yyyy. So the char[10] would be incorrect usage, rigth?

while (date >> expense)  // reads the file

date is a variable, not a file and not a stream.

const int arraySize = 100;
  int a[arraySize];
  int i, hold;
  char date [10];
  double expense;

  ifstream inFile ("exp.txt"); // opens the input file
  ofstream outFile ("final.txt");

Are you trying to read from a file here?

while (date >> expense)  // reads the file

If so, I imagine you want to read from inFile , which is an ifstream , not date , which is a char array..

Let me try again. I have instead defined the date as a string for that is what the format is.

***************************

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void bubbleSort (string, double);
void mean (string, double);

int main()
{
  const int arraySize = 100;
  int a[arraySize];
  int i, hold;
  
  
  ifstream inFile ("exp.txt"); // opens the input file
 
  if (!inFile) // if it is not able to open the file
    {
      cout << "Unable to open file" << endl;
      exit (1);
    }
   
  double expense;
  string date;
  
 
  cout << setiosflags (ios::left ) << setw (10) << "Date"
<< setw (10) << "Expense\n" ; 

  inFile >> date >> expense;  // reads the file
  while (!inFile.eof() ) // keep reading until end of file
  {
    for (i=0; i < arraySize; i++)  // loop
      cout << setw (15) << a[i];
  }



// Bubble sort in ascending order

  bubbleSort (a, arraySize); // sorting the array

  for (i=0; i < arraySize; i++)
    cout << setw (15) << a[i];

  cout << endl; // Loads the sorted expenses 
  

   

// Getting the mean for a particular date
   ********UNFINISHED CODING  
    



// Create formatted text file for printing

  ofstream outFile ("final.txt", ios::out);
  if (!outFile)
    { cout << "Unable to open file" << endl;
      exit (1);
    }
  if (outFile.open ())
    {
      **********UNFINISHED CODING

      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;
      }
}


void mean (const int answer[], int size)
{
  int total = 0;
  for (int i = 0; i < size; i++)
    total += answer[i];

}
while (date >> expense)  // reads the file

date is a variable, not a file and not a stream.
-----> yes, so I put it as a string, instead of a character.. The top should instead read as:

inFile >> date >> expenses; // here - strings can be streamed in, yes?

const int arraySize = 100;
  int a[arraySize];
  int i, hold;
  char date [10];
  double expense;

  ifstream inFile ("exp.txt"); // opens the input file
  ofstream outFile ("final.txt");

Are you trying to read from a file here?

YES. I'm trying to read from a file.

while (date >> expense)  // reads the file

If so, I imagine you want to read from inFile , which is an ifstream , not date , which is a char array..

Please let me know if I've misunderstood your explanations. Thanks so much!

Please let me know if I've misunderstood your explanations. Thanks so much!

Yes, this is better:

inFile >> date >> expense;  // reads the file

The issue wasn't whether date was a string or a character array, but that the way you had it before:

while (date >> expense)

had date on the LEFT side of the >> symbol rather than the RIGHT side and that inFile was not in that line at all. The ifstream must be on the left side of the first >> symbol. All the variables that are going to receive data FROM the ifstream must be to the right of a >> symbol. This is fine:

inFile >> date >> expense;

and is the same as:

inFile >> date;
inFile >> expense;

You can have a variable in between two >> symbols as you now have, but you can't have a variable BEFORE the first >> symbol as you had it before.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.