Thread: Beginner Problem with Loop

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    5

    Beginner Problem with Loop

    I wrote a program for an assignment that should read data from a file on the computer then display the sum of both the positive integers and negative integers. However, when I run my program it goes into an infinite loop. I need to understand what I am doing wrong. Any help would be greatly appreciated.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        ifstream inputFile;
        int even = 0;
        int odd = 0;
        int x;
    
        inputFile.open("C:....\hw4dataf11.txt"); //I have attached the file to this post
    
        if (!inputFile)
            cout << "Error opening file.\n";
    
        while(!inputFile.eof()){
    
            if(x % 2 == 0){
                even += x;
                cout << "Number of even numbers is: " << even << endl;
                even++;
            }
    
            else {
                odd += x;
                cout << "Number of odd numbers is: " << odd << endl;
                odd++;
            }
        }
    
    
        inputFile.close();
        cout << "Program is finished. \n";hw4dataf11.txt
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    Where is the variable x coming from? You open the file and you go into a loop, but you don't actually read in x. Put a priming read (read in x) before the while loop and then read in another x at the end of the while loop.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This is a common problem, discussed in the FAQ on this site.
    Cprogramming.com FAQ > Why it's bad to use feof() to control a loop

    Hmm, that article is more about C though. Here's a discussion of the same problem in C++.
    c++ - Reading from text file until EOF repeats last line - Stack Overflow
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    Thank you both very much. I appreciate the help. ^_^

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    Yes, that link did the trick!! Also, Linell, thank you for reminding me about the priming reads. You two really helped me out. ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner question: infinite loop
    By c++urious in forum C++ Programming
    Replies: 5
    Last Post: 12-12-2010, 03:07 AM
  2. beginner loop help
    By helloalyssa in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2010, 09:08 PM
  3. beginner question about stopping a for-loop.
    By Techboy10 in forum C Programming
    Replies: 3
    Last Post: 12-11-2008, 05:15 PM
  4. Help Please, Beginner Question, rewrite loop
    By office888 in forum C Programming
    Replies: 4
    Last Post: 12-11-2006, 10:07 AM
  5. trouble with a loop (beginner)
    By Procta in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2003, 10:27 AM