Hey everyone! Well I'm sooo close to getting the names to align BUT there is 1 slot that is skipped in the non-smoking section and a few in the smoking section and i can't figure out why. The Data needs to be displayed in a 10 row x 3 column format (first 7 rows being non-smokers and last 3 rows being for smokers). Any help would be greatly appreciated! Thanks!

Here's what I've coded so far. (LINES 39-70 is what I need help with)

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
struct roster
{
    string last_name;
    string first_name;
    char smoking;
    int row;
    int seat;
};
    
int main()
{
    int x,y;
    roster manifest[41];
    
    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 >> manifest[x].last_name;
        info >> manifest[x].first_name;
        info >> manifest[x].smoking;
        info >> manifest[x].row;
        info >> manifest[x].seat;
    }
    info.close();  // finished with the file
    
    cout << "\t\t*******************************************\n"
         << "\t\t*           NON-SMOKING SECTION           *\n"
         << "\t\t*******************************************\n\n";
    
    for (x = 0, y = 0; x < 41; x++,y++)
    {
        if(manifest[x].smoking == 'N')
        {
        cout << "\t" << manifest[x].last_name << " " << manifest[x].first_name << "\t";
        if (y > 2) // used to add newline after 3rd name in row
        {
           cout << endl;
           y = 0; // resets the counter back to zero to start over
        }
        }
        
    }
    
    cout << "\n\n"
         << "\t\t*******************************************\n"
         << "\t\t*           SMOKING SECTION               *\n"
         << "\t\t*******************************************\n\n";
   
    for (x = 0, y = 0; x < 41; x++,y++)
    {
        if(manifest[x].smoking == 'S')
        {
        cout << "\t" << manifest[x].last_name << " " << manifest[x].first_name << "\t";
        if (y > 2)
        {
           cout << endl;
           y = 0;
        }
        }
        
    }
   cout << endl;
   system("PAUSE");
   return 0;
}

Here is what the text file I'm pulling the data from looks like (in case it helps):

SMITH JOHN N 3 2
JOBS STEVEN S 9 3
CAPONE AL N 3 2
JACKSON MICHAEL S 8 1
HUMPHREY H S 8 2
RINEHART JIM N 3 1
EASTMANN KEN N 1 1
WINSTON SALEM S 8 3
SMYTHE SUSAN S 9 1
KENDRICKS AL S 9 2
ALLISON DENNIS S 9 3
GREENBLATT RICHARD S 10 1
DAVIS BOB S 10 3
WOODS BILL S 10 2
BORAX M.T S 9 2
HENRY JOHN N 1 2
STEVASON E N 1 3
WILLIAMS KEN N 2 1
SMITH MARTHA N 2 2
KOCH JOE N 2 3
PACKARD H.P N 3 3
RINEHART JANE N 3 2
SWENSON CECIL N 4 1
CORY TROY N 4 2
BYES NICKOLINE N 4 3
BYES JENNIFER N 5 3
HARRIS JOHN N 5 2
HARRIS JUDY N 5 1
HARTMAN F.G N 6 1
SOUSE D.T N 6 2
JOHNSON MAGIC N 6 3
HAMPTON GEORGE N 7 1
THOMAS LINDA N 7 2
HAYWARD MARK N 7 3
SWIFT TOM N 8 1
DOBBS DR. N 8 2
GOLDEN VINNIE N 8 3
HACKER E N 5 3
RUSSEL STEVE N 5 2
CHAMPION SPARKY N 3 1
INNERMENTS TEX S 8 2

You're using a tab, use "setw"

setw sets a given number of spaces for the next item to be outputted.

Here's a small example:

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

int main()
{
    string firstColumn = "Name";
    int secondColumn = 21;
    float thirdColumn = 3.58;
    cout << setw(20) << firstColumn << setw(5) << secondColumn << setw(5) << thirdColumn << endl;
}

Assuming the person's name is less than 20 characters, other info less than 5, etc. It will be aligned correctly.

setw basically does this:
[Name ][21 ][3.58 ]

You can also specify alignment with other stream manipulators, and there are other stream manipulators for floating point stuff.

Setting the alignment to right would make "Name" appear on the right side of it's setw "field".

You may also want some basic error checking, at least to make sure the file actually opened.

info >> manifest[x].last_name;
        info >> manifest[x].first_name;
        info >> manifest[x].smoking;
        info >> manifest[x].row;
        info >> manifest[x].seat;

This is more of a personal preference, but if you're not making sure your stream failed to read each of these (individually), you can do this:

