0

Here's the code in question:

int participant_count; int budget; int hotel_count; int week_count;
int minimum_cost = INT32_MAX;
while (cin >> participant_count >> budget >> hotel_count >> week_count) {
    int hotel_price;
    int *available_bed_count = new int[week_count];
    while (cin >> hotel_price) {
        for (int i = 0; i < week_count; i++) {
            scanf("%d", available_bed_count[i]);
            if (available_bed_count[i] >= participant_count) {
                minimum_cost = min(minimum_cost, (hotel_price * participant_count));
            }
        }
    }
    delete []available_bed_count;
}

and here's the textfile that I'm reading from. I've drawn an arrow to highlight the exact line that I cannot properly handle

3 1000 2 3
200
0 2 2  <---
300
27 3 20
5 2000 2 4
300
4 3 0 4
450
7 8 0 13

Unfortunately, my code breaks and nothing gets output to my file once the scanf function is reached and I don't know why. Could someone please tell me what I'm doing wrong? I'm thinking that there might be an issue with using 'cin' and 'scanf' together where I might be attempting to read from the same line that 'cin' read from, but I don't really know.

mooglin
  • 500
  • 5
  • 17
  • 1
    So the obvious question is, why? Why not do `cin >> available_bed_count[i]` and avoid this problem? – john May 04 '20 at 19:59
  • Don't mix C and C++ IO. Pick one and use it exclusively. – NathanOliver May 04 '20 at 20:01
  • Incidentally there's no need for the array `available_bed_count`. You only ever use one value of that array at a time, so you could just as easily use an `int` variable. There's no need for an array. – john May 04 '20 at 20:01
  • I didn't even know I could do that. I assumed that each call to cin would start and end on a different line – mooglin May 04 '20 at 20:09
  • @mooglin That's completely incorrect. – john May 04 '20 at 20:16
  • You should be use `fscanf` to read from a file. Use `scanf` for reading from the console or text that was piped into your program. – Thomas Matthews May 04 '20 at 21:30
  • See also: [Disadvantages of scanf](https://stackoverflow.com/questions/2430303/disadvantages-of-scanf). – Thomas Matthews May 04 '20 at 21:32

0 Answers0