Errors

Hi, I need some help on my programs. Im trying to do Prepaid balance program but there are few things that im stuck in.
1. After I main loop for the first time n it shows me the current balance that I have, If i select lets say I want to choose calls, and choose internet plan how do I calculate my new balance?
2. I have errors in displaying my Transaction summary, how do i call the function so that i can display the items inside my transaction summary.
3. And how do I make sure my balance will never be negative, how do I ensure that I cant perform the action if my input is exceeded the balance I have.


// Main function
void main()
{
// Assign data type and initialization
char resp, sentinel = 'Y';
int selection;
double call_charges,sms_charges, top_up_charges, internet_charges, new_balance;

resp = display_logo();
if (toupper(resp) == sentinel)
cout << "You've exited." << endl;
else
{
display_prepaid_balance();
display_menu();
do
{
cout << "Selection: ";
cin >> selection;// Input for selection
switch (selection)
{
case 1: {// Calls
display_call_charges();
call_charges = calculate_call_charges();
}
break;
case 2: {// SMS
display_sms_charges();
sms_charges = calculate_sms_charges();
}
break;
case 3: // Top Up
top_up_charges = calculate_top_up();
break;
case 4: {// Internet Plan
display_internet_charges();
internet_charges = calculate_internet_charges();
}
}
new_balance = calculate_new_balance(call_charges, sms_charges, top_up_charges, internet_charges);
cout << "NEW BALANCE = RM " << new_balance << endl;
cout << "\n" << endl;
if (new_balance <= 2) {
cout << "Your balance is RM " << new_balance << endl;
do {
cout << "You must have minimum RM2.00 before making any calls or sms. " << endl;
new_balance += calculate_top_up();
cout << "Your balance is RM " << new_balance << endl;
} while (new_balance <= 2);
}
}while (selection >= 1 && selection <= 4);

if (selection == 5) {// show Transaction Summary
display_transaction_summary( call_duration, sms, call_charges, charges, new_balance);
}
}

system("pause");
} // End main function

char display_logo()
{
char cont;
cout << " WELCOME TO TARMOBILE CENTER ~" << endl;
cout << "Enter any key to continue (Y to exit): ";
cin >> cont;
return cont;
}

void display_prepaid_balance() // Display prepaid balance
{
cout << "\t\t\tYour initial prepaid balance\t: RM " << INI_BAL << "\n" << endl;
}

void display_menu() // Display menu
{
cout << "\t\t\t| TARMOBILE Prepaid service (Selection) | " << endl;

cout << "1. CAL" << endl;
cout << "2. SMS|" << endl;
cout << "3. TOP UP " << endl;
cout << "4. INTERNET PLAN" << endl;
cout << "5. EXIT" << endl;

}

void display_call_charges() // Display Charges Rates for Calls
{

cout << "\t\t|The Charge Rates For Calls

cout << " \t\t| Band 1 (first 10 minutes)\t: The charge rate is RM " << fixed << setprecision(2) << BAND1 << " per minute.\t|" << endl;
cout << " \t\t| Band 2 (next 10 minutes)\t: The charge rate is RM " << fixed << setprecision(2) << BAND2 << " per minute.\t|" << endl;
cout << " \t\t| Band 3 (subsequent minutes)\t: The charge rate is RM " << fixed << setprecision(2) << BAND3 << " per minute.\t|" << endl;

}

double calculate_call_charges()
{
double minutes, call_charges;
do {
minutes = input_minutes();
if (minutes <= 10) //Band 1
call_charges = B1_charges(minutes);

if (minutes > 10 && minutes <= 20) //Band 2
call_charges = B2_charges(minutes);

if (minutes > 20) // Band 3
call_charges = B3_charges(minutes);
cout << " \t Charge for this call = RM " << call_charges ;
} while (minutes < 0);
call_charges_accumulator(call_charges);
return call_charges;
}

double input_minutes()
{
double call_duration;
cout << "Call duration (minutes) \t:";
cin >> call_duration;
minutes_accumulator(call_duration);
return call_duration;
}

double minutes_accumulator(double call_duration)
{
total_minutes += call_duration;
return total_minutes;
}