info >> manifest[x].last_name 
     >> manifest[x].first_name 
     >> manifest[x].smoking 
     >> manifest[x].row 
     >> manifest[x].seat;

>>It doesn't appear that you are actually outputting your information in a 10 row 3 column format, I really only see two columns and some unnecessary %3 logic.

It's a complete mystery to me what the third column should contain, so you'll have to decide that for yourself.

for (x = 0, y = 0; x < 41; x++,y++)
    {
        if(manifest[x].smoking == 'N')
        {
        cout << "\t" << manifest[x].last_name << " " << manifest[x].first_name << "\t";
        if (y > 2) // used to add newline after 3rd name in row
        {
           cout << endl;
           y = 0; // resets the counter back to zero to start over
        }
        }
        
    }

An approach I would take, is:

for(int i = 0; i < 41; i++){
    if(manifest[i].smoking == 'N')
        cout << setw(15) << manifest[i].last_name << setw(15) << manifest[i].first_name << /* Some other field here. */ << endl;
}

Also you have done a very good job so far, and your comments are good too.

Hi,

if I'm correct, there are 41 entries in the file.
41/3 = 13.66666666666666
I think, that can't be aligned.

Additionally, you have odd numbers of 'N' entries.

What's the problem...
What output do you like to have????
What is printed by your code???

Hi,

if I'm correct, there are 41 entries in the file.
41/3 = 13.66666666666666
I think, that can't be aligned.

Additionally, you have odd numbers of 'N' entries.

What's the problem...
What output do you like to have????
What is printed by your code???

Hi! I have attached a screenshot of my printed code/output. But yes, you are indeed correct. There are 41 entires in total. I am instructed to write a program that will fill a plane of 30 max passengers. The first 7 rows ( 7 x 3 = 21 seats) are for non-smokers and the last 3 rows (3 x 3 = 9 seats) are for smokers. The remaining passenegers (11 total) are to be added to a waiting list *single column output is okay* As you may have guessed, I'm clueless on how to tell the program to add passengers to the waiting list AFTER all available seats are filled.
Program rules:
1. All smoking and non-smoking requests MUST be honored. If no seats are available in the desired section, they will be put onto the waiting list.
2. If the desired seat is unavailable (ex: John Smith and Al Capone both want the same seat), then add them to the most forward seat available in the proper smoking/ non-smoking section. If there are no more seats available in the section, add them to waiting list.

Here is the code I used to get the screenshot provided:

#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct roster
{
    string last_name;
    string first_name;
    char smoking;
    int row;
    int seat;
};
    
int main()
{
    int x,y;
    roster manifest[41];
    
    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 >> manifest[x].last_name;
        info >> manifest[x].first_name;
        info >> manifest[x].smoking;
        info >> manifest[x].row;
        info >> manifest[x].seat;
    }
    info.close();  // finished with the file
    
    cout << "\t\t*******************************************\n"
         << "\t\t*           NON-SMOKING SECTION           *\n"
         << "\t\t*******************************************\n\n";
    
    for (x = 0, y = 0; x < 41; x++,y++)
    {
        if(manifest[x].smoking == 'N')
        {
        cout<< "\t" << manifest[x].last_name << " " << manifest[x].first_name << "\t";
        if (y > 2) // used to add newline after 3rd name in row
        {
           cout << endl;
           y = 0; // resets the counter back to zero to start over
        }
        }
        
    }
    
    
    cout << "\n\n"
         << "\t\t*******************************************\n"
         << "\t\t*           SMOKING SECTION               *\n"
         << "\t\t*******************************************\n\n";
   
    for (x = 0, y = 0; x < 41; x++,y++)
    {
        if(manifest[x].smoking == 'S')
        {
        cout << "\t" << manifest[x].last_name << " " << manifest[x].first_name << "\t";
        if (y > 2)
        {
           cout << endl;
           y = 0;
        }
        }
        
    }
   cout << endl;
   system("PAUSE");
   return 0;
}

As I've already told you, using setw will fix your output problems.

You're using a tab, use "setw"

setw sets a given number of spaces for the next item to be outputted.

Here's a small example:

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

int main()
{
    string firstColumn = "Name";
    int secondColumn = 21;
    float thirdColumn = 3.58;
    cout << setw(20) << firstColumn << setw(5) << secondColumn << setw(5) << thirdColumn << endl;
}

Assuming the person's name is less than 20 characters, other info less than 5, etc. It will be aligned correctly.

setw basically does this:
[Name ][21 ][3.58 ]

You can also specify alignment with other stream manipulators, and there are other stream manipulators for floating point stuff.

