Hi fellow c++ programmers,

I need some help with an assignment from my c++ class. The assignment is to use a input file with the following

1 4/12/2007 34 -- 1 is the reference number, date , and temperature to be converted to farenheit.

Now i have to output it to a .txt file that looks like this:

CELCIUS TO FAHRENHEIT CONVERSION
----------------------------------
REF. DATE CONVERSION
1 4/12/2007 1°F
2 4/12/2007 1°F
3 4/12/2007 2°F
4 4/12/2007 3°F
6 4/12/2007 4°F
7 4/12/2007 5°F
8 4/12/2007 6°F
9 4/12/2007 6°F
10 4/12/2007 7°F
11 4/12/2007 7°F
12 4/12/2007 8°F
13 4/12/2007 9°F
14 4/12/2007 10°F
15 4/12/2007 10°F
16 4/12/2007 11°F
17 4/12/2007 11°F
18 4/12/2007 12°F
19 4/12/2007 13°F
20 4/12/2007 13°F
21 4/12/2007 14°F
22 4/12/2007 15°F
23 4/12/2007 15°F
24 4/12/2007 16°F
----------------------------------
Total temperature average:

my problem is, that i have been trying to use a counter to display the average and im stumped... simply put i have no idea how to do it and i cant seem to find information on how to do it anywhere. Heres the code i have done so far, any help or explanation is appreciated.

#include <iostream.h>
#include <fstream.h>
int Celsius(int); //Celsius conversion formula function
int main()
{
    // Variables Setup
    int ref, ctemp, myloop;
    char mydate[9];
    //File Management
    ifstream inFile; //Input File Logical Name
    ofstream outFile; //Output File Logical Name
    inFile.open("H:\\1.dat"); //Input File Merge Setup
    outFile.open("H:\\2.txt"); //Output File Merge Setup
    inFile >> ref >> mydate >> ctemp; // Read File
    
    //Headings
    outFile << "\tCELCIUS TO FAHRENHEIT CONVERSION" << endl;
    outFile << "\t";
    for (myloop=1; myloop<35; myloop++)
        {outFile << "-";} // Dash Line Print Cycle
        outFile << endl;
        outFile << "\tREF." << "\tDATE" << "\tCONVERSION" << endl << endl;
        // End of Headings
        while (inFile) //End-ofFile Controlled Loop
        {
              outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp)
              << "°F" << endl << endl; // Write Record
              inFile >> ref >> mydate >> ctemp; // Read File
        }
          outFile << "\t";
          for (myloop=1; myloop<35; myloop++)
          {outFile << "-";} // Dash Line Print Cycle
          outFile << endl;
          outFile << "\tTotal temperature average:" << endl;
          int number;
          int average;
          int count = 0;
          {
          ctemp += number;
          count++;
               }
                  average = ctemp / count;
   
               return 0;
               }

              int Celsius(int ctemp) // Celsius to Fahrenheit Conversion Function
              {
                  int ctof = 0;
                  ctof = (ctemp - 32) * 5/9;
                  ctemp = ctof;
                  return ctemp;
                  }

Look at your output table.... do you see anything in your code you could use as a counter? (hint: your days are numbered)

>my problem is, that i have been trying to use a counter to display the average and im stumped
The problem is that you need to store the data in some sort of array -- preferably a vector, and then you can calculate the average.

Vectors are nice, because they can dynamically resize. I suggest you read up on them here:
http://www.cprogramming.com/tutorial/stl/vector.html

how would i apply the vector to my code?? by using vector <int>; and including all the reference numbers or is there a more simple way because i really dont understand the explanation that is in that link. Thanks for the replies!

>how would i apply the vector to my code??
Basically keep push ing values into the vector as you read in new temperatures. Then outside of the loop, you would read them out and add up the total, yada yada.

Ok, so it would go something like this :

{
vector <int> num;         //Vector to store integers
    num.push_back(1);         
    num.push_back(2);       
    num.push_back(3); 
    //**etc. till 24       
    for(int x=0; x<num.size(); x++) 
    {
     cout << num[x]/24 << " ";
}

another question which values should i be pushing into the
vector from my input file, the temperatures in themselves
or just the reference numbers as above? or...am i going
completely wrong about this which i feel i am... argh! some
things are so easy and yet some things are so hard.

No... insert it here:

while (inFile) //End-ofFile Controlled Loop
        {
              // push_back 'ctemp' here

              outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp)
              << "°F" << endl << endl; // Write Record
              inFile >> ref >> mydate >> ctemp; // Read File
        }

>which values should i be pushing into the vector from my input file, the temperatures in themselves
Yes, the temperatures. You don't need to worry about the reference numbers; vectors have their own way of referencing values. Remember, you want to calculate the average of the temperatures, so that's what you're going to need to store, not anything else.

Once you're out of the loop, create another one to calculate the sum of all the temperatures, and then divide this total by the number of temperatures, which ends up being your average.

>my problem is, that i have been trying to use a counter to display the average and im stumped
The problem is that you need to store the data in some sort of array -- preferably a vector, and then you can calculate the average.

Why? Go simple.
In this loop:

while (inFile) //End-ofFile Controlled Loop
{
    outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp)
            << "°F" << endl << endl; // Write Record
    inFile >> ref >> mydate >> ctemp; // Read File
}

Simply add ctemp to a total counter and add 1 to a 'lines read' counter before your output. Then when you exit the loop, calculate total/lines. No need to store all the data unless you need it later in the program.

