0

Basically I'm trying to read from a text file which I created. I have a header of value 5, indicating that the rest of the file contains the data for 5 separate auctions.

The next line contains a value of 4, indicating 4 bids in the first auction. The four integers after that represent bids in the auction. Here's the entire text file:

5
4
80 70 100 270
2
8 7
1
800
7
90 81 72 685 49 50
4
800 900 785 600

Basically, I need to figure out the largest of the bid values for each auction. I can probably do that on my own, but I'm really stuck when it comes to representing those bid values as an array.

Here's my code so far:

#include <stdio.h>

int main()
{
    int header, i, j, cur_val, auction[cur_val];
    FILE *ifp;

    ifp = fopen("input.txt", "r");

    fscanf(ifp, "%d", &header);

    for (i = 0; i < header; i++) {
        fscanf(ifp, "%d", &cur_val);
        printf("%d\n", cur_val);

        for (j = 0; j < cur_val; j++) {
            fscanf(ifp, "%d", &auction[cur_val]);
            printf("%d\n", auction[cur_val]);
        }
        printf("\n");
    }
    //printf("Auction %d was sold for ");

    fclose(ifp);
    return 0;
}

If I create auction without the array then it prints the values but I have no way of accessing each individual value in order to determine the max value. How can I get the "bidding" values to act as an array of values? Using this code just crashes my entire program.

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
Josh
  • 55
  • 3
  • 10
  • 1
    `int header, i, j, cur_val, auction[cur_val];` `cur_val` has an unknown value, so your array is an unknown size. Apart from that, not clear what you are asking. You don't need an array just to find the max. – John3136 Oct 10 '16 at 23:19
  • Sorry, sort of hard to explain. Perhaps this will be more practical. How can I make it so that auction[0] would print 80, auction[1] would print 70, auction[2] would print 100, and auction[3] would print 270? Likewise, auction[0] would print 8 and auction[1] would print 7 when going to the next value of cur_val. – Josh Oct 10 '16 at 23:20
  • To keep things simple, just declare the array with a fixed size `int auction[200];` And then make sure you don't try to put more than 200 numbers into it :) Also, the index into the array is `j`, so `fscanf(ifp,"%d",&auction[j])` and the same for the `printf`. – user3386109 Oct 10 '16 at 23:25
  • It appears your data file has a consistency problem; you have a 'count' line of 7 but it is followed by just 6 numbers on the next line. Do you need to change the 7 to 6 or add an extra bid? – Jonathan Leffler Oct 10 '16 at 23:26
  • Note that using `scanf()` for 'line based' inputs is going to be an exercise in frustration. `scanf()` doesn't care in the slightest about newlines. You need to be using `fgets()` to read lines and then understand [How to use `sscanf()` in loops](http://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops) to analyze the multi-entry lines. Or you can simply decide that it doesn't matter: the whole input file could be one number per line, or all numbers on one line, or multiple blank lines between, or any permutation or combination of these — `scanf()` won't care. – Jonathan Leffler Oct 10 '16 at 23:29
  • @user3386109 you are a life saver! – Josh Oct 10 '16 at 23:30
  • @JonathanLeffler My professor hasn't yet introduced those but I've seen videos on them and have done some practice programs with them, but I'm using fscanf just to be safe since those are what the lectures have been on. – Josh Oct 10 '16 at 23:32
  • OK; then you need to use my (recently added) "don't care" option — just make sure the data is correct in the file. As it stands, there are 800 bids in the last auction — which will blow things sky-high. Do add a check to each `fscanf()` to ensure it reads a number — you should probably report an error (on `stderr`) and terminate if it fails. – Jonathan Leffler Oct 10 '16 at 23:35

0 Answers0