Hello Everyone! So I have an assignment to create a program to read in data from text file. I managed to get that part, but I'm having trouble with two areas.
1. Having the names read from the display read into a 10x3 format (10 rows, 3 colums/seats.)
Expected output:
Capone AL Eastman Ken Henry John
Stevenson E Williams Ken Smith Martha
Etc...

I can't seem to write a for loops that will go to the nextline after the 3rd name without the program running on endlessly.

My output:
Capone Al
Eastman Ken
Henry John
Stevenson E
Etc...


Here's what I have so far (I've bolded the area I need help with):

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string last_name[41];
    string first_name[41];
    char smoking[41];
    int row[41];
    int seat[41];
    int x;
    int y;
    
    char choice;
    while((choice != 'Q') && (choice != 'q'))
    {
        ifstream info; // ifstream variable declaration
                         
        // opens text file where data is stored
        info.open("ffmanifest.txt");   
        
        // for loop to read in data from text file
        for (x = 0; x < 41; x++)       
        {
           info >> last_name[x];
           info >> first_name[x];
           info >> smoking[x];
           info >> row[x];
           info >> seat[x];
        }
    
         info.close();  // finished with the file
          
          cout << "\t\t*** WELCOME TO THE FAST FLIGHT AIRLINES MENU! ***\n\n"
               << "Please select from one of the following options:\n\n"
               << "\ta) View Standard List\n"
               << "\tb) Book New Flight\n"
               << "\tq) Quit\n\n"
               << "\tYour Selection: ___\b\b";
               
          cin >> choice;
          
          
          
          switch(choice)
          {
               case 'A':
               case 'a': system("CLS");
                         cout << endl;
                         cout << "\t\t*******************************************\n"
                              << "\t\t*           NON-SMOKING SECTION           *\n"
                              << "\t\t*******************************************\n\n";
                         
                        for (x = 0; x < 41; x++)
                         {
                            if (smoking[x] == 'N' || smoking[x] == 'n')
                            {
                            cout << "\t" << last_name[x] << " " << first_name[x] << endl;
                            }
                         }
                         cout << endl;
                         system("PAUSE");
                         system("CLS");
                         break;
               
               case 'B':
               case 'b': char fName[20];
                         char lName[20];
                         char smoker;
                         int seat;
                         int row;
                         system("CLS");
                         cout << "\nEnter Passenger's First Name: ";
                         cin >> fName;
                         cout << "Enter Passenger's Last Name: ";
                         cin >> lName;
                         cout << "(S)moking or (N)on-Smoking?: ";
                         cin >> smoker;
                         cout << "Preferred Seat: ";
                         cin >> seat;
                         cout << "Preferred Row: ";
                         cin >> row;
               
                         cout << "\n\nHere is your Fast Flight Information:\n"
                              << lName << " " << fName << "\t" << smoker
                              << "\t" << seat << "\t" << row << "\n\n";
                         system("PAUSE");
                         system("CLS");
                         break;
               
               case 'Q':
               case 'q': cout << endl << "GOODBYE! =)\n\n";
                         //system("PAUSE");
                         break;                
               
               default:  cout << endl << "\"" << choice << "\""
                              << " is not a valid choice, please choose "
                              << "from one of the options shown.\n\n";
                         system("PAUSE");
                         system("CLS");
                         
          }
    }                
    //system("PAUSE");
    return 0;
}

Recommended Answers

All 8 Replies

You probably need to provide more details about your problem, such as an example of the file that you're trying to read in, and maybe some code that you've written yourself to try and solve the problem. Also, you should probably explain exactly how nested for loops are involved here?

You probably need to provide more details about your problem, such as an example of the file that you're trying to read in, and maybe some code that you've written yourself to try and solve the problem. Also, you should probably explain exactly how nested for loops are involved here?

Yeah sorry, I just updated it. I accidently submitted it before I finished typing.

You should use code tags when posting code.

OK, it looks like your program is trying to read a file with a format that looks like:

Stan Smith N 12 4
Francine Smith Y 12 3
Steve Smith N 12 2

This not what the question is asking; it says "Having the names read from the display read into a 10x3 format (10 rows, 3 colums/seats.)" is that correct? If it's in the format shown above, then it should be working OK as long as the file is exactly like that.

Also, when you open a file, you should always check that it opened OK, so something like:

std::ifstream inputFile;
inputFile.open( "myFilename.txt", std::ios::in );
if ( inputFile.is_open() == false ){
   std::cerr << "Error! Failed to open file for input. Exiting." << std::endl;
   return 1;
}

/* Now continue with the rest of your program */

