**HELP** Stuck in infinite loop! Count all negative #'s in File??

This is a discussion on **HELP** Stuck in infinite loop! Count all negative #'s in File?? within the C++ Programming forums, part of the General Programming Boards category; Hi, I've been stuck on this problem for way too long, can any of you help me out?? My assignment ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    11

    **HELP** Stuck in infinite loop! Count all negative #'s in File??

    Hi,
    I've been stuck on this problem for way too long, can any of you help me out?? My assignment is as follows:
    You must implement the menu system using a do {} while loop. For each of the options available in the menu, you may use a loop of your
    choice, as long as at least one of the options is implemented using a for loop, and at least one of the options
    is implemented using a while loop. The options for the menu are:
    1. Count all negative numbers in a file (filename is "pa5.numbers")
    2. Average of all non-negative numbers in a file (filename is "pa5.numbers")
    3. Sum all whole numbers between x & y, where x & y are input by the user
    4. Quit

    This is my sample input file:
    34 67 2 0 52 13 8 7 14 31 90 49 64 82 67 33 43 41 42
    199 2006 18 15 6 9 12 16 60 57 45 48 57 1 4 5 14

    I am still stuck on #1!!! It keeps going in an infinite loop and the counter++ is not even counting.. it is still stuck on the first number in the input file.

    I did a cout to see what it was printing, and it prints a 0 for count, and 67 for num.
    Can anyone help?? If anyone knows how to do #'s 2-4 also it would be greatly appreciated. Thanks

    Felix

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    int main()
    {
        ifstream inFile;
        int count, choice;
        int num;
        
        count=0;
    
        inFile.open("pa5.numbers.cpp");
        
        if (inFile.fail())
            {
               cout << "Error Opening input file pa5.numbers! \n";
               system("pause");
               return 0;
            }    
        
        do
        {
                 cout << "1 - Count Negatives" << endl;
                 cout << "2 - Average Non-negatives" << endl;
                 cout << "3 - Sum Numbers" << endl;
                 cout << "4 - Quit" << endl;
                 cin >> choice;
                 
                 if(choice == 1)
                 {          
                           while (!inFile.eof())
                           {
                                 inFile >> num;
                                 if(num < 0)
                                        {
                                        count++;
                                        }
                                 else
                                     inFile >> num;
                                     cout << count << endl;
                           }
                 } 
    
                               inFile.close();
                               inFile.clear();
        }while(choice !=4);
        
      system("Pause");
      inFile.close();
      
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> inFile.open("pa5.numbers.cpp");
    I thought the file with the data was called pa5.numbers.

    >> if (inFile.fail())
    You should test to see if the file opened with is_open(), not fail(). That might be what is causing the infinite loop- the file isn't opening.

    Also, why do you do inFile >> num; twice inside your loop?

    Something you should change is the while (!inFile.eof()). That is not the proper way to loop through a file. Hopefully your instructor didn't teach that. The problem is that you can loop to the end of the file but still have a new line at the end so the eof() flag doesn't trigger, then you run the loop one extra time using the previous values and it throws off your calculations. Instead, you should put the one and only inFile >> num inside the while control.

    Finally, just a general C++ tip: you should declare your variables as late in the code as possible and only when you are ready to initialize them to some default value, and you should initialize your variables if there is any chance that they don't have a value assigned to them before being used. In this case you don't need num until much later in the program, and if you initialized it to something you might be able to tell if it is actually getting changed.
    Last edited by Daved; 10-23-2006 at 05:29 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Judging by your indentation, it appears that your intent in the else clause of the if-else statement was to execute both statements, not just the first. But since they're not enclosed in brackets, that's not going to happen.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    11
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    int main()
    {
        ifstream inFile;
        int count, choice;
        int num, sum;
        
        count = 0;
    
        inFile.open("pa5.numbers.cpp");
        
    //    if (inFile.fail())
    //        {
    //           cout << "Error Opening input file pa5.numbers! \n";
    //           system("pause");
    //           return 0;
    //        }    
        
        do
        {
                 cout << "1 - Count Negatives" << endl;
                 cout << "2 - Average Non-negatives" << endl;
                 cout << "3 - Sum Numbers" << endl;
                 cout << "4 - Quit" << endl;
                 cin >> choice;
                 
                 if(choice == 1)
                 {          
                           inFile >> num;
                           while (!inFile.eof())
                           {
                                 if(num < 0)
                                    count++;
                                    inFile >> num;   
                                    cout << count << endl;
                           }
    
                 } 
    
                 
                               inFile.close();
                               inFile.clear();
        }while(choice !=4);
        
                                         
        
      system("Pause");
      inFile.close();
      
      return 0;
    }
    The reason it is pa5.numbers.cpp is because i'm running it in devC++ and it keeps the file extension.

    I printed out the input file variable num, and it loops "67" over and over again. It is not getting past the first 2 numbers in the input file because they are not negative. Since the if statement seems false, the count++ also isn't counting. I know it is reading my input file, but I can't find why.. anyone?

    I took out the else statement inside my if statement and also took out the "if(inFile.fail()" statement to see if that was the cause of the infinite loop but its still doing it..

    And to answer your question, yes my teacher did tell me to put "while(!inFile.eof())" to start it off? What is the proper way to read an input file from a while statement?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    As is that code should work. Make sure the - in the input file really is a -. Is it possible you copied the data from Word or something and pasted it into Dev-C++? Open the pa5.numbers.cpp file in Notepad and re-type the '-' characters, or create a new file in Notepad from scratch.

    >> What is the proper way to read an input file from a while statement?
    Your current code avoids the problem with looping one too many times, because you check for eof() after you read but before you use the value. That is good. An even better option is to use inFile >> num as the while control. That works better because not only does it stop the loop when the end of the file is reached, but also if any read errors occur. For example, if the - is a bad character, or if another non-numeric character is in the file, it will cause an infinite loop because it will stop the stream from reading in another number but not set the eof bit. You could also replace eof() with bad() to achieve the same effect.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    11
    yep, it was the negative (-) in the input file the whole time.. wow... thanks alot

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    If you're bold enough this would be a great example to show your teacher of why while (inFile >> num) is better than while (inFile.eof()). The second one might seem more intuitive and therefore better to use when you don't want to focus on such details, but the first is just a better option overall.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Program gets stuck in infinite loop
    By Xanth in forum C++ Programming
    Replies: 10
    Last Post: 02-07-2005, 11:51 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21