Like Tree1Likes
  • 1 Post By DRK

Cannot get the correct output

This is a discussion on Cannot get the correct output within the C++ Programming forums, part of the General Programming Boards category; Hello guys, I need help in this exercise. The problem that occurred was when user input "y" at "Do you ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    10

    Question Cannot get the correct output

    Hello guys, I need help in this exercise. The problem that occurred was when user input "y" at "Do you want to purchase .......", the output will show else statement inside void display(). What I need to do to make the output show if statement inside void display()? Please give me a hint. Thanks in advance.

    This is the question:

    Create class Ticket with the following:
    i. Data members (private):
     no : int
     price : float
    ii. Declare class Student as a friend of class Ticket.
    o Public member functions:
     A default constructor to set price to 10.00
     void setTickets() : prompt and get user input for no (number of tickets to purchase).
    Create class Student with the following:
    i. Data members (private):
     id : string
     name : string
     purchase : string
     P : Ticket
    ii. Public member functions:
     void setStudent( ): prompt and get user input for name and id (refer to screenshot).
     void ticket_entry( ): prompt user whether to purchase tickets.
     If user enters ‘Y’, set purchase to “Yes”, and call function setTickets() using object P.
     If user choose not to purchase ticket, display “--------No tickets purchase--------“
     void display( ): to display the student’s details (name, id) and additional details . If purchase equals “Yes’, display the details (no, total amount of tickets purchased (no multiply price). Otherwise, display “You’ve not purchased any tickets”.[Refer to sample output screen]
    In main( ):
     Declare an array of 3 object elements of class Student.
     Using a for-loop that loops on every array element:
     Call setStudent( ), ticket_entry( ), and display( )


    Sample Output screen
    Enter ID : 1011
    Enter Name : Julie
    Do you want to purchase charity tickets? [Enter Y or N]:Y
    Please enter number of tickets to purchase: 2
    --------------------------------
    STUDENT DETAILS
    --------------------------------
    ID : 1011
    Name : Julie
    --------------------------------
    ADDITIONAL DETAILS
    --------------------------------
    You've purchase 2 Tickets
    Total amount : RM 20
    Enter ID : 1013
    Enter Name : Andy
    Do you want to purchase charity tickets? [Enter Y or N]:N
    --------No tickets purchase--------
    --------------------------------
    STUDENT DETAILS
    --------------------------------
    ID : 1013
    Name : Andy
    --------------------------------
    ADDITIONAL DETAILS
    --------------------------------
    You've not purchased any tickets
    Enter ID : 1055
    Enter Name : Roslan
    Do you want to purchase charity tickets? [Enter Y or N]:Y
    Please enter number of tickets to purchase: 20
    --------------------------------
    STUDENT DETAILS
    --------------------------------
    ID : 1055
    Name : Roslan


    Syntax that I have prepared:
    Code:
    #include <iostream>
    using namespace std;
    
    
    class Ticket
    {
    private:
        int no;
        float price;
    
    
    public:
        friend class Student;
    
    
        Ticket()
        {
            price = 10.00;
        }
    
    
        void setTickets()
        {
            int no;
    
    
            cout << "Please enter number of tickets to purchase: ";
            cin >> no;
        }
    };
    
    
    class Student
    {
    private:
        string id, name, purchase;
        int purchaseticket;
        Ticket P;
    
    
    public:
        void setStudent()
        {
            cout << "\nEnter ID        : ";
            getline (cin, id);
            cout << "Enter Name      : ";
            getline (cin, name);
        }
    
    
        void ticket_entry()
        {
            char y, n, choice, yes;
    
    
            cout << "Do you want to purchase charity tickets? [Enter Y or N] :" ;
            cin >> purchaseticket;
    
    
            if (choice == 'y')
            {
                purchase = "yes";
                P.setTickets();
            }
    
    
            if (choice == 'n')
            {
                cout << "---------No tickets purchase----------";
            }
        }
    
    
        void display()
        {
            char yes;
            float total;
            int no, tickets;
    
    
            if (purchase == "yes")
            {
                cout << "--------------------------------" << endl;
                cout << "         STUDENT DETAILS        " << endl;
                cout << "--------------------------------" << endl;
                cout << "ID            : " << id << endl;
                cout << "Name          : " << name << endl;
    
    
                cout << "--------------------------------" << endl << endl;
                cout << "       ADDITIONAL DETAILS       " << endl;
                cout << "--------------------------------" << endl;
                cout << "You've purchase " << no << "Tickets" << endl;
                cout << "Total amount    : RM " << total << endl;
    
    
                total = tickets * 10.00;
            }
    
    
            else
                cout << "--------------------------------" << endl;
                cout << "         STUDENT DETAILS        " << endl;
                cout << "--------------------------------" << endl;
                cout << "ID            : " << id << endl;
                cout << "Name          : " << name << endl;
    
    
                cout << "--------------------------------" << endl << endl;
                cout << "       ADDITIONAL DETAILS       " << endl;
                cout << "--------------------------------" << endl;
                cout << "You've not purchased any ticktes" << endl;
            }
    };
    
    
    int main()
    {
        Student S1[3];
    
    
        for (int i = 0; i < 3; i++)
        {
            S1[i].setStudent();
            S1[i].ticket_entry();
            S1[i].display();
        }
        return 0;
    }

  2. #2
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    109
    Line #57 should be:
    Code:
    cin >> choice;

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    10
    Oh! I didn't see that. Thanks. Last question, how do I display the correct amount of tickets and total amount. I don't think I put the wrong data member.

    Code:
                cout << "--------------------------------" << endl << endl;
                cout << "       ADDITIONAL DETAILS       " << endl;
                cout << "--------------------------------" << endl;
                cout << "You've purchase " << number << "Tickets" << endl;
                cout << "Total amount    : RM " << total << endl;
    Code:
    #include <iostream>
    using namespace std;
    
    
    class Ticket
    {
    private:
        int number;
        float price;
    
    
    public:
        friend class Student;
    
    
        Ticket()
        {
            price = 10.00;
        }
    
    
        void setTickets()
        {
            cout << "Please enter number of tickets to purchase: ";
            cin >> number;
        }
    };
    
    
    class Student
    {
    private:
        string id, name, purchase;
        Ticket P;
    
    
    public:
        void setStudent()
        {
            cout << "\nEnter ID        : ";
            getline (cin, id);
            cin.ignore();
            cout << "Enter Name      : ";
            getline (cin, name);
            cin.ignore();
        }
    
    
        void ticket_entry()
        {
            char y, n, yes, choice;
    
    
            cout << "Do you want to purchase charity tickets? [Enter Y or N] :" ;
            cin >> choice;
    
    
            if (choice == 'y')
            {
                purchase = "yes";
                P.setTickets();
            }
    
    
            if (choice == 'n')
            {
                cout << "---------No tickets purchase----------";
            }
        }
    
    
        void display()
        {
            char yes, noo;
            float total;
            int number;
    
    
            if (purchase == "yes")
            {
                cout << "--------------------------------" << endl;
                cout << "         STUDENT DETAILS        " << endl;
                cout << "--------------------------------" << endl;
                cout << "ID            : " << id << endl;
                cout << "Name          : " << name << endl;
    
    
                cout << "--------------------------------" << endl << endl;
                cout << "       ADDITIONAL DETAILS       " << endl;
                cout << "--------------------------------" << endl;
                cout << "You've purchase " << number << "Tickets" << endl;
                cout << "Total amount    : RM " << total << endl;
    
    
                total = number * 10.00;
            }
    
    
            if (purchase == "noo")
            {
                cout << "--------------------------------" << endl;
                cout << "         STUDENT DETAILS        " << endl;
                cout << "--------------------------------" << endl;
                cout << "ID            : " << id << endl;
                cout << "Name          : " << name << endl;
    
    
                cout << "--------------------------------" << endl << endl;
                cout << "       ADDITIONAL DETAILS       " << endl;
                cout << "--------------------------------" << endl;
                cout << "You've not purchased any ticktes" << endl;
            }
        }
    };
    
    
    int main()
    {
        Student S1[3];
        int i = 0;
    
    
    
    
            S1[i].setStudent();
            S1[i].ticket_entry();
            S1[i].display();
    
    
        return 0;
    }

  4. #4
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    109
    Code:
    float total;
    int number;
    These are uninitialized local variables, that means they hold random value. You have to assign their values before printing on the screen.

    Don't ignore warnings from the compiler:
    warning: 'total' is used uninitialized in this function
    warning: 'number' is used uninitialized in this function
    Last edited by DRK; 07-16-2013 at 06:51 AM.
    Elysia likes this.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by DRK View Post
    Don't ignore warnings from the compiler:
    warning: 'total' is used uninitialized in this function
    warning: 'number' is used uninitialized in this function
    In order to get this warnings you may need to use -Wall flag
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-17-2012, 04:34 PM
  2. why am i not getting correct output
    By roaan in forum C Programming
    Replies: 1
    Last Post: 09-11-2009, 05:10 PM
  3. Getting correct output
    By blah3 in forum C++ Programming
    Replies: 3
    Last Post: 08-01-2008, 05:54 PM
  4. Switch statement and returning the correct output
    By slowcoder in forum C Programming
    Replies: 6
    Last Post: 09-28-2006, 04:00 PM
  5. Need help fixing error to get correct output.
    By kctrk4 in forum C Programming
    Replies: 12
    Last Post: 04-07-2006, 10:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21