It might be a good idea to make a struct or class to store the data about each booking in, rather than a bunch of arrays. Then you can make an array of these structs to stre all the booking in. So, something like:

struct Booking{

   std::string m_firstName;
   std::string m_lastName;
   bool m_smoking;
   int m_row;
   int m_seat;
};

int main()
{
   const unsigned maxNumberOfBookings;
   Booking allBookings[ maxNumberOfBookings ];

   Booking newBooking;
   newBooking.m_firstName = "Haley";
   newBooking.m_secondName = "Smith";
   newBooking.m_smoking = false;
   newBooking.m_row = 12;
   newBooking.m_seat = 1;

   allBookings[0] = newBooking;
   return 0;
}

Thanks for replying Ravenous, I really appreciate your help. Ok so to answer your question, Yes the data file is in the format of

Smith John N 3 2
Jobs Steven S 9 3
Capone Al N 3 2
Jackson Michael S 8 1

with 41 total names.
Where I'm having trouble is the expected output is supposed to have 3 names across 10 rows down with the first 7 rows being for non-smokers, and last 3 rows being for smokers. (11 on waiting list but I'll figure that out after)

for (x = 0; x < 41; x++)
      {
         if (smoking[x] == 'N')
         {
            cout << "\t" << last_name[x] << " " << "first_name[x] << endl;
         }
      }

With the above code, I only get 1 name across, not 3. I've tried to embed a second for loop to somehow insert a newline after the 3rd name of every row but my program always ends up running on forever.

Here's an example of one of my failed attempts:

for (x = 0; x < 41; x++)
      {
         for (y = 0; y > 21; y+=3) // 21 because 7 rows x 3 = 21 seats
         {
            if (smoking[x] == 'N')
            {
               cout << "\t" << last_name[x] << " " << first_name[x] << "\t";
            }
            cout << endl; // FAILED ATTEMPT to add newline after 3 names accross
         }
      }

IF you want to have 3 colums with 10 rows each then you are going to need 2 loops. One loop controls what row you are on and the other loop will control what colomn you are in. Look at this loop

string foo[41]
for (int i = 0; i < rows; i++)
{
    cout << foo[i];
}

This loop would only print out just the rows. So now we have to put another loop inside the row loop to print the seperate colums.

for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < cols; j++)
    {
        cout << foo[...]
    }
}

I left then index blank for you to figure out. If you go through it step by step you should figure out how to use i and j to get the right value. Look at what index starts each row. In this case it would be 0,3,6,9,12... and the ending index of each row is 2,5,8,11,14...

IF you want to have 3 colums with 10 rows each then you are going to need 2 loops. One loop controls what row you are on and the other loop will control what colomn you are in. Look at this loop

string foo[41]
for (int i = 0; i < rows; i++)
{
    cout << foo[i];
}

This loop would only print out just the rows. So now we have to put another loop inside the row loop to print the seperate colums.

for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < cols; j++)
    {
        cout << foo[...]
    }
}

I left then index blank for you to figure out. If you go through it step by step you should figure out how to use i and j to get the right value. Look at what index starts each row. In this case it would be 0,3,6,9,12... and the ending index of each row is 2,5,8,11,14...

Thanks, that help to clear some confusion. I managed to get the names to appear in a 10x3 row with this code.

for (row = 0,x = 0; row < 10; row++)
    {
        for (column = 0; column < 3; column++)
        {
           cout << "\t" << manifest[x].lName;
           cout << " " << manifest[x].fName;
           x++;
        }
        cout << endl;
    }

Now my next question is how do I get the for loops to display only the smokers/non-smokers? (29 non-smokers, 12 smokers) I tried using an if statement like this one but it didnt work

for (row = 0,x = 0; row < 10; row++)
    {
        for (column = 0; column < 3; column++)
        {
           if (manifest[x].smoking == 'N')
           {
           cout << "\t" << manifest[x].lName;
           cout << " " << manifest[x].fName;
           x++;
           }
        }
    cout << endl;
    }

You are incrementing the x value only inside the if block ... I think you need to increment the x irrespective of the fact if the person is a smoker or a non smoker

Also, your input data file already tells you which row and which seat each person is in. Since you have that information, you might want to create a new array of seats, where each person is placed in the correct position:

Person *seats[10][3];
// TODO: initialize to NULL values

for (int x = 0; x < 41; x++) {
    row = manifest[x].row;
    seat = manifest[x].seat;
    if (seats[row][seat] != NULL)
        cout << "somebody already sitting in row " << row << ", seat " << seat << endl;
    else
        seats[row][seat] = &manifest[x];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.