-1

The goal of my program is to eventually read a file filled with numbers (chosen by the user) into an array and to output the min and max values. However, I cannot get my code to output the correct number of inputs in the file, which I want to use as the size of my array.

The code I have now is

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main(){
    string fileName;
    int count(0);
    cout << "Enter input file name: ";
    cin >> fileName;
    ifstream in_file;
    in_file.open(fileName);
    if(in_file.is_open()){
        in_file >> count;
        }
    cout << "The number of entries is " << count;
    int arrayNumbers[count];    
    for (int i = 0; i < count; ++i){
        in_file >> arrayNumbers[i];
        cout << arrayNumbers[i] << endl;
    }
    in_file.close();
}

I have a text file called tester.txt in the same directory as my .cpp file, but it only has 9 entries (1-9 on seperate lines) yet when I cout the count, it says the count is 12. I have seen in other questions like mine the use of

in_file >> count;

to count how many numbers are in a file, but I don't understand what this does since its my first time reading from a file. The file I am trying to read from has the following in it

1
2
3
4
5
6
7
8
9

I have not started the second part of the problem, finding the min and max, however I was just going to sort the array and then display arrayNumber[0] and arrayNumber[count-1] to show the min and max, but first I need to know how large to make the array based on the input file.

Connor Hale
  • 85
  • 1
  • 6
  • Please post the contents of the file from which you are reading. – R Sahu Sep 03 '18 at 02:55
  • having the first number in a file dictate the number of things to read is usually done for program test input (like competitions or school assignments) and is not indicative of all files – kmdreko Sep 03 '18 at 02:55
  • 2
    Consider `std::vector`. –  Sep 03 '18 at 02:56
  • I have included the contents of the file I am trying to read. Also @kmdreko , does using in_file >> count mean that the first entry in the file must be the number of entries in that file? If that is so, is something like while(!in_file.eof()) with a counter in the loop the correct way know how large to make the array? Also, we were told expressly to use arrays. – Connor Hale Sep 03 '18 at 03:05
  • yes, you'll have to read until the end or until some other designator. also, see [this](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) for stream reading because `while(eof)` has problems – kmdreko Sep 03 '18 at 03:15
  • `in_file >> count;` reads the number that goes into count from the file. So according to your program the first number in the file should be the count. If you are going to use an array and if the file doesn't equal the count then you need to read the file twice: once to count the entries before creating the array and once to read the entries into the array. – Jerry Jeremiah Sep 03 '18 at 03:32
  • 1
    Also, don't use eof in a loop condition: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Jerry Jeremiah Sep 03 '18 at 03:33
  • Here is an example using an array: https://onlinegdb.com/rkl_SE5vQ – Jerry Jeremiah Sep 03 '18 at 04:15
  • 1
    @JerryJeremiah Arrays are constant size, and if you're going to use an `std::vector`, you might as well just use `push_back`. You really shouldn't use VLAs. – BessieTheCookie Sep 03 '18 at 04:40
  • Do you really *need* to read into an array? You can solve this for an arbitrary number of inputs using only three integers (and no sorting). – molbdnilo Sep 03 '18 at 05:50
  • It doesn't say that the count is 12. It says that the count is 1 and then outputs the only value you read, 2. – molbdnilo Sep 03 '18 at 05:51
  • @FeiXiang I know that. I was assuming that because he used an array that he wasn't allowed to use std::vector - I think it is ridiculous but that's a very typical example of how they teach C++. – Jerry Jeremiah Sep 03 '18 at 22:58

2 Answers2

2

but I don't understand what this does

It reads the first number of your ifstream into count.

For the code you have to work as intended, you need to append the total count of numbers to the beginning of the input file so that your file will look like this.

9
1
2
3
4
5
6
7
8
9
P.W
  • 24,743
  • 6
  • 32
  • 69
0

thanks for all the advice, I managed to get it working as intended. I'll post my code below.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

string fileName;
int num, counter = 0;

int main(){
    cout << "Enter input file name: ";
    cin >> fileName;
    ifstream in_file;
    in_file.open(fileName);

    while(in_file >> num){
        counter = counter+1;
    }
    in_file.clear();                    //clear the eof flag after reaching end of file
    in_file.seekg (0, ios::beg);        //go back to the start of the file to read
    int arrayNumbers[counter];          
    for (int i = 0; i < counter; ++i){
        in_file >> arrayNumbers[i];
    }
    in_file.close();
    sort(arrayNumbers, arrayNumbers+counter);
    cout << "Min: " << arrayNumbers[0] << endl 
         << "Max: " << arrayNumbers[counter-1];
}
Connor Hale
  • 85
  • 1
  • 6