This program will read a bunch of numbers from an external file (numbers.txt) and calculate their total and average. In addition, the total will be multiplied by an integer that is supplied by the user.
** IMPORTANT: The numbers are included in your starter code as a separate file (numbers.txt). Don't touch that file! All of your code should be placed in code.cpp, as usual. **
Input:
After all numbers are read, what number would you like to multiply the total by? [user types: 2]
Output:
Total = 1717
Average = 50.5
Total * 2 = 3434
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
/*numbers.txt
20 30 40 50 60 70 80
*/
int main()
{
ifstream ifs("numbers.txt");
if (!ifs.is_open())
cout << "File isn`t opened!";
else
{
int val,number;
int total = 0;
int count = 0;
while (ifs>>val)
{
total += val;
count++;
}
cout << "Enter number would you like to multiply the total by: ";
cin >> number;
cout << "Output:" << "\nTotal = " << total
<< "\nAverage = " << (float)total / count
<< "\nTotal * " << number << " = " << total*number;
}
}
Comments
Leave a comment