Hello Gieniusz Krab,
This is what I see with your program:
Is this a C or C++ program?
#include <cstdio>
is not needed here. And I do not see where
#include <algorithm>
is used anywhere.
Consider this bit of code:
1 2 3 4 5 6 7 8
|
if(inputf.fail())
{
cerr <<"ERROR"<<endl;
exit(1);
}
inputf.open("exemplar.txt");
|
How do you check if a file is open before it is opened? It does work and does not cause any problem with the way the program works for now, but some day in another program it could be a problem.
In the code:
1 2 3 4 5 6
|
if(important<11)
{
sum = sum + important;
average = sum / important;
}
}
|
Not the way do do the average. This should be done after the while loop. Also I would suggest making "average" a double and not an int. This would give a more accurate number for the average with the division. Dividing by "important" does work for this program, but for different numbers it will fail. See later code for what I changed.
The "printf" statements can easily be changed with the "cout" statements I changed to. The use of "std::setw(?)" needs the header file "iomanip".
This is your program with the changes I made. Notice the comments in the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include <iostream>
#include <fstream>
#include <iomanip> // <--- for setw().
int main()
{
// <--- Always initialize your variables.
int T[17]{ 0 }; // <--- It will initialize the whole array to 0.
int sum = 0;
int count{ 0 }; // <--- Added this variable.
double average{ 0 }; // <--- Changed to double.
int important{ 0 };
int evenmoreimportant{ 0 };
std::ofstream outputf;
std::ifstream inputf;
outputf.open("exemplar.txt");
for (int i = 1; i < 17; i++)
{
T[i] = i;
outputf << T[i] << '\n';
}
outputf.close();
inputf.open("exemplar.txt");
if (inputf.fail())
{
std::cerr << "ERROR" << std::endl;
exit(1);
}
while (inputf >> important)
{
if (important < 15)
{
//printf("%8d\n", important);
std::cout << std::setw(8) << important << std::endl;
}
if (important < 11)
{
sum += important; // <--- Same as sum = sum + important.
count++;
}
}
// <--- The "static_cast" is to make sure the division is done by doubles not ints.
average = static_cast<double> (sum) / static_cast<double> (count);
inputf.close();
outputf.open("newfile.txt");
inputf.open("exemplar.txt");
while (inputf >> evenmoreimportant)
{
if (!(evenmoreimportant % 3 == 0))
{
outputf << evenmoreimportant << std::endl;
//printf("%d\n", evenmoreimportant);
std::cout << std::setw(3) << evenmoreimportant << std::endl;
}
}
inputf.close();
outputf.close();
std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << "\nAverage of those numbers that are smaller than 11 is: " << average << std::endl;
// Something here to keep the window open if you need it.
// See http://www.cplusplus.com/forum/beginner/1988/
return 0;
}
|
One last point
using namespace std;
is not a good practice. It
WILL get you in trouble some day.
Hope that helps,
Andy