Answer to Question #309454 in C++ for nickname

Question #309454

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



1
Expert's answer
2022-03-11T07:23:34-0500
#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;
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog