Question

convert this program from java to c++ import java.util.Scanner; class Main { public static void main(String[]...

convert this program from java to c++

import java.util.Scanner;

class Main

{

public static void main(String[] args)

{

Scanner sc=new Scanner(System.in);

String name,seat,fcls;

int st,i;

String price;

int ch1;

char ch;

seat="";

String time[]={"7.00","9.00","11.00","13.00","15.00"};

String arrive[]={"9.30","11.30","13.30","15.30","17.30"};

int cp[]=new int[5];

do{

System.out.println("Welcome to CSO1511 Flight Booking System");

System.out.println("Enter full name?");

name=sc.nextLine();

System.out.println("The available travel times for flights are:");

System.out.println(" Depart \t Arrive");

System.out.println("1. 7.00 \t\t 9.30");

System.out.println("2. 9.00 \t\t 11.30");

System.out.println("3. 11.00 \t\t 13.30");

System.out.println("4. 13.00 \t\t 15.30");

System.out.println("5. 15.00 \t\t 17.30");

do

{

System.out.println("Choose the time by entering the option number from the displayed list:");

st=sc.nextInt();

if(st>5 || st<1)

System.out.println("Incorrect option! Please Choose from 1-5.");

}while(st>5 || st<1);

cp[st-1]=cp[st-1]+1;

System.out.println("The available seats for "+time[st-1]+" are as follows");

System.out.println("First Class(1920.00)");

System.out.println("|A1|A2|A3|---- |A4|A5|A6|");

System.out.println("|B1|B2|B3|---- |B4|B5|B6|");

System.out.println("|C1|C2|C3|---- |C4|C5|C6|");

System.out.println("|Economy Class(1600.00)");

System.out.println("|E1|E2|E3|---- |E4|E5|E6|");

System.out.println("|F1|F2|F3|---- |F4|F5|F6|");

System.out.println("|G1|G2|G3|---- |G4|G5|G6|");

System.out.println("|H1|H2|H3|---- |H4|H5|H6|");

System.out.println("|I1|I2|");

System.out.println("Please key in a seat number to choose a seat(eg:A2)");

seat=sc.next();

ch=seat.charAt(0);

if(ch=='A' || ch=='B' || ch=='C')

{

fcls="First Class";

price="1920.00";

}

else

{

fcls="Economy Class";

price="1600.00";

}

System.out.println("*****************************");

System.out.println("Travel Ticket for FLIGHT");

System.out.println("*****************************");

System.out.println("Name : "+name+" \t\t\tTravel Ticket Class: "+fcls);

    System.out.println("\t\t\t\t\t\tSeat No: "+seat);

System.out.println("Departure:Johanesburg Departure Time: "+time[st-1]);

System.out.println("Destination:Cape Town Arrival Time: "+arrive[st-1]);

System.out.println("*****************************");

System.out.println("Amount:R"+price+" Thank you for booking with COS1511.Your travel agent for queries is Annie Mathew.");

System.out.println("Do you want to make anothere booking (1.Yes/2.No)?");

ch1=sc.nextInt();

}while(ch1!=2);

for(i=0;i<5;i++)

System.out.println("Number of bookings made for "+time[i]+" :"+cp[i]);

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

Code screenshot:

---------------------------------------------------------------------------------

Source code:

#include <iostream>         //Header file declaration..
#include <cstring>
#include <cstdlib>

using namespace std;

int main(void)
{
    char name[50];                  //Variable declaration..
    char seat[50]="NULL";
    char fcls[20];
    int st,i;
    char price[20];
    int ch1;
    char ch;
    char time[][10]={"7.00","9.00","11.00","13.00","15.00"};    //2D Array
    char arrive[][10]={"9.30","11.30","13.30","15.30","17.30"}; //2D Array
    int cp[5];
    do{
        cout<<"Welcome to CSO1511 Flight Booking System"<<endl;
        cout<<"Enter full name? ";
        cin>>name;                                  //Reading value from user..
        cout<<"The available travel times for flights are:"<<endl<<endl;
        cout<<" Depart \t Arrive"<<endl;        //Tme details menu
        cout<<" 1. 7.00 \t 9.30"<<endl;
        cout<<" 2. 9.00 \t 11.30"<<endl;
        cout<<" 3. 11.00 \t 13.30"<<endl;
        cout<<" 4. 13.00 \t 15.30"<<endl;
        cout<<" 5. 15.00 \t 17.30"<<endl;
        do                                      //Reading the choice..    
        {
            cout<<"Choose the time by entering the option number from the displayed list: ";
            cin>>st;
            if(st>5 || st<1)
                cout<<"Incorrect option! Please Choose from 1-5."<<endl;
        }while(st>5 || st<1);

cp[st-1]=cp[st-1]+1;
        cout<<"The available seats for "<<time[st-1]<<" are as follows"<<endl;
        cout<<"First Class(1920.00)"<<endl;
        cout<<"|A1|A2|A3|---- |A4|A5|A6|"<<endl;
        cout<<"|B1|B2|B3|---- |B4|B5|B6|"<<endl;
        cout<<"|C1|C2|C3|---- |C4|C5|C6|"<<endl;
        cout<<"|Economy Class(1600.00)"<<endl;
        cout<<"|E1|E2|E3|---- |E4|E5|E6|"<<endl;
        cout<<"|F1|F2|F3|---- |F4|F5|F6|"<<endl;
        cout<<"|G1|G2|G3|---- |G4|G5|G6|"<<endl;
        cout<<"|H1|H2|H3|---- |H4|H5|H6|"<<endl;
        cout<<"|I1|I2|"<<endl;
        cout<<"Please key in a seat number to choose a seat(eg:A2) : ";
        cin>>seat;
        ch=seat[0];
        if(ch=='A' || ch=='B' || ch=='C')
        {
            strcpy(fcls,"First Class");     //Copying data
            strcpy(price,"1920.00");
        }
        else
        {
            strcpy(fcls,"Economy Class");   //Copying data
            strcpy(price,"1600.00");
        }

cout<<"*****************************"<<endl;
        cout<<"Travel Ticket for FLIGHT"<<endl;         //Printing ticket
        cout<<"*****************************"<<endl;
        cout<<"Name : "<<name<<"\t"<<"Travel Ticket Class: "<<fcls<<"\t";
        cout<<"Seat No: "<<seat<<endl;
        cout<<"Departure:Johanesburg Departure Time: "<<time[st-1]<<endl;
        cout<<"Destination:Cape Town Arrival Time : "<<arrive[st-1]<<endl;
        cout<<"*****************************"<<endl;
        cout<<"Amount:R"<<price<<" Thank you for booking with COS1511."<<endl;
        cout<<"Your travel agent for queries is Annie Mathew."<<endl;
        cout<<"Do you want to make anothere booking (1.Yes/2.No)? ";
        cin>>ch1;
    }while(ch1!=2);

    for(i=0;i<5;i++) {
        cout<<"Number of bookings made for "<<time[i]<<" :"<<cp[i]<<endl;
    }
return 0;
}
-----------------------------------------

Output:

Run as : g++ <filename.cpp>

-------------------------------------------------------------------------

Please comment if you have any queries or changes required.

Kindly comment if you are satisfied with this solution

Add a comment
Know the answer?
Add Answer to:
convert this program from java to c++ import java.util.Scanner; class Main { public static void main(String[]...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class SieveOfEratosthenes {    public static void main(String args[]) {       Scanner sc...

    import java.util.Scanner; public class SieveOfEratosthenes {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number");       int num = sc.nextInt();       boolean[] bool = new boolean[num];            for (int i = 0; i< bool.length; i++) {          bool[i] = true;       }       for (int i = 2; i< Math.sqrt(num); i++) {          if(bool[i] == true) {             for(int j = (i*i); j<num; j = j+i) {                bool[j] = false;...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {...

    import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {        String name;        String answer;        int correct = 0;        int incorrect = 0;        Scanner phantom = new Scanner(System.in);        System.out.println("Hello, What is your name?");        name = phantom.nextLine();        System.out.println("Welcome " + name + "!\n");        System.out.println("My name is Danielle Brandt. "            +"This is a quiz program that...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • make this program run import java.util.Scanner; public class PetDemo { public static void main (String []...

    make this program run import java.util.Scanner; public class PetDemo { public static void main (String [] args) { Pet yourPet = new Pet ("Jane Doe"); System.out.println ("My records on your pet are inaccurate."); System.out.println ("Here is what they currently say:"); yourPet.writeOutput (); Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the correct pet name:"); String correctName = keyboard.nextLine (); yourPet.setName (correctName); System.out.println ("Please enter the correct pet age:"); int correctAge = keyboard.nextInt (); yourPet.setAge (correctAge); System.out.println ("Please enter the...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){...

    Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next();    String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word);    if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...

  • import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new...

    import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a; System.out.println("Input your age"); a = sc.nextInt(); boolean mess = isAllowed(a); String mess2 = ?isAllowed(a)return"You are allowed to vote";:"You arent allowed"; String age = personAge(a); personAge(a); } public static String personAge(int age) { String mess = ""; if(age<18) return mess = "You are minor"; else if(age>=18 && age<=22) return mess = "You are legal you can vote"; else if(age>=22 && age<=60) return...

  • Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    //...

    Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    // checks customers in and assigns them a boarding pass    // To the human user, Seats 1 to 2 are for First Class passengers and Seats 3 to 5 are for Economy Class passengers    //    public void reserveSeats()    {       int counter = 0;       int section = 0;       int choice = 0;       String eatRest = ""; //to hold junk in input buffer       String inName = "";      ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT