I modify this .cpp code into .c but it gives error:

the original code is:(runs fine)

#include <string>
#include <iostream>
#include <stdlib.h>


using namespace std;
int main()
{
	int c, s[10];
	int i, j;
	string name;



	cout << "Welcome to Airline Reservations System!\n";
	for (j=0; j<10; j++)
	{
		cout << "Please enter your name: ";
		getline(cin,name);

		cout << "Choose a class: ";
		cin >> c;

		switch(c)
		{
		case 1:
			cout << "First class" << endl;
			cout << "Seats available are 1,2,3,4,5.\n";
			do {
	    cout << "Pick a seat: ";
	    cin >> s[j];
	    for (i=0; i<j; i++) if (s[j]==s[i]) {cout << "Seat taken. ";
		break;}
	  } while (i!=j);
  	  if(s[j] <= 5)
		{

			cout << "\n";
			cout << "--------------------------\n";
		cout << "Name: " << name << endl;
		cout << "Class: " << "First class" << endl;
		cout << "Seat no.: " << s[j] << endl;
			cout << "--------------------------\n";

		}
		else
			cout << "Wrong number!  No seat for you!\n";
		break;
	case 2:
		cout << "Economic class\n";
		cout << "Seats available are 6,7,8,9,10.\n";
		do {
		cout << "Pick a seat number: ";
		cin >> s[j];
		for (i=0; i<j; i++) if (s[j]==s[i]) {cout << "Seat taken. ";
	break;}
  } while (i!=j);
		if(s[j] >= 6)
		{
			cout << "\n";
			cout << "--------------------------\n";
		cout << "Name: " << name << endl;
		cout << "Class: " << "Economic class" << endl;
		cout << "Seat no.: " << s[j] << endl;
			cout << "--------------------------\n";
		}
		else
			cout << "Wrong number!  No seat for you!\n";
		break;
		default:
				break;
	}
}


	system("pause");
	return 0;
}

But the converted code doesn't:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(void)
{
	int c;   /*untuk class*/
	int i;   /*untuk ulang compare s[i]==s[j]*/
	int j;   /*untuk ulang suruh pilih class*/
	char s[10]={'1','2','3','4','5','6','7','8','9','10'};   /*untuk seat*/

	printf("Welcome to Airline Reservations System!\n");

	for (j=0; j<10; j++)
	{
		printf("Choose 1 for First Class and 2 for Economic class\n");
		printf("Choose a class:\t");
		scanf("%d",&c);

		switch(c)
		{
		case 1:
			printf("First class\n");
			printf("Seats available are 1,2,3,4,5.\n");

			do
			{
			    printf("Pick a seat: ");
			    scanf("%s",s[j]);

			    for (i=0; i<j; i++)
                    if (s[j]==s[i])
                    {
                        printf("Seat taken.\n");
                        break;
                    }

            }
            while (i!=j);

                if(s[j] <= 5)
                {
                    printf("\n");
                    printf("--------------------------\n");
                    printf("Class: First class\n");
                    printf("Seat no : %s",s[j]);
                    printf("--------------------------\n");
                }

                else
                printf("Wrong number!  No seat for you!\n");

            break;

        case 2:

            printf("Economic class\n");
            printf("Seats available are 6,7,8,9,10.\n");

            do
            {
                printf("Pick a seat number: ");
                scanf("%s",s);

                for (i=0; i<j; i++)
                    if (s[j]==s[i])
                    {
                        printf("Seat taken.\n");
                        break;
                    }
            }

            while (i!=j);
                if(s[j] >= 6)
                {
                    printf("\n");
                    printf("--------------------------\n");
                    printf("Class: Economic class\n");
                    printf("Seat no: %s",s[j]);
                    printf("--------------------------\n");
		}
		else
			printf("Wrong number!  No seat for you!\n");
		break;


        default:
        break;
	}
}

	system("pause");
	return 0;
}

Somebody help me please. i'm a newby. just started learning c standard few weeks ago.

Recommended Answers

All 9 Replies

What error does it give?

it runs fine until i choose first/economical class.
After i input the seat number, the windows error dialog box appear:

AppName: airlinegddddedited.exe AppVer: 0.0.0.0 ModName: msvcrt.dll
ModVer: 7.0.2600.3085 Offset: 00034609


Any suggestion? I just learn until chapter 4(Arrays)...