>No need to store all the data unless you need it later in the program.
It's good to plan ahead. I prefer doing things harder the first time if it makes future options easier. Now say you want to add some extra features to the program using the data you collected, wouldn't it be way easier if you already had done the groundwork?

But I see your point, and perhaps it's better in this case to calculate the average on-the-spot, since this is just a homework assignment...

Simply add ctemp to a total counter and add 1 to a 'lines read' counter before your output. Then when you exit the loop, calculate total/lines. No need to store all the data unless you need it later in the program.

sorry for replying so late, ive been kinda busy with other schoolwork,
so it would be something like this

while (inFile) 
        {

              int total = ctemp;
              (total=1; total<25; total++)
              outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp)
              << "°F" << endl << endl; // Write Record
              inFile >> ref >> mydate >> ctemp; // Read File
        }

where would i put the lines counter or how would i apply it to here??

ok. almost there, what about the line counter waltp suggested so that i could calculate the average?? how do i incorporate that

ok. almost there, what about the line counter waltp suggested so that i could calculate the average?? how do i incorporate that

Here's a start:

int counter=0; // increment this inside the loop, add +1 after the loop
int total=0;
        int count=0;
        while (inFile) //End-ofFile Controlled Loop
        {
               total += ctemp;
              (total=1; total<25; total++)
              outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp)
              << "°F" << endl << endl; // Write Record
              inFile >> ref >> mydate >> ctemp; // Read File
              (count=1; count<25; )
        }
             count++;
             average= total/count;

it should be something like that, i imagine, i must be bugging the hell outta u joeprogrammer, for that im sorry:rolleyes:. Its simply because up till now i havent done any programs that require counters of any sort, just very basic stuff, and the logic escapes me but thanks for your patience.

Put the incrementation inside the loop. And what are these lines supposed to do?

(total=1; total<25; total++)
(count=1; count<25; )

Here's how it should look:

int total=0;
int count=0;
while (inFile) //End-ofFile Controlled Loop
{
    total += ctemp;
    ++count;
    outFile << "\t" << ref << "\t" << mydate << "\t" << Celsius(ctemp)
            << "°F" << endl << endl; // Write Record
    inFile >> ref >> mydate >> ctemp; // Read File
}
++count;
average= total/count;

And you don't need to worry about bugging me, we were all newbies once.

  1. (total=1; total<25; total++)
  2. (count=1; count<25; )

I thought i had to define the number of lines within the input file, and i used that from another example code that i saw,
thats probably not the right way to do it. In other words i thought that was the incrementation or that thats how you do it

nevermind those 2 lines, it didnt make sense to include them, ok, i have a problem, the counter runs fine, but it is taking the temperatures from the input file, i need it to count the temperatures from the output file since those are the temperatures already converted how can i make the counter so that it counts the already converted temperatures??

I imagine it might be done with another decision?

while (inFile)

{ total += ctemp // .....

  1. (total=1; total<25; total++)
  2. (count=1; count<25; count++)

I thought i had to define the number of lines within the input file, and i used that from another example code that i saw,
thats probably not the right way to do it.

You were taking a section from a for() loop. A typical for() loop looks like this:

for (int i=0; i < x; ++i) {

   // blah blah
}

The part that actually does the incrementation is the last statement, ++i . All the rest is just loop-related stuff.

i have a problem, the counter runs fine, but it is taking the temperatures from the input file, i need it to count the temperatures from the output file since those are the temperatures already converted how can i make the counter so that it counts the already converted temperatures??

I imagine it might be done with another decision?

while (inFile)

{ total += ctemp // .....

Convert the amount to celsius before you add it onto the total. (not tested)

int total=0;
int count=0;
int celsiusTemp;
while (inFile) //End-ofFile Controlled Loop
{
    celsiusTemp = Celsius(ctemp); // convert it to celsius beforehand
    total += celsiusTemp;
    ++count;

    // don't have to call 'Celsius' here, use the variable 'celsiusTemp'
    outFile << "\t" << ref << "\t" << mydate << "\t" << celsiusTemp
            << "°F" << endl << endl; // Write Record
    inFile >> ref >> mydate >> ctemp; // Read File
}
++count;
average= total/count;

CELCIUS TO FAHRENHEIT CONVERSION
----------------------------------
REF. DATE CONVERSION

1 4/12/2007 1°F

2 4/12/2007 1°F

3 4/12/2007 2°F

4 4/12/2007 3°F

6 4/12/2007 4°F

7 4/12/2007 5°F

8 4/12/2007 6°F

9 4/12/2007 6°F

10 4/12/2007 7°F

11 4/12/2007 7°F

12 4/12/2007 8°F

13 4/12/2007 9°F

14 4/12/2007 10°F

15 4/12/2007 10°F

16 4/12/2007 11°F

17 4/12/2007 11°F

18 4/12/2007 12°F

19 4/12/2007 13°F

20 4/12/2007 13°F

21 4/12/2007 14°F

22 4/12/2007 15°F

23 4/12/2007 15°F

24 4/12/2007 16°F

----------------------------------
Total temperature average:8

End result= perfect, only thing left is to clean up the code a little bit and im done.


joeprogrammer.... what can i say man, thanks so much for helping me out, i appreciate it in full, after so many questions thanks for sticking with me. have a nice week.:cheesy:

This question has already been answered. Start a new discussion instead.