"Write a program that reads a set of integers and then finds and prints the sum of the numbers that are evenly divisible by 5"

So here is what I am trying to do: "Write a program that reads a set of integers and then finds and prints the sum of the numbers that are evenly divisible by 5. You can use modulo for this; if the remainder is 0, then the number is evenly divisible by 5. For example, suppose your list is 13, 20, 16, and 15. 20 and 15 are evenly divisible by 5, so the sum would be 35. You should come up with your own file for input, and you can format the numbers any way you want (spaces between the numbers, tabs between the numbers, one number per line, etc.). Make sure you use a while loop to read until EOF. Your program should work if the amount of numbers in the input file are changed, so don’t assume that you know how many numbers are in the file."

I have been searching through textbooks and websites all over and I cannot find a solution for this. I am obviously a beginner so if anyone can think of a solution for this it'd be greatly appreciated.
Make sure you use a while loop to read until EOF

Tell your instructor that he is still stuck in the nineties.
Today you check if the state of the input stream is good.
If the state is good then you can read data, if not the end of the file might have been reached.

I have been searching through textbooks and websites all over and I cannot find a solution for this. I am obviously a beginner so if anyone can think of a solution for this it'd be greatly appreciated.

Of course you are not.
This is an exercise for you, what have you done so far?

I give you a few hints:
- use the fstream library

that's it, no more information necessary.
I think I'll do you the favour of making nearly all of your programm.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <fstream>

int main()
{
    std::ifstream file("test.txt");
    int sum;
    int n;
    while(file >> n) // if you can read a number
    {
        // do something with it
    }
    return 0;
}
Last edited on
this shld do the trick

#include <iostream>
using namespace std;

int main ()
{
int number,total;



cout<< "enter a block of intergers and (-1) to end \n";
cin>>number;
total=0;
while(number!=-1)
{
cout << "enter the next number";
cin>>number;
if (number%5==0)

total+=number;

}
cout<<"the sum total of all numbers divisible by 5 is" << total <<endl;
return 0;

}
@ArnoldMakore: nah that won't do, he is supposed to read the integers from a file.
You should come up with your own file for input, and you can format the numbers any way you want
@gamer2015: my bad (kkk)... hadnt read the qsn properly...but I think cha0sweaver (1) gets the idea of the while loop and use of modulus
I think I can take care of the rest from here but I will let you know. Thanks a lot for the help. I had similar code to what Gamer2015 made but it was more complex and I think I just over-thought the whole thing.
Topic archived. No new replies allowed.