-1

I have a problem for my CS class that I'm just not getting. I have to read an unspecified amount of integers in a text file. From there on I have to find the integers that are divisible by 5 and then add them up. Say I have a text file containing 1, 5, 7, 10, and 11, so my answer should be 15. The instructor says we have to use a while loop to read until End-Of-File.

Here is what I have, which is completely wrong:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int sum, even;
    sum = 0;
    ifstream inFile;

    inFile.open("numbers.txt");

    inFile >> even;

    do
    {
        if (even % 5 == 0)
        {
            sum = sum + even;
            cout << "\nThe sum of the numbers is:" << sum << endl;
        }

        else
        {
            cout << "\nNo numbers divisible by 5." << endl;
        }
    } 
    while (!inFile.eof());

    inFile.close();

    system("pause");

    return 0;
}

Something does happen; an endless loop that goes through large negative numbers.

Can anyone point me into the right direction?

Thanks,

Tobi

Update: I have this so far, but it prints out 4 answers.

#include <iostream> 
#include <fstream> 

using namespace std;

int main()
{
    int num, sum, count;
    sum = 0;
    ifstream inFile;

    inFile.open ("numbers2.txt");

    if (!inFile)
    {
        cout << "Can't open the input file.\n" << endl;
        system("pause");
        return 1;
     }

     inFile >> num;

     while (!inFile.eof())
     {
        inFile >> num;

        if (num % 5 == 0)
        {
            sum += num;
            cout << "Sum is: " << sum << endl;
        }

        else
        {
            cout << "Is not divisible by 5." << endl;
        }
     }

    inFile.close();

    system("pause");

    return 0;
}

My output looks like this:

  1. sum is: 5
  2. sum is: 25 (What I want as the output)
  3. Is not divisible by 5.
  4. Is not divisible by 5

I'll keep trying until I get this.

Thanks to everyone who has answered so far.

Tobi
  • 1
  • 3
  • Here's an example of while loop to [read file line by line in C](http://stackoverflow.com/questions/3501338/c-read-file-line-by-line) – zedfoxus Jul 28 '15 at 03:13
  • The program only 'reads' `even` once; so it will always be the first lines value. A new value needs to be read for each line/loop. Also, consider a while instead of a do-while. – user2864740 Jul 28 '15 at 03:19

1 Answers1

0

You can try something like this...not perfect, but just something you can use to understand and improve upon.

C++ example - program.cpp

#include <iostream> // provides cout, endl etc.
#include <fstream>  // provides file stream functionality
#include <string>   // provides string
#include <stdlib.h> // provides atoi functionality

using namespace std;

int main()
{
    int sum = 0;
    int num = 0;
    bool multiple_of_5_found = false;

    std::ifstream file("numbers.txt");
    std::string str;

    // while begins
    while(std::getline(file, str))
    {
        // convert string to number
        num = atoi(str.c_str());
        if (num % 5 == 0)
        {
            multiple_of_5_found = true;
            sum += num;
        }
    }
    // while ends

    // based on what we observed/calculated, print info
    if (multiple_of_5_found)
    {
        cout << "The sum of numbers divisible by  5 is "
             << sum << endl;
    }
    else
    {
        cout << "No number divisible by 5" << endl;
    }

    return 0;
}

GCC version

$> g++ --version
g++-4.7.real (Debian 4.7.2-5) 4.7.2

numbers.txt

$ cat numbers.txt
1
5
7
10
11
15

compile and run

$ g++ program.cpp && ./a.out
The sum of numbers divisible by  5 is 30

Visual Studio 2013 screenshot of numbers.txt resource Visual Studio 2013 screenshot of numbers.txt resource

Visual Studio 2013 screenshot of Source.cpp Visual Studio 2013 screenshot of Source.cpp

Visual Studio 2013 screenshot after pressing F7 and clicking on Local Windows Debugger Visual Studio 2013 screenshot after pressing F7 and clicking on Local Windows Debugger

zedfoxus
  • 35,121
  • 5
  • 64
  • 63
  • Somewhat helped, but I kept getting "No number divisible by 5." – Tobi Jul 28 '15 at 06:25
  • What are the contents of your file? Did you recompile this program with g++ and run it? On GNU/Linux, you could do `g++ program.cpp && ./a.out`. Ensure that numbers.txt is in the same directory. – zedfoxus Jul 28 '15 at 06:29
  • My file only looks exactly like the one you just posted. Except, my numbers are 1, 5, 20, 24, and 29. I am using Microsoft Visual Studio 2013. I attach the file to my resource files tab. I've done another few projects with the same parameters, but they worked just fine. – Tobi Jul 28 '15 at 06:43
  • Added screenshots from VS 2013 on my system. Works like a charm at my end. Perhaps you may want to run through breakpoints and see whether the code path travels through while loop or not. – zedfoxus Jul 28 '15 at 13:04