# Thread: Short and Sweet Part 2.

1. ## Short and Sweet Part 2.

Why is it that when I use a calculator to get my answer, I come up differently than what the program gives.

What is wrong here??

Code:
```#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>

using namespace std;

int main()

{

ifstream inFile;
ofstream outFile;

float average;
float num,sum,count;
bool lessThanThirteen;

inFile.open("K:\\DATFILE1.TXT");
if ( !inFile )
{
cout<<"**Can't open input file**"<<endl;
return 1;
}

sum = 0;
count= 1;
lessThanThirteen = true;

while (inFile)

{
inFile >> num;
sum = sum + num;
count++;

}

while (lessThanThirteen)
{
lessThanThirteen = (count < 13);
average = sum / count;
}

outFile <<fixed<<setprecision(3)
<<"The average of the first 12 integers is "<< average << " ."<<endl;
inFile.close();
inFile.clear();
return 0;
}```

2. Is it supposed to average the first 12 numbers? Because right now it it adds up the ALL the numbers in the file and then if the count is less than 13, it divides that sum by the count.

You need to sum only the first 12 numbers which means you need to STOP adding numbers to sum when count gets bigger than 12.

Is it supposed to average the first 12 numbers? Because right now it it adds up the ALL the numbers in the file and then if the count is less than 13, it divides that sum by the count.

You need to sum only the first 12 numbers which means you need to STOP adding numbers to sum when count gets bigger than 12.

That is what I need to do. I think I can get this from here, but if not, I will be back.

4. I don't know if I'm overtired or what here....This is what I came up with but still not the correct answer. Am I going about this all wrong?

Code:
```inFile >> num;

while (inFile && count < 13)

{
sum = sum + num;
count++;
inFile >> num;

}

while (lessThanThirteen)
{
lessThanThirteen = (count < 13);
average = sum / count;
}```

5. I would write the same as
Code:
```	while (inFile >> num)
{
sum += num;
count++;
if(count >= 13)
break;
}
if(count > 0)
average = sum / count;```

6. Originally Posted by vart
I would write the same as
Code:
```	while (inFile >> num)
{
sum += num;
count++;
if(count >= 13)
break;
}
if(count > 0)
average = sum / count;```
Riddle me this... when I manually put my first 12 integers into a calculator I get an average of 340.083. Even when I use your code, I get an average of 313.923. Ughh.. this is going to be the death of me yet tonite.

7. Here is working code (without files for simplisity)
Code:
```#include<iostream>
#include<sstream>
#include<iomanip>
#include<cmath>

using namespace std;

int main()
{

float average;
float num,sum = 0.f;
int count = 0;

stringstream in ("10.0 20.0 30.0");

while (in >> num)
{
sum += num;
count++;
if(count >= 13)
break;
}

average = sum /count;

cout <<fixed<<setprecision(3)
<<"The average of the first " << count <<" integers is "<< average << " ."<<endl;
return 0;
}```

8. vart's code sums the first 13 integers. Perhaps that's why the average is higher? Try changing the 13 to 12.

9. Originally Posted by vart
Here is working code (without files for simplisity)
Code:
```#include<iostream>
#include<sstream>
#include<iomanip>
#include<cmath>

using namespace std;

int main()
{

float average;
float num,sum = 0.f;
int count = 0;

stringstream in ("10.0 20.0 30.0");

while (in >> num)
{
sum += num;
count++;
if(count >= 13)
break;
}

average = sum /count;

cout <<fixed<<setprecision(3)
<<"The average of the first " << count <<" integers is "<< average << " ."<<endl;
return 0;
}```
Whoa... that is way beyond what I have been instructed so far. I'm just a beginner here. LOL. There is alot here I don't know the meaning to. Take it back to wayyyy basic. I would like to understand what I'm doing. I'm just wondering if I don't have a problem with my visual studio. It wouldn't be the first time and how the heck it happens is beyond me.

10. Code:
```#include<iostream>
#include<sstream>
#include<iomanip>
#include<cmath>

using namespace std;

int main()
{

float average;
float num,sum = 0.f;
int count = 0;

stringstream in ("55 67 458 23 81 33\n782 375 528\n405 324 950 46\n14 864 551 38 167 518 630");

while (in >> num)
{
sum += num;
count++;
if(count >= 12)
break;
}

average = sum /count;

cout <<fixed<<setprecision(3)
<<"The average of the first " << count <<" integers is "<< average << " ."<<endl;
return 0;
}```
Output
Code:
`The average of the first 12 integers is 340.083 .`
You have to replace the in stream withyour inFile stream
It should not be a problem

11. Originally Posted by Daved
vart's code sums the first 13 integers. Perhaps that's why the average is higher? Try changing the 13 to 12.
I think his code is correct, it states greater than or equal to 13 to stop running.

ok maybe I'm just too tired.. <sigh>

12. Originally Posted by dispatch4599
I think his code is correct, it states greater than or equal to 13 to stop running.
But I start counting from 0, so if you want only 12 - you have to stop when count riches 12 - as in my last sample

13. >> I think his code is correct, it states greater than or equal to 13 to stop running.
It stops running after including the 13th item. I thought you only wanted 12.

14. Originally Posted by Daved
>> I think his code is correct, it states greater than or equal to 13 to stop running.
It stops running after including the 13th item. I thought you only wanted 12.
you are correct. I apologize. Now can someone explain to me what the float num, sum=0.f means.. mainly the 0.f? I have never seen this used before.

15. That just initializes the sum to zero. Since sum is a float, vart used a float value to initialize it. Numbers with decimals are doubles in C++, but since you are using floats, you have to add the f to the end to indicate that you want to use float. (doubles are actually recommended over float assuming you don't have a requirement to use float.)