Look at your output table.... do you see anything in your code you could use as a counter? (hint: your days are numbered)
Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
>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
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
>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.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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 thetemperatures, 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.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
>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 addctemp 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.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
>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...
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
How about:
int total=0;
while (inFile) {
total += ctemp;
// ....
}
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
- (total=1; total<25; total++)
- (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.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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;
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339