Setting the alignment to right would make "Name" appear on the right side of it's setw "field".

You may also want some basic error checking, at least to make sure the file actually opened.


This is more of a personal preference, but if you're not making sure your stream failed to read each of these (individually), you can do this:

info >> manifest[x].last_name 
     >> manifest[x].first_name 
     >> manifest[x].smoking 
     >> manifest[x].row 
     >> manifest[x].seat;

>>It doesn't appear that you are actually outputting your information in a 10 row 3 column format, I really only see two columns and some unnecessary %3 logic.

It's a complete mystery to me what the third column should contain, so you'll have to decide that for yourself.

An approach I would take, is:

for(int i = 0; i < 41; i++){
    if(manifest[i].smoking == 'N')
        cout << setw(15) << manifest[i].last_name << setw(15) << manifest[i].first_name << /* Some other field here. */ << endl;
}

Also you have done a very good job so far, and your comments are good too.

Hey, thanks for your feedback. I attempted to use the setw in place of the tabs but I'm still having alignment issues.

Here's what my code looks like and attached is a screenshot of what my printed output looks like:

cout << "\t\t*******************************************\n"
         << "\t\t*           NON-SMOKING SECTION           *\n"
         << "\t\t*******************************************\n\n";
    
    for (x = 0, y = 0; x < 41; x++,y++)
    {
        if(manifest[x].smoking == 'N')
        {
        cout << "\t" << manifest[x].last_name << " " << manifest[x].first_name << left << setw(10);
        if (y > 2) // used to add newline after 3rd name in row
        {
           cout << endl;
           y = 0; // resets the counter back to zero to start over
        }
        }
        
    }

You have to use setw() immediately before the output you want in the "column".

//example:
//outputStream  << setw(width1) << data1 << setw(width2) << data2 ...
std::cout << setw(10) << data10 << setw(20) << data20 << //...

You are using it after all of your output has already occurred. In that location, it affects the width of the first column of your next output line.

The rules which you have mentioned above doesn't exists in your code. I have a clue for you but lake of my time. I will try to cover it for you.
First if you make the program much complicated then you will face a problem with the
valid insertion. You should either mention the seats for Smoker and non-Smoker and prompt for input or define a function to check whether the value inserted in valid or not.
Coming to your code, you can set each counter for the either situations of Smokers and non-Smokers respectively. After assigning a seat increment the counter and you will have available seats until the counter reaches to the desired sets. Then you can convert the remainings to waiting list. In this situation the last will always get to the waiting list. While with the others it will be a first come first serve service, who ever will request for a seat first will get it. You can apply a check on the seat no: while comparing it with the counter if the counter didn't exceeded that value then the seat is available else it is reserved and the customer will be assigned the available.

Hope That Helped!
If further help is needed i will be right there!

The rules which you have mentioned above doesn't exists in your code. I have a clue for you but lake of my time. I will try to cover it for you.
First if you make the program much complicated then you will face a problem with the
valid insertion. You should either mention the seats for Smoker and non-Smoker and prompt for input or define a function to check whether the value inserted in valid or not.
Coming to your code, you can set each counter for the either situations of Smokers and non-Smokers respectively. After assigning a seat increment the counter and you will have available seats until the counter reaches to the desired sets. Then you can convert the remainings to waiting list. In this situation the last will always get to the waiting list. While with the others it will be a first come first serve service, who ever will request for a seat first will get it. You can apply a check on the seat no: while comparing it with the counter if the counter didn't exceeded that value then the seat is available else it is reserved and the customer will be assigned the available.

Hope That Helped!
If further help is needed i will be right there!

Alright, so I've been working on this program for the last 6 hours to try and incorporate the program rules and attached is a screenshot of as close as I've to getting it. Here's the code I used. I'm sure there's a much simpler way to do this, but I'm a programmign newbie so bare with me. =/

Oh, and by the way. I used 2 structure arrays to do this. The first (manifest) has all the passenger data and the second (seating_chart) was initialed to default values.