After a day squeezing my brain, I finally solved it. Maybe my compiler or not, but the for scanf funtion for array %d,%c and %s cannot be used. I used %i put the s[j] outside the scanf and magically the code runs fine. Awkward..

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>

int main(void)
{
	int c,p;
	int i;
	int j;
	int s[10]={};

	printf("Welcome to Airline Reservations System!\n");

	for (j=0; j<10; j++)
	{
		printf("Choose 1 for First Class and 2 for Economic class\t");
		scanf("%i",&c);
		//system("cls");

		switch(c)
		{
		case 1:

			printf("\nFirst class\n");
			printf("Seats available are 1,2,3,4,5.\n");

			do
			{
                printf("Pick a seat:\n\n");
                scanf("%i",&p); //Here is the problem...
                s[j]=p;//I declared new equation..
                //system("cls");

			    for (i=0; i<j; i++)
			    {
                    if (s[j]==s[i])
                    {
                        printf("\n\nSeat taken.\n\n");
                        break;
                    }
			    }


            }
            while (i!=j);

                if(s[j] <= 5)
                {
                    printf("\n");
                    printf("--------------------------\n");
                    printf("Class: First class\n");
                    printf("Seat no : %i\n",s[j]);
                    printf("--------------------------\n\n");
                }

                else
                printf("\nWrong number!  No seat for you!\n\n");

            break;





        default:
                break;




        }//end switch case

    }//end kira orang*/

	return 0;
}

oh i forgot, i have deleted the case 2 for testing purpose. ooops.

I think people should help making code for other people. Nobody in the world can learn from nothing, right. We must have something that we can start or refer our homework to (such as the structure, control etc.). Even I search the entire internet for this code (but in c++), and convert it to c basic myself. We, as newbies feel like we don't have any hope in this c programming course. Maybe only geniuscan say otherwise.

You(not you, cscgal) want us to start making the code from scratch. We just learnt this thing!!! Not that I put my effort just for my grade, but I really have all my interest in programming. It's just that sometimes it is really difficult because nobody's gonna help us. And accusing us for being lazy.

I made a few changes;
Now i need to figure out how to add "Do you want to continue, Yes or No" that kind of thing after every successful seat number giving. And prevent from entering number bigger than 10.

#include<stdio.h>
#include<math.h> //just put here in case of error
#include<string.h>//i don't know where i use this...
#include<stdlib.h>//same here

int main(void)
{
	int c,p;
	int i;
	int j;
	int s[10]={};

	printf("Welcome to Airline Reservations System!\n");

	for (j=0; j<10; j++)
	{
		printf("Choose 1 for First Class and 2 for Economic class\t");
		scanf("%i",&c);
		//system("cls");

		switch(c)
		{
		case 1:

			printf("\nFirst class\n");
			printf("Seats available are 1,2,3,4,5.\n");

			do
			{
                printf("Pick a seat:\n\n");
                scanf("%i",&p);
                s[j]=p;
                //system("cls");

			    for (i=0; i<j; i++)
			    {
                    if (s[j]==s[i])
                    {
                        printf("\n\nSeat taken.\n\n");
                        break;
                    }
			    }


            }
            while (i!=j);

                if(s[j] <= 5)
                {
                    printf("\n");
                    printf("--------------------------\n");
                    printf("Class: First class\n");
                    printf("Seat no : %i\n",s[j]);
                    printf("--------------------------\n\n");
                }

                else
                printf("\nWrong number!  No seat for you!\n\n");

            break;


		case 2:

			printf("\nEconomical class\n");
			printf("Seats available are 6,7,8,9,10.\n");

			do
			{
                printf("Pick a seat:\n\n");
                scanf("%i",&p);
                s[j]=p;
                //system("cls");

			    for (i=0; i<j; i++)
			    {
                    if (s[j]==s[i])
                    {
                        printf("\n\nSeat taken.\n\n");
                        break;
                    }
			    }


            }
            while (i!=j);

                if(s[j] >= 6)
                {
                    printf("\n");
                    printf("--------------------------\n");
                    printf("Class: Economical class\n");
                    printf("Seat no : %i\n",s[j]);
                    printf("--------------------------\n\n");
                }

                else
                printf("\nWrong number!  No seat for you!\n\n");

            break;


        default:
                break;


        }//end switch case

    }//end counting people*/

	return 0;
}

if seats are taken in 2nd class, and there is no available seats the compiler would not exit so rather it should point the costumer to the 1st class.

Why there isn't any use of files????????

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.