Quick c++ Help

Associate
Joined
4 Dec 2007
Posts
103
Hey Everyone..

Right, Doing a little Tutorial for UNI, only just started c++. I have to create an Array that basically shows the Free and Taken seats in a coach.

Right, so F would indicate Free and N would indicate Not free. so far i've gotten:

char p[3];



p[0] = "F";


p[1] = "F";


p[2] = "N";

cout<< p;


Thing is, its having none of it in compiling. I tried it with INT's, it worked, but the numbers were displayed in HEX and i couldt get my head around it.

Now i tried to create 2 arrays so it would show

Seat Avail

1 F
2 F
3 N

But all the online books i been reading have many different formats in what to include which i dont have etc.

How do i go about doing it :(

Michael
 
You need to use single quotes instead of double quotes:

Code:
char p[3];

p[0] = 'F';
p[1] = 'F';
p[2] = 'N';

Double quotes are used for constructing strings, and they return a pointer to a sequence of characters (char*) in memory.

This doesn't work because you're trying to put a character pointer in an array of characters.

Single quotes, however, give you just one character (i.e. a char value), so this can be put in the array :)

Also, if you're going to display a manually-constructed array/buffer of characters as a string, you also need to null-terminate it by putting the null character on the end:
Code:
char p[4];

p[0] = 'F';
p[1] = 'F';
p[2] = 'N';

p[3] = '\0';

cout << p;

Since a string is just a sequence of characters in memory, there needs to be something to indicate where it actually finishes; this is what the null character does.
 
Last edited:
Wooo it worked, Thanks man. I rustled around with it again and i got it to work it was only the single quotation i wasn't including :(

This is what i got so far..

//My array being set up

char Seats[10] = {'F', 'F', 'F', 'N', 'N', 'F', 'N', 'F', 'F', 'N'};


cout<<"Legend: F = FREE SEAT N=NOT FREE\n\n";

cout <<"\nSeat" <<" Free?\n\n";


for (int i = 1; i < 10; i++)

cout<< i <<" ---- "<< Seats <<endl <<endl;

Now... Is it possible, when my program is running, to lets say, change a F to a N and Vice Versa. Would the way i've written the array allow me to be able to change the contents in Seats in the program to F or N. So lets say i make a booking on the program, then the user wants to seat in a Free seat .. Seat 1... Can i change it to show N then?
 
You can certainly update the array to reflect the change – you just have to set the corresponding element to 'F' or 'N':

Code:
// Swap user from seat 2 to seat 4.
seats[2] = 'F';
seats[4] = 'N';

However, you'd then have to display the table again.

Also, i should be starting at 0 rather than 1 in your for loop.
 
I have i starting at 1 because i cant book seat 0 lol

I don't get your code though... I got this when run..



Like is there a way , like i have a method to book seats, there a way i could
basically show the seats then i get a cin>> prompt to choose which seat you want to book (Choose a F) one and then re-display the array and show the F has now been changed to N.
 
I have i starting at 1 because i cant book seat 0 lol

Like is there a way , like i have a method to book seats, there a way i could
basically show the seats then i get a cin>> prompt to choose which seat you want to book (Choose a F) one and then re-display the array and show the F has now been changed to N.

It doesn't stop the first element in your array having index position 0 though!

To change a seat after setup, get the seat numer you want from input. Check what the value is at present, then act accordingly. You can change the values in your array as Inquisitor showed you.
 
Thanks, ill give it a go now, ill change the increment for the i++ to ++i too. Ill see how i can tackle it :)

**edit** Im a bit confused on the go ahead on doing it :$
 
Last edited:
Ok im a failure, it's not having none of it :|

This is what i've got at the moment :

cout<<"\n\n\n\nWelcome to Checking the booking...\n";
cout<<"----------------------------------\n\n\n\n";

//My array being set up with Free and Not free Values

char Seats[10] = {'F', 'F', 'F', 'N', 'N', 'F', 'N', 'F', 'F', 'N'};


cout<<"Legend: F = FREE SEAT N=NOT FREE\n\n";

cout <<"\nSeat" <<" Free?\n\n";


for (int i = 1; i < 10; i++)

cout<< i <<" ----- "<< Seats <<"\n\n";


}

What i tried to do was, see if i could Change the value. I suggest it would be something like " If seat[?] selected then change to Y else N." right? But how do i do the code to which ever the user enters as the seat to change :( . 5 hours left till submission :(
 
Last edited:
This might help you see how to evaluate inputs to your program. Is a bit rough but i hope it shows you enough to get started

Code:
	char Seats[5] = {'N','N','F','N','F'};
	int option = 0;
	int seat;
	while (option != -1)
	{
		option = 0;
		cout << "Enter an option (-1 to quit)" << endl;
		cin >> option;

		switch (option)
		{
		case 1: /*Book a seat*/
				   seat = 0;
				   cout << "Book which seat " << endl;
				   cin >> seat;
				   if (Seats[seat] == 'F')
				   {
						Seats[seat] = 'N'; //Seat now booked
						cout << "That seat is now booked!" << endl;
				   }
				   else
				   {
					   cout << "Sorry that seat is already booked!" << endl;
				   }
				   break;
		case 2: /*print current seat layout */ break;

		case -1: break;
		}
	}
	return 0;
 
Oh I misread what you asked for aswell :/ Thought you were asking for how to evaluate inputs from a user not how to evaluate if a seat is free or not :p Hope it was helpful anyway, let us know if you get stuck again.
 
Back
Top Bottom