// for loop to process all 41 data enties
                     for (x = 0, y = 0; x < 41; x++) 
                     {
                        // the if statement contains mulitple arugments. They are
                        // 1 & 2. if the correspnding [x].row / [x].seat are both 0 then 
                        //        set seating_chart[x].[y] the same as manifest[x].[y]
                        // 3. if a Non-smoking passenger requested a seat in the smoking
                        //    section, put him in the next available non-smoking seat
                        //    OR on the waiting list if seats are filled (in other if loop) 
                        // 4. manifest[x].smoking verifies that the passenger is a Non-Smoker
                        if((seating_chart[x].row == 0) && (seating_chart[x].seat == 0) && (manifest[x].row < 8) && (manifest[x].smoking == 'N') && (y < 21)) 
                        {
                           // if loop to assign seat if it is available
                           //if (manifest[x].smoking == 'N')
                           //{
                           seating_chart[y].lName = manifest[x].lName;
                           seating_chart[y].fName = manifest[x].fName;
                           seating_chart[y].smoking = manifest[x].smoking;
                           seating_chart[y].row = manifest[x].row;
                           seating_chart[y].seat = manifest[x].seat;
                           y++;
                        }
                               
                        //else statement to assign next available seat if the
                        // desired seat was already occupied
                        else if(((y > 21) || (manifest[x].row > 7)) && (manifest[x].smoking != 'S'))
                        {
                           seating_chart[y].lName = manifest[x].lName;
                           seating_chart[y].fName = manifest[x].fName;
                           seating_chart[y].smoking = manifest[x].smoking;
                           seating_chart[y].row = manifest[x].row;
                           seating_chart[y].seat = manifest[x].seat;
                           y++;
                        }
                     
                        // COMMENTED OUT BECAUSE SORTING WONT WORK WITH THIS IF STATEMENT
                        /*else if((manifest[x].smoking == 'S') && (seating_chart[x].row == 0) && (seating_chart[x].seat == 0) && (y<30))
                        {
                           seating_chart[y].lName = manifest[x].lName;
                           seating_chart[y].fName = manifest[x].fName;
                           seating_chart[y].smoking = manifest[x].smoking;
                           seating_chart[y].row = manifest[x].row;
                           seating_chart[y].seat = manifest[x].seat;
                           y++;
                        }*/
                     
                     }
                     
                     cout << "\t\t*******************************************\n"
                          << "\t\t*           NON-SMOKING SECTION           *\n"
                          << "\t\t*******************************************\n\n";
                     for (x = 0, y = 0; seating_chart[x].seat < 21; x++)
                     {
                         cout << "\t" << seating_chart[x].lName;
                         cout << " " << seating_chart[x].fName << "\t";
                         y++;
                         if (y > 2) // used to add newline after 3rd name in row
                         {
                            cout << endl;
                            y = 0; // resets the counter back to zero to start over
                         }
                     }
                     
                     // COMMENTED OUT BECAUSE IT WONT WORK WITH THIS SECTION
                     /*cout << "\n\n"
                          << "\t\t*******************************************\n"
                          << "\t\t*           SMOKING SECTION               *\n"
                          << "\t\t*******************************************\n\n";
                     for (x = 21, y = 0; seating_chart[x].seat < 30; x++)
                     {
                        if ((seating_chart[x].smoking == 'Y') && (x < 30))
                        cout << "\t" << seating_chart[x].lName;
                        cout << " " << seating_chart[x].fName << "\t";
                        y++;
                        if (y > 2) // used to add newline after 3rd name in row
                        {
                           cout << endl;
                           y = 0; // resets the counter back to zero to start over
                        }
                     }*/
                     
                     cout << "\n\n\t\t*******************************************\n"
                          << "\t\t*            WAITING LIST                 *\n"
                          << "\t\t*******************************************\n\n";
                     cout << "\t" << "Last Name First Name\t"
                          << "Smoking/Non-Smoking\t"
                          << "Row\tSeat\n\n";
                     for (x = 21; x < 29; x++)
                     {
                         cout << "\t" << seating_chart[x].lName << " "
                              << seating_chart[x].fName << "\t\t\t"
                              << seating_chart[x].smoking << "\t\t "
                              << seating_chart[x].row << "\t "
                              << seating_chart[x].seat << "\n";                         
                     }
                     cout << endl;
                     system("PAUSE");
                     system("CLS");

The rules which you have mentioned above doesn't exists in your code. I have a clue for you but lake of my time. I will try to cover it for you.
First if you make the program much complicated then you will face a problem with the
valid insertion. You should either mention the seats for Smoker and non-Smoker and prompt for input or define a function to check whether the value inserted in valid or not.
Coming to your code, you can set each counter for the either situations of Smokers and non-Smokers respectively. After assigning a seat increment the counter and you will have available seats until the counter reaches to the desired sets. Then you can convert the remainings to waiting list. In this situation the last will always get to the waiting list. While with the others it will be a first come first serve service, who ever will request for a seat first will get it. You can apply a check on the seat no: while comparing it with the counter if the counter didn't exceeded that value then the seat is available else it is reserved and the customer will be assigned the available.