double B1_charges(double minutes) //Band 1
{
double charges;

charges = minutes * BAND1;
cout << "Band 1 : " << minutes << " minutes @ RM " << BAND1 << " = RM " << charges << endl;
cout << "Band 2 : 0.0 minutes @ RM " << BAND2 << " = RM 0.00" << endl;
cout << "Band 3 : 0.0 minutes @ RM " << BAND3 << " = RM 0.00" << endl;
return charges;
}

double B2_charges(double minutes) // Band 2
{
double B1_charges, B2_mins, B2_charges, charges;

B1_charges = 10.0 * BAND1;
B2_mins = minutes - 10.0;
B2_charges = B2_mins * BAND2;
charges = B1_charges + B2_charges;
cout << "Band 1 : 10.0 minutes @ RM " << BAND1 << " = RM " << B1_charges << endl;
cout << "Band 2 : " << fixed << setprecision(1) << B2_mins << " minutes @ RM " ) << BAND2 << " = RM " << B2_charges << endl;
cout << "Band 3 : 0.0 minutes @ RM " << BAND3 << " = RM0.00" << endl;
return charges;
}

double B3_charges(double minutes) // Band 3
{
double B1_charges, B3_mins, B2_charges, B3_charges, charges;

B1_charges = 10.0 * BAND1;
B2_charges = 10 * BAND2;
B3_mins = minutes - 20.0;
B3_charges = B3_mins * BAND3;
charges = B1_charges + B2_charges + B3_charges;
cout << "Band 1 : 10.0 minutes @ RM " << BAND1 << " = RM " << B1_charges << endl;
cout << "Band 2 : 10.0 minutes @ RM " << BAND2 << " = RM " << B2_charges << endl;
cout << "Band 3 : " << B3_mins << " minutes @ RM " << BAND3 << " = RM " << B3_charges << endl;
return charges;
}

double call_charges_accumulator(double call_charges)
{
total_call_charges += call_charges;
return total_call_charges;
}

void display_sms_charges() // Display Charges Rates for SMS
{

cout << "The Charge Rates For SMS" << endl;
cout << "The charge rate for each SMS is RM 0.10" << endl;

}

double calculate_sms_charges()
{
int num_sms;
double charges;
num_sms = input_sms();
charges = num_sms * SMS;
cout << "Your total SMS charges : RM " << charges << endl;
sms_charges_accumulator(charges);
return charges;
}

int input_sms()
{
int sms;
cout << "How many SMS do you want to send? : ";
cin >> sms;
num_sms_accumulator(sms);
return sms;
}

double num_sms_accumulator(int sms)
{
total_num_sms += sms;
return total_num_sms;
}

double sms_charges_accumulator(double charges)
{
total_sms_charges += charges;
return total_sms_charges;
}

double calculate_top_up()
{
double top_up;
cout << "\nAmount to TOP UP? : RM ";
cin >> top_up;
top_up_accumulator(top_up);
return top_up;
}

double top_up_accumulator(double top_up)
{
total_top_up += top_up;
return total_top_up;
}

void display_internet_charges()
{
cout << "Internet Data Subscribtion Plan" << endl;
cout << "PLAN A: RM 2 / day for 1GB of internet" << endl;
cout << "PLAN B: RM 10 / week for 10GB of internet |" << endl;
}

double calculate_internet_charges()
{
char int_plan;
double int_duration, int_charge;
do {
int_plan = input_internet_plan();
if (toupper(int_plan) == 'A') {
cout << "Days : ";
cin >> int_duration;
int_charge = PLAN_A * int_duration;
}
if (toupper(int_plan) == 'B') {
cout << "Weeks : ";
cin >> int_duration;
int_charge = PLAN_B * int_duration;
}
cout << "Your total internet charges : RM " << int_charge << endl;
internet_charge_accumulator(int_charge);
return int_charge;
} while (toupper(int_plan) != 'A' && toupper(int_plan) != 'B');
}

char input_internet_plan()
{
char int_plan;
cout << "Internet Data Plan (A/B) : ";
cin >> int_plan;
return int_plan;
}

double internet_charge_accumulator(int int_charge)
{
total_internet_charge += int_charge;
return total_internet_charge;

}

double calculate_new_balance(double call_charges, double sms_charges, double top_up_charges, double internet_charges)
{
double new_balance;
new_balance = INI_BAL - call_charges - sms_charges + top_up_charges - internet_charges;
return new_balance;
}

