Cannot get the correct output

Hello guys, I want to know why I still didn't get the correct output for number of tickets and total amount. Anyone can 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( )

The syntax that I have prepared
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
using namespace std;

class Ticket
{
private:
    int no;
    float price;

public:
    friend class Student;

    Ticket()
    {
        price = 10.00;
    }

    void setTickets()
    {
        cout << "Please enter number of tickets to purchase: ";
        cin >> no;
    }
};

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 no;

        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 = no * 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];

    for (int i = 0; i < 3; i++)
    {
        S1[i].setStudent();
        S1[i].ticket_entry();
        S1[i].display();
    }
    return 0;
}
1
2
3
4
           cout << "You've purchase " << no << "Tickets" << endl;
           cout << "Total amount    : RM " << total << endl;

            total = no * 10.00;


no is a local variable that hasn't been initialized to anything. It contains junk. I believe you actually want to use P.no

total is a local variable that hasn't been initialized to anything. It contains junk. (I may be seeing a pattern here.) After you print the junk it contains, you assign it the value you wanted to output. There may be something wrong with that order of events. total = P.no * P.price;
Ok, now I understand but how about total? I put P.total and declare total inside class Ticket but it gives me the output of "RM 0". If I put total = P.no * P.price, it will gives junk output.
It's ok. I got the correct output already. I remove total and put this to get the correct output. Thanks for your help

1
2
3
4
5
            cout << "--------------------------------" << endl << endl;
            cout << "       ADDITIONAL DETAILS       " << endl;
            cout << "--------------------------------" << endl;
            cout << "You've purchase " << P.no << " Tickets" << endl;
            cout << "Total amount    : RM " << P.no * P.price << endl;
You don't need to put total inside the class. It's fine as a local variable. The key point is that it contains junk when you output it. Then you set it to the value you wanted to output.

You need to switch the order around so that you set it to what you want to output before you actually output it.

It's kind of like opening your car door before you unlock it. You're not likely to get the result you intended to get.
Last edited on
Topic archived. No new replies allowed.