Write a C++ program to sum the values in the file with respect to the input of the user. The file contains a set of numerical inputs. Read all the inputs from th file and calculate the sum of odd and even numbers based on the choice of the user.
KINDLY SOLVE MY ERROR ACCORDING TO COMPILER ERROR YOU CAN REWRITE THE CODE IF NECESSARY.
THE ERROR IS RELATED TO "GetArr".
------------------------------------------------------------------------------
QUESTION AND COMPILER ERROR ATTACHED.
AND MY SOLUTION IS GIVEN BELOW :-
#include <iostream> #include <fstream>
using namespace std;
int getArr(int choice) { int num, sum=0; ifstream rfile; rfile.open("file.txt"); //open the file in read mode while (rfile >> num) //enters loop if there is num { if ((num % 2) == (choice % 2)) //if num leaving same remainder as of choice sum += num; //add num to sum } return sum; }
int main() {
int choice, sum = 0;
cout << "Enter the choice" << endl; cout << "1.Odd" << endl; cout << "2.Even" << endl; cin >> choice; //input choice cout << "The total of all numbers is " << getArr(choice) << endl; //print sum
return 0; } |
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images