void display_transaction_summary(double call_duration, int sms, double call_charges, double charges, double new_balance)
{
cout << " \tTRANSACTION SUMMARY\n" << endl;
cout << "In total, you talked for " << minutes_accumulator(call_duration) << " minutes and made ";
cout << num_sms_accumulator(sms) << " SMS's.\n" << endl;
cout << "Starting Balance\t\t\t: RM " << INI_BAL << endl;
cout << "Total call charges\t\t: RM " << call_charges_accumulator(call_charges) << endl;
cout << "Total SMS charges\t\t: RM " << sms_charges_accumulator(charges) << endl;
cout << "Ending Balance\t: RM" << new_balance << endl;
}
1) When you post code, please use <> code tags.

2) Please post compilable or nearly compilable code.

3) Writing tons of code before starting testing it it’s a good way to spend a lot of time debugging.

4) You’d better avoid declaring variables before you have a value to store into; specifically do avoid declaring your variables in a bunch at the beginning of your functions.

5) In general, you’d better initialise your variables as soon as you declare them.

6) In your functions there are undeclared variables. Do you use any global variables? Where do you declare them?

7) I’d avoid calling double variables ‘int_something’ (this is a matter of tastes, anyway).

8) Sometimes you neglet the return value from your non-void functions, even when you haven’t passed the arguments by reference. That means whatever it happens inside the functions, it’s lost.

9) A lot of errors the compiler provides you are very easy to interpret: please, do try harder! ;-)

What follows and in the next post is your code, but a bit more tidier and with some comments (it doesn’t compile yet, of course).

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Hi,  I need some help on my programs. Im trying to do Prepaid balance program
// but there are few things that im stuck in.
// 1. After I main loop for the first time n it shows me the current balance
//    that I have, If i select lets say I want to choose calls, and choose
//    internet plan how do I calculate my new balance?
// 2. I have errors in displaying my Transaction summary, how do i call the
//    function so that i can display the items inside my transaction summary.
// 3. And how do I make sure my balance will never be negative, how do I ensure
//    that I cant perform the action if my input is exceeded the balance I have.
#include <iostream>


constexpr double Ini_Bal { 6.66 };


double B1_charges(double minutes);
double B2_charges(double minutes);
double B3_charges(double minutes);
double calculate_call_charges();
double calculate_internet_charges();
double calculate_new_balance(double call_charges,
                             double sms_charges,
                             double top_up_charges,
                             double internet_charges);
double calculate_sms_charges();
double calculate_top_up();
double call_charges_accumulator(double call_charges);
char display_logo();
void display_transaction_summary(double call_duration,
                                 int sms,
                                 double call_charges,
                                 double charges,
                                 double new_balance);
char input_internet_plan();
double input_minutes();
double internet_charge_accumulator(int int_charge);
double minutes_accumulator(double call_duration);
double num_sms_accumulator(int sms);
double sms_charges_accumulator(double charges);
double top_up_accumulator(double top_up);
int input_sms();
void display_call_charges();
void display_internet_charges();
void display_menu();
void display_prepaid_balance();
void display_sms_charges();


void main()
{
    // Assign data type and initialization
    char sentinel = 'Y';

    char resp = display_logo();
    if (toupper(resp) == sentinel) {
        std::cout << "You've exited.\n";
        return 0;
    }
    
    display_prepaid_balance();
    display_menu();

    double call_charges {},
           sms_charges {},
           top_up_charges {},
           internet_charges {},
           new_balance {};
    int selection;
    do {
        std::cout << "Selection: ";
        std::cin >> selection;
        switch (selection)
        {
        case 1: // Calls
            display_call_charges();
            call_charges = calculate_call_charges();
            break;
        case 2: // SMS
            display_sms_charges();
            sms_charges = calculate_sms_charges();
            break;
        case 3: // Top Up
            top_up_charges = calculate_top_up();
            break;
        case 4: // Internet Plan
            display_internet_charges();
            internet_charges = calculate_internet_charges();
        default:
            // Add controls here!
            break;
        }

        double new_balance = calculate_new_balance(call_charges,
                                                   sms_charges,
                                                   top_up_charges,
                                                   internet_charges);
        std::cout << "NEW BALANCE = RM " << new_balance << "\n\n\n";
        if (new_balance <= 2) {
            std::cout << "Your balance is RM " << new_balance << '\n';
            do {
                std::cout << "You must have minimum RM2.00 before making any "
                             "calls or sms. \n";
                new_balance += calculate_top_up();
                std::cout << "Your balance is RM " << new_balance << '\n';
            } while (new_balance <= 2);
        }
    } while (selection >= 1 && selection <= 4);

    // show Transaction Summary
    if (selection == 5) {
        display_transaction_summary(call_duration,  // not declared in this scope
                                    sms,            // not declared in this scope
                                    call_charges,
                                    charges,        // not declared in this scope
                                    new_balance);
    }
}


