Friday, September 8, 2023

C++ program that reads numbers from a file and calculates their sum

// C++ program that reads numbers from a file and calculates their sum
using namespace std;
#include <iostream>
#include <fstream>
int main() {
// Open the input file
ifstream inputFile("numbers.txt");
if (!inputFile) {
cout << "Failed to open the file." << endl;
return 1;
}
double sum = 0;
double number;
// Read numbers from the file and calculate the sum
while (inputFile >> number) {
sum += number;
}
// Close the file
inputFile.close();
// Print the sum
cout << "Sum of numbers in the file: " << sum << endl;
return 0;
}

No comments:

Post a Comment

N-point Star in Microsoft Visual Studio Console App

#include <windows.h> #include <cmath> #include <iostream> LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,...