count won't display correctly

closed account (j17Dy60M)
This isn't the finished product, but I need to read a file and the file has numbers in it, but i need to count the numbers that are between the values of 60 and 600. Whenever I run the code that count doesn't go up it starts over. My output looks like this
1 310
1 482
1 259
1 273
1 567
1 535
1 300

when I want it to look like this

1 310
2 482
3 259
4 273
5 567
6 535
7 300

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void printHeader();
void calcSongs(int);


int main(){
  const int maxMinutes = 80;
   
  ifstream inputFile;
  string filename;

  cout << "Enter filename: ";
  cin >> filename;

  inputFile.open(filename);
  int secs;

  printHeader();

  inputFile >> secs;
  while(inputFile){
    calcSongs(secs);
    inputFile >> secs;
    
  }
  
}
void printHeader(){
  cout << "Song \t\t\t Song Time \t\t\t\t Total Time "<< endl;
  cout << "Number \t\t Minutes  Seconds \t\t Minutes  Seconds" << endl;
  cout << "-----------------------------------------------------" << endl;
}
void calcSongs(int secs){
  int sum = 0, count = 0, time, minutes, seconds, totalTime, totalMin, totalSec;
  while(secs >= 60 && secs <= 600){
    count++;
    cout << count << "\t" << secs << endl;
    if(EOF){
      break;
    }
  }
}


Thanks for the help!
That is what you tell it to do.

Quite apparently EOF == true. Therefore your function is effectively:
1
2
3
4
void calcSongs(int secs) {
  int count = 1;
  cout << count << "\t" << secs << endl;
}
EOF is always a negative integer, usually -1.
if (-1) means if (-1 != 0), which is true.
Last edited on
Topic archived. No new replies allowed.