1

I am trying to write a program which will ask the user to input an integer, and will then proceed to search a predefined text file (containing integers only) for the number of occurrences of that specific integer, printing the result. Here is what I have coded so far (doesn't work when I run it), but I don't know if I'm going in the right direction here, or if I should be trying to read all the integers from the file into an array, or even do something else entirely. I should add that this is for homework, and I am just looking for pointers, not a full solution. I have tried everything I can think of, and haven't been making much progress. Could someone point me in the right direction? Thanks in advance.

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    int x, y;
    int sum1 = 0;
    ifstream infile ("file.txt");
    if (infile)
        cout << "File successfully opened" << endl;
    else
    {
        cout << "Can't open file" << endl;
        exit (EXIT_FAILURE);
    }
    cout << "Input number: ";
    cin >> x;
int number;
while(!infile.eof()) //this is where my problems start
         {
           infile >> number;
         }
while(number = x) //doesn't seem to be working, not sure if I should even be using
                  //a while statement
         {
           sum1++;
         }
        cout << sum1++ << " " ;
}
maelstrom
  • 23
  • 1
  • 6
  • look at the warnings your compiler is telling you. I bet your compiler has found the error already. – Mooing Duck Jul 25 '13 at 19:37
  • Please don't do `while(!infile.eof())`, it's [wrong](http://stackoverflow.com/questions/21647/reading-from-text-file-until-eof-repeats-last-line). – jrok Jul 25 '13 at 19:38

2 Answers2

4

You are using single = in comparison statement: while(number = x) which is wrong. use == in comparison

Do it like:

while(number == x)  //note ==
             ^^

Other thing to note is that you are using while 2 times that's logically not good and using !infile.eof() is also not good as you may not get expected results (1 more than original)

Try this code:

int number;

while (true) {

    infile >> number;
    if( infile.eof() )      //This line will help in avoiding processing last value twice 
      break;

    if (number == x)
    {
        sum1++;
        cout << sum1++ << " " ;
    }
}
Shumail
  • 3,103
  • 4
  • 28
  • 35
  • remove the second infile>>number :) – rabensky Jul 25 '13 at 19:51
  • I'm not sure I fully understand how end of file works, or what you mean by avoiding the last value being processed. I was told to use while(!infile.eof()), and after having done some testing it seems to be working in this case. However, I will try using your solution as well, and will read up on end of file so that I understand what is going on a bit better. Thanks for taking the time to answer! – maelstrom Jul 25 '13 at 20:34
  • Well you are started i think. You will know with time about usage of `eof` - btw, how come my answer was different from accepted one ? I was first to answer your question. Vote UP atleast. – Shumail Jul 25 '13 at 20:51
  • I chose the other answer as I understand its code better at this time and it is the one that I ended up using. I appreciate you taking the time to answer, and I had already tried to vote up but unfortunately it won't let me as I just signed up and have less than 15 reputation. – maelstrom Jul 25 '13 at 21:17
  • @maelstrom : I see you have 18 points now and CAN vote for me. Please VOTE now – Shumail Jul 25 '13 at 21:20
  • Up voted your answer. You can ease off on the Caps Lock now. :) – maelstrom Jul 25 '13 at 21:33
3

Do this:

// ...
int number;
infile >> number;
while(!infile.eof())
{
    if (number == x)
    {
        sum1++;
    }
    infile >> number;
}
cout << sum1;
wjmolina
  • 2,625
  • 2
  • 26
  • 35