Hope That Helped!
If further help is needed i will be right there!

Forgot to include attatchment! Here it is!

The above code which you have posted makes sense. Making your program more realistic -- create two arrays, both of them of two dimensions, first for non-smokers[7][3] and second for smoker[3][3]. Initialize both of them with '0'. Turn the passengers to their respective arrays. Just have a simple check if smokes then smoker[][] if don't then non-smoker[][]. As you have prompted for the row and seat from the passenger -- check that if non-smoker then do, if(non-smoker[row][seat] == 0)? As you have initialized them with '0' so if it's '0' then the seat is available and you can assign a value "non-smoker[row][seat] = 1" showing that the seat is reserved now.
You can include another member in structure, i.e. seatstatus. If a passenger gets a seat then turn his status to 1, showing that he has got the seat. Using seatStatus will help you to identify those passenger whom didn't got the seats - which can then easily be included to the waiting list.

was that helpful?

The above code which you have posted makes sense. Making your program more realistic -- create two arrays, both of them of two dimensions, first for non-smokers[7][3] and second for smoker[3][3]. Initialize both of them with '0'. Turn the passengers to their respective arrays. Just have a simple check if smokes then smoker[][] if don't then non-smoker[][]. As you have prompted for the row and seat from the passenger -- check that if non-smoker then do, if(non-smoker[row][seat] == 0)? As you have initialized them with '0' so if it's '0' then the seat is available and you can assign a value "non-smoker[row][seat] = 1" showing that the seat is reserved now.
You can include another member in structer, i.e. seatstatus. If a passenger gets a seat then turn his status to 1, showing that he has got the seat. Using seatStatus will help you to identify those passenger whom didn't got the seats - which can then easily be included to the waiting list.

was that helpful?

I would prefer using class instead of structure. Here is an example of your program.

/*
 * src.cpp
 *
 *  Created on: Jun 7, 2011
 *      Author: Mafia
 */

#include <iostream>
#include <iomanip>

using namespace std;

class Roster
{

public:
	Roster();
	void getinfo();
	void assignSeat();
	void waitingList();
private:
	string last_name;
	string first_name;
	int seatStatus;
	char smoking;
	int row;
	int seat;
};

	int smokersSeats[3][3];
	int nonSmokersSeats[7][3];
Roster::Roster(){
	row = seat = seatStatus = 0;
	last_name = first_name = ' ';
	smoking = ' ';
	int i, j;

	for(i = 0; i < 7; i++){
		for(j = 0; j < 3; j++){
			nonSmokersSeats[i][j] = 0;
		}
	}

	for(i = 0; i < 3; i++){
			for(j = 0; j < 3; j++){
				smokersSeats[i][j] = 0;
			}
		}
}

void Roster::getinfo(){

	cout << "Enter First Name :" << endl;
	cin >> first_name;
	cout << "Enter Last Name :" << endl;
	cin >> last_name;
	cout << "Do you smoke? [Y|N] :" << endl;
	cin >> smoking;
	cout << "Enter the row :" << endl;
	cin >> row;
	cout << "Enter seat :" << endl;
	cin >> seat;
	}

void Roster::assignSeat(){
		if(nonSmokersSeats[row][seat] == 0){
			nonSmokersSeats[row][seat] = 1;
			seatStatus = 1; // Say 1 indicates that seat is allocated!
		}
		else
			seatStatus = 0; // Say 0 indicates that seat not allocated!
	}

void Roster::waitingList(){
	if(seatStatus == 0){
		cout << first_name << ' ' << last_name << setw(10) << smoking << endl;
	}
}
int main()
{
	Roster passenger[2];
	int i;

	for(i = 0; i < 2; i++){
		passenger[i].getinfo();
	}

	for(i = 0; i < 2; i++){
		passenger[i].assignSeat();
	}

	cout << setw(17) << "WAITING LIST" << endl
			<< "NAME" << setw(20) << "SMOKING" << endl;
	for(i = 0; i < 2; i++){
		passenger[i].waitingList();
	}

	return 0;
}

I have tried just for 2 and 4 passengers. Works fine! Try it. Try to reserve the same seat for more than one passenger and you will see that the next person will be in the waiting list. You can print those pessengers also which have got seats and try another stuff too. You can make the program more complicated.

If my help or suggestion is needed on any instance, i will be ready to help.

HAPPY PROGRAMMING!:)

Most programmers would use a matrix instead of two arrays, but w/e. bool seatReservations[64][64] = {false}; Etc.

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.