char display_logo()
{
    std::cout << "WELCOME TO TARMOBILE CENTER ~\n"
                 "Enter any key to continue (Y to exit): ";
    char cont;
    std::cin >> cont;
    return cont;
}


void display_prepaid_balance()
{
    std::cout << "\t\t\tYour initial prepaid balance\t: RM " << Ini_Bal << "\n\n";
}


void display_menu()
{
    std::cout << "\t\t\t| TARMOBILE Prepaid service (Selection) |\n"
                 "1. CAL\n"
                 "2. SMS\n"
                 "3. TOP UP \n"
                 "4. INTERNET PLAN\n"
                 "5. EXIT\n";
}


void display_call_charges()
{
    std::cout << "\t\t|The Charge Rates For Calls\n"
                 " \t\t| Band 1 (first 10 minutes)\t: The charge rate is RM "
              << std::fixed << std::setprecision(2) << BAND1
              << " per minute.\t|\n"
                 " \t\t| Band 2 (next 10 minutes)\t: The charge rate is RM "
              << std::fixed << std::setprecision(2) << BAND2
              << " per minute.\t|\n"
                 " \t\t| Band 3 (subsequent minutes)\t: The charge rate is RM "
              << std::fixed << std::setprecision(2) << BAND3
              << " per minute.\t|\n";
}


double calculate_call_charges()
{
    double minutes,
           call_charges;
    do {
        minutes = input_minutes();
        if (minutes <= 10) {                    // Band 1
            call_charges = B1_charges(minutes);
        }
        
        if (minutes > 10 && minutes <= 20) {    // Band 2
            call_charges = B2_charges(minutes);
        }
        
        if (minutes > 20) {                     // Band 3
            call_charges = B3_charges(minutes);
        }
        std::cout << " \t Charge for this call = RM " << call_charges ;
    } while (minutes < 0);

    call_charges_accumulator(call_charges);
    return call_charges;
}

Last edited on
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
double input_minutes()
{
    std::cout << "Call duration (minutes): ";
    double call_duration;
    std::cin >> call_duration;
    minutes_accumulator(call_duration);
    return call_duration;
}


double minutes_accumulator(double call_duration)
{
    // total_minutes hasen't been declared.
    total_minutes += call_duration;
    return total_minutes;
}


//Band 1
double B1_charges(double minutes)
{
    double charges = minutes * BAND1;
    std::cout << "Band 1: " << minutes
              << " minutes @ RM " << BAND1
              << " = RM " << charges << '\n'
              << "Band 2: 0.0 minutes @ RM " << BAND2 << " = RM 0.00\n"
              << "Band 3: 0.0 minutes @ RM " << BAND3 << " = RM 0.00\n";
    return charges;
}


// Band 2
double B2_charges(double minutes)
{
    double B1_charges = 10.0 * BAND1;
    double B2_mins = minutes - 10.0;
    double B2_charges = B2_mins * BAND2;
    double charges = B1_charges + B2_charges;
    std::cout << "Band 1: 10.0 minutes @ RM " << BAND1
              << " = RM " << B1_charges << '\n'
              << "Band 2: " << std::fixed << std::setprecision(1) << B2_mins
              << " minutes @ RM " ) << BAND2 << " = RM " << B2_charges << '\n'
              << "Band 3: 0.0 minutes @ RM " << BAND3 << " = RM0.00\n";
    return charges;
}


