Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <cmath>
#include<string.h>
#include <stdlib.h> 
#include<time.h>
#include <fstream> 
#include<list>
using namespace std;

#define NO_OF_ROWS	20

#define NO_OF_COLUMNS	6


main(void)
{
	unsigned int r,c,Class,DesiredSeat,n=1,Age,PR=0,SP=0,OfficeWork=0;

	unsigned int SeatsNo[NO_OF_ROWS][NO_OF_COLUMNS],FlagAge=0;

	char Sex;

	unsigned int SeatingPlan[NO_OF_ROWS][NO_OF_COLUMNS] = {{0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0},

													    {0, 0, 0, 0, 0, 0}

														 };

	

	cout<<"Seating Nos.\n";

	n=1;

	for(r=0;r<NO_OF_ROWS;r++)
	{
		cout<<"Row-"<<(r+1);

		for(c=0;c<NO_OF_COLUMNS;c++)
		{

			cout<<"\t"<<n;

			n=n+1;

		}

		if(r<=5) cout<<"\tFirst Class";

		if(r>=6) cout<<"\tEconomy Class";

		cout<<"\n";

	}
	
	while(1)

	{

	FlagAge=1;	

	cout<<"\n\n";

	cout<<"Seats Availability\n";

	for(r=0;r<NO_OF_ROWS;r++)
	{

		cout<<"Row-"<<(r+1);

		for(c=0;c<NO_OF_COLUMNS;c++)

		{

			if(SeatingPlan[r][c] == 1) cout<<"\tX";

			else					  cout<<"\t*"; 	

		}

		if(r<=5) cout<<"\tFirst Class";

		if(r>=6) cout<<"\tEconomy Class";

		cout<<"\n";

	}

	cout<<"\n\n Enter 0-> To EXIT..."; 

	cout<<"\n\n Enter 1-> First Class"; 

//	cout<<"\n  Enter 2-> Business Class"; 

	cout<<"\n Enter 3-> Economy Class\n"; 

	cout<<"\n\nEnter the Class: "; 	cin>>Class;

	if(Class == 0) exit(0);

	

	cout<<"\n Enter the Gender M/F: "; cin>>Sex;

	if(Sex == 'F' || Sex == 'f')
	{

		cout<<"\nEnter the Age = "; cin>>Age;	

		if(Age<35) FlagAge=0;

	}

	else 

	FlagAge=1;
	
	cout<<"\nIf the travel is for Official Work: Press 1 for YES and 0 for NO: "; cin>>OfficeWork;

	cout<<"\nIf the traveller has Permanant Resisdency Certificate? Press 1 for YES and 0 for NO: "; cin>>PR;

	cout<<"\nIf the traveller has Special Permission? Press 1 for YES and 0 for NO: "; cin>>SP;

	

	if(FlagAge && (OfficeWork || PR || SP))

	{

	

	if(Class==1) cout<<"\n\nDesired Seat (1 - 30) = "; 

	if(Class==3) cout<<"\n\nDesired Seat (31 - 120) = "; 

	cin>>DesiredSeat;

	

	while(Class==1 && DesiredSeat>30)

	{

		cout<<"\nInvalid Desired Seat";

		cout<<"\n\nDesired Seat (1 - 30) = "; 

		cin>>DesiredSeat;

	}

	
	while(Class==3 && (DesiredSeat < 31 || DesiredSeat>120))

	{

		cout<<"\nInvalid Desired Seat";

		cout<<"\n\nDesired Seat (31 - 120) = "; 

		cin>>DesiredSeat;

	}

	

	if(Class == 0) exit(0);

	

	r = DesiredSeat/NO_OF_COLUMNS;

	c = (DesiredSeat%NO_OF_COLUMNS)-1;

	

	if(SeatingPlan[r][c] == 0) 	SeatingPlan[r][c] = 1;

	else						cout<<"\n\nSeat No. "<<DesiredSeat<<" is not Available";

	}

	else

	{

		cout<<"\n\nTHE TRAVELLING IS NOT ALLOWED.\n\n";

	}
}
}


What I have tried:

I've tried it into C Programming but I couldn't solve
Posted
Updated 22-Apr-22 3:26am
v2
Comments
Richard MacCutchan 9-Jun-20 4:33am    
Apart from the use of cin and cout that is already C code.

This should be pretty easy. Remove all of the headers except for stdio.h and remove the using statement.

"cout<<" needs to be translated into printf calls. You need to read the documentation on how to use it because it is pointless for me to reproduce it here.

"cin>>" needs to be translated into a function to read a string. gets is one candidate. Then that text string needs to be converted into the data type needed. The function atoi takes a text string and returns an integer. You might want to make a function that will prompt for a text string and return an integer.
 
Share this answer
 
The same as with everything else. You do this by completely understanding the C++ code and writing equivalent code in C.

But, since C is a subset of C++, this will be no easy task. There will not be a direct conversion of code and there is no tool that will do it for you.
 
Share this answer
 
Comments
merano99 22-Apr-22 12:53pm    
In this particular case, it's an easy task. Almost no feature of C++ is used. What the many C++ includes are there for is not clear. There was someone at work who didn't exactly know what he was doing.
Dave Kreskowiak 22-Apr-22 13:35pm    
This is yet another "student" grabbing code off the web hoping to turn it in as their own work after "C-ifying" it.
The program is already more C than C++, so it shouldn't cause any problems. It would probably be much more difficult to make real C++ out of it ;-)
Commenting out the C++ headers and the namespace directive and renaming the file from cpp to c should be very easy.
C
// #include <iostream>
// #include <cmath>
// #include <fstream> 
// #include<list>
// using namespace std;

e.g.
C
// cout << "Seating Nos.\n";
printf("Seating Nos.\n");

// cout << "Row-" << (r + 1);
printf("Row-%d", (r + 1));

// cout << "\n\nEnter the Class: "; 	
// cin >> Class;
printf("\n\nEnter the Class:");
scanf("%d", &Class);
 
Share this answer
 
v2
Comments
Dave Kreskowiak 22-Apr-22 10:21am    
Questions like this are more about someone trying to take code they found on the web and convert it to some language they need for their class, bypassing the work they really need to be doing to actually learn something.

Doing their work for them doesn't help them.
merano99 22-Apr-22 12:48pm    
I am of course aware of this fact. However, a solution can also be used for learning. Anyone who tries to get along with copy & paste is neither willing to learn anything nor has they understood anything.
A turnkey solution that can be handed over directly without any personal contribution would really not make sense here. I agree.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900