**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;
}