Reading integers from .txt file help! (Practice Question Inside)

I am pretty new to C++, only have been self taught, and am every eager to learn as much as possible. I have been struggling with this practice problem for days. This is the first time I am doing a problem that requires me to access an outside file so I've just recently discovered fstream. Here is the problem:

Congratulations! You’re getting married! So far the wedding planning has been going great but now you’ve reached the difficult task of creating the seating chart at the reception. It’s important that we have a system for placing guests or else grandma is going to be sitting at the kids table. 

Every guest will have a number that reflects the type of individual they are.

Input:

Your program should read from a file called guestnums.txt. The first line of the file will tell you how many different sets of guests there are. On the next line we begin working with the first set of guests. The first number is how many guests can sit at a table and the second number is the total amount of guests. The next line will be the numbers that represent the guests; these are separated by a single space. That’s the end of the first set’s information. The next set begins immediately after it. Eg.

2
4 18
17 3 9 16 10 11 14 5 8 2 4 12 1 6 15 7 21 13
6 20
48 40 47 44 32 38 41 46 35 31 34 33 37 49 45 39 30 36 42 43 23

Output:

We want to see how the seating arrangement would look if we applied these rules to them individually:

For every odd number at a table, there must be an even number at the same table

We can’t have a number that is divisible by 3 and a number NOT divisible by 3 sitting at the same table.

Not everyone will get a seat; if that’s the case list out how many people are without a seat. Also, it is possible that some poor sap may be sitting at a table by themself. The output for the input above would look something like this:

Group 1
Arrangement 1:
Table 1: 17 16 3 10
Table 2: 9 14 11 8
Table 3: 5 2 4 1
Table 4: 12 15 6 7
2 guests not seated

Arrangement 2:
Table 1: 3 9 12 6
Table 2: 15 21
Table 3: 17 16 10 11
Table 4: 14 5 8 2
Table 5: 4 1 7 13

Group 2
Arrangement 1:
Table 1: 48 47 40 41 44 35
Table 2: 32 31 38 33 46 37
Table 3: 34 49 45 30 39 36
Table 4: 42 43
1 guest not seated

Arrangement 2:
Table 1: 48 33 45 39 30 36
Table 2: 42
Table 3: 40 47 44 32 38 41
Table 4: 46 35 31 34 37 49
Table 5: 43


I have the .txt file with the same numbers the problem used. After doing tons of research into the ins and outs of fstream, I was able to come up with what you see below, which is just simply able to properly display the integers but whenever I try to use those numbers like the problem wants, it just becomes messy and broken and I am not sure the best way to tackle it. If someone could help walk me through this problem it would be greatly appreciated, my head is just hurting after trying all of these tutorials online and trying to apply them to my specific problem and seeing it become more complicated than I feel it needs to be.

(Sorry for such a long post!)

Thanks!

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
#include <iostream>
#include <fstream>

   
    using namespace std;
    
    int main () {
        string line;
        ifstream numbers;
        numbers.open("guestnums.txt");
        
        if (numbers.is_open())
        {
            while (! numbers.eof() )
            {
                getline (numbers,line);
                cout << line << endl;
            }
            numbers.close();
        }
        
        else cout << "Unable to open file"; 
        
        return 0;
    }
Well, in this example it might be useful to think of the ifstream as being very much like cin. If you used cin / cout, it might look like this:
1
2
3
4
5
6
7
8
    int numsets;
    cout << "How many different sets of guests are there: ";
    cin >> numsets;

    for (int i=0; i<numsets; i++)
    {
         // etc.
    }

When you use ifstream it is almost the same, except that you don't need the cout, and cin is replaced by numbers.
1
2
3
4
5
6
7
8
9
    ifstream numbers("guestnums.txt");
    int numsets;

    numbers >> numsets;

    for (int i=0; i<numsets; i++)
    {
         // etc.
    }


One other comment. It's usually not a good idea to use eof() in a while loop like this, while (! numbers.eof() ). If you're just starting out, it's best to stop using that now, rather than having to unlearn it later.

What's the alternative? Well, if you just wanted to read all the numbers from the file, you might do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    char filename[] = "guestnums.txt";
    ifstream numbers(filename);

    if (!numbers.is_open())
    {
        cout << "Unable to open file: " << filename << endl;
        return 1;
    }

    int num;
    int count = 0;

    while (numbers >> num)
    {
        cout << num << " ";
        count++;
    }
    cout << "\nFile contained " << count << " numbers" << endl;

and if you wanted to do a similar thing on a line by line basis, it might be done this way, making use of a stringstream to access the contents of each line just as though it was a file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    string line;

    while (getline(numbers, line))
    {
        int count = 0;
        int num;
        istringstream ss(line);

        while (ss >> num)
        {
            cout << num << " ";
            count++;
        }
        cout << "\nLine contained " << count << " numbers\n" << endl;
    }
Topic archived. No new replies allowed.