// Band 3
double B3_charges(double minutes)
{
    double B1_charges = 10.0 * BAND1;
    double B2_charges = 10 * BAND2;
    double B3_mins = minutes - 20.0;
    double B3_charges = B3_mins * BAND3;
    double charges = B1_charges + B2_charges + B3_charges;
    std::cout << "Band 1: 10.0 minutes @ RM " << BAND1
              << " = RM " << B1_charges << '\n'
              << "Band 2: 10.0 minutes @ RM " << BAND2
              << " = RM " << B2_charges << '\n';
              << "Band 3: " << B3_mins << " minutes @ RM " << BAND3
              << " = RM " << B3_charges << '\n';
    return charges;
}


double call_charges_accumulator(double call_charges)
{
    total_call_charges += call_charges;
    return total_call_charges;
}


// Display Charges Rates for SMS
void display_sms_charges()
{
    std::cout << "The Charge Rates For SMS\n"
                 "The charge rate for each SMS is RM 0.10\n";
}


double calculate_sms_charges()
{
    int num_sms = input_sms();
    double charges = num_sms * SMS;
    std::cout << "Your total SMS charges: RM " << charges << '\n';
    sms_charges_accumulator(charges);
    return charges;
}


int input_sms()
{
    std::cout << "How many SMS do you want to send?: ";
    int sms;
    std::cin >> sms;
    num_sms_accumulator(sms);
    return sms;
}


double num_sms_accumulator(int sms)
{
    total_num_sms += sms;
    return total_num_sms;
}


double sms_charges_accumulator(double charges)
{
    // total_sms_charges hasn't been declared.
    total_sms_charges += charges;
    return total_sms_charges;
}


double calculate_top_up()
{
    std::cout << "\nAmount to TOP UP?: RM ";
    double top_up;
    std::cin >> top_up;
    top_up_accumulator(top_up);
    return top_up;
}


double top_up_accumulator(double top_up)
{
    // total_top_up hasn't been declared.
    total_top_up += top_up;
    return total_top_up;
}


void display_internet_charges()
{
    std::cout << "Internet Data Subscribtion Plan\n"
                 "PLAN A: RM 2 / day for 1GB of internet\n"
                 "PLAN B: RM 10 / week for 10GB of internet |\n"
}


double calculate_internet_charges()
{
    char int_plan;
    do {
        int_plan = input_internet_plan();
        if (toupper(int_plan) == 'A') {
            std::cout << "Days: ";
            double int_duration;
            // Suggestion: explicitely ask for duration.
            std::cin >> int_duration;
            double int_duration;
            int_charge = PLAN_A * int_duration;
        }
        if (toupper(int_plan) == 'B') {
            std::cout << "Weeks: ";
            std::cin >> int_duration;
            int_charge = PLAN_B * int_duration;
        }
        std::cout << "Your total internet charges: RM " << int_charge << '\n';
        internet_charge_accumulator(int_charge);
        return int_charge;  // function terminates here, so it never gets to the
                            // following line; hence it iterates only once
    } while (toupper(int_plan) != 'A' && toupper(int_plan) != 'B');
}


char input_internet_plan()
{
    std::cout << "Internet Data Plan (A/B): ";
    char int_plan;
    std::cin >> int_plan;
    return int_plan;
}


double internet_charge_accumulator(int int_charge)
{
    total_internet_charge += int_charge;
    return total_internet_charge;

}


double calculate_new_balance(double call_charges,
                             double sms_charges,
                             double top_up_charges,
                             double internet_charges)
{
    double new_balance =   Ini_Bal
                         - call_charges
                         - sms_charges
                         + top_up_charges
                         - internet_charges;
    return new_balance;
}


void display_transaction_summary(double call_duration,
                                 int sms,
                                 double call_charges,
                                 double charges,
                                 double new_balance)
{
    std::cout << " \tTRANSACTION SUMMARY\n\n"
                 "In total, you talked for "
              << minutes_accumulator(call_duration)
              << " minutes and made "
              << num_sms_accumulator(sms)
              << " SMS's.\n\n"
                 "Starting Balance\t\t\t: RM "
              << Ini_Bal
              << "\nTotal call charges\t\t: RM "
              << call_charges_accumulator(call_charges)
              << "\nTotal SMS charges\t\t: RM "
              << sms_charges_accumulator(charges)
              << "\nEnding Balance\t: RM" << new_balance << '\n';
}

Topic archived. No new replies allowed.