I need advice to fix my code.

Hello, I need some assistance with my code. So with this question, I need to combine for loop and if/else statements. However there are problems. 1. (I'm not finished with the code) When I loop the entire code, it gives me a higher number than intended for each customer. 2. For home-phone, it doubles the cout statment meaning it gives vat and total bill twice.

This is the question.
A telecommunication company offers two types of phone services: home phone and mobile
phone.
The home phone has a monthly rental fee of $25.00 per unit and a charge of $0.25 per minute of
airtime.
The mobile phone has two packages: pay-as-you-go and pre-paid.
The pay-as-you-go package is billed at $0.50 per minute of airtime.
The pre-paid package is a fixed monthly cost of $50.00.
Write a program, PhoneService.cpp, to calculate the bill for ten (10) customers. For each
customer, the program should prompt the user for the type of phone service (1: home phone and
2: mobile phone). The user is then asked to enter the number of minutes of airtime used. If the
user has a mobile phone they are prompted to enter which package they subscribe to (3: pay-asyou-go
and 4: pre-paid). VAT is charged at 12.5% for each customer. The program should print
the type of phone service (home phone or mobile phone), package if any (mobile phone only),
the number of minutes used, the cost of the airtime, the VAT charged and the total bill.
The program should also output the total bill and total VAT of the ten (10) customers.


Here is my code.
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
#include <iostream>
using namespace std;
int main(){
	int home_phone;
	int mobile_phone;
	int phone_service;
	int count;
	int package;
	int airtime;
	int prepaid;
	int paygo;//pay as you go
	double phonerental_fee;
	double phone_charge;
	double mobile_charge;
	double vat;
	double total_bill;
	phone_charge=0.25;
	vat=0.125;
	phonerental_fee=25;
	mobile_charge=0.50;
	
	
		
	
	
	cout<<"Enter the phone service(1 for home phone and 2 for mobile phone): ";
	cin>>phone_service;
	  cout<<"Enter the amount of minutes of airtime used: ";
	  cin>>airtime;
	  
	  if(phone_service==1){
	
	  phone_charge=(phone_charge*airtime)+phonerental_fee;
      vat=vat*phone_charge;
	  total_bill=vat+phone_charge;
	  cout<<"Vat charged: $"<<vat<<endl;
	  cout<<"Bill of the customer: $"<<total_bill<<endl;//Home Phone
	  }
	  
	  else{ 
	  cout<<"Enter the mobile package(3 for pay-as-you-go or 4 for pre-paid): ";
	  cin>>package;
	  }
	  if(package==3){
		  mobile_charge=mobile_charge*airtime;
		  vat=vat*mobile_charge;
		  total_bill=vat+mobile_charge;
		  cout<<"Vat charged: $"<<vat<<endl;
		  cout<<"Bill of the customer: $"<<total_bill;//Mobile Phone Package 3
	
}
	  else{
	  	prepaid=50;
	  	vat=vat*prepaid;
	  	total_bill=vat+prepaid;
	  	cout<<"Vat charged: $"<<vat<<endl;
		  cout<<"Bill of the customer: $"<<total_bill;//Mobile phone package 4
	  }


}
Last edited on
I've summarized in a scheme the assignment requirements. Perhaps it can be of some help.

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
// A telecommunication company offers two types of phone services: home phone
// and mobile phone.
// The home phone has a monthly rental fee of $25.00 per unit and a charge of
// $0.25 per minute of airtime.
// The mobile phone has two packages: pay-as-you-go and pre-paid.
//    The pay-as-you-go package is billed at $0.50 per minute of airtime.
//    The pre-paid package is a fixed monthly cost of $50.00.
// Write a program, PhoneService.cpp, to calculate the bill for ten (10)
// customers.
// For each customer, the program should prompt the user for the type of phone
// service (1: home phone and 2: mobile phone).
// The user is then asked to enter the number of minutes of airtime used.
// If the user has a mobile phone they are prompted to enter which package they
// subscribe to (3: pay-asyou-go and 4: pre-paid).
// VAT is charged at 12.5% for each customer.
// The program should print the type of phone service (home phone or mobile
// phone), package if any (mobile phone only), the number of minutes used, the
// cost of the airtime, the VAT charged and the total bill.

#include <iostream>

int main()
{
    /* Assignment:
     * Write a program, PhoneService.cpp, to calculate the bill for ten (10)
     * customers.
     */
    set up 'something' to store the 10 sums of the total bills and total VATs;

    start 10-iterations loop {
        /* Assignment:
         * For each customer, the program should prompt the user for the type
         * of phone service (1: home phone and 2: mobile phone).
         */
        std::cout << "Enter 1 for home phone or 2 for mobile phone: ";
        create proper variable for home_or_mobile;
        std::cin >> home_or_mobile;

        /* Assignment:
         * The user is then asked to enter the number of minutes of airtime used.
         */
        std::cout << "Please enter the amount of minutes of airtime used: ";
        create proper variable for airtime;
        std::cin >> airtime;

        /* Assignment:
         * If the user has a mobile phone they are prompted to enter which
         * package they subscribe to (3: pay-asyou-go and 4: pre-paid).
         */
        create proper variables for bill and vat;
        create proper variable for package; // it's required later

        if(home_or_mobile ... ) {
            // home
            /* Assignment:
             * The home phone has a monthly rental fee of $25.00 per unit and a
             * charge of $0.25 per minute of airtime.
             */
            bill = ... ;
        }

        else {
            // mobile
            /* Assignment:
             * If the user has a mobile phone they are prompted to enter which
             * package they subscribe to (3: pay-asyou-go and 4: pre-paid).
             */
            std::cout << "Enter 3 for pay-as-you-go or 4 for pre-paid: ";
            std::cin >> package;

            if(package ...) {
                // pay-as-you-go
                /* Assignment:
                 * The pay-as-you-go package is billed at $0.50 per minute of
                 * airtime.
                 */
                bill = ... ;
            }

            else {
                // pre-paid
                /* Assignment:
                 * The pre-paid package is a fixed monthly cost of $50.00.
                 */
                bill = ... ;
            }
        }

        /* Assignment:
         * The program should print the type of phone service (home phone or 
         * mobile phone), package if any (mobile phone only), the number of 
         * minutes used, the cost of the airtime, the VAT charged and the total
         * bill.
         */
        std::cout << "Phone service: ";
        if(home_or_mobile ...) {
            std::cout << "home\n";
        } else {
            std::cout << "mobile, with package ";
            if(package ...) {
                std::cout << ... ;
            } else {
                ...
            }
        }
        std::cout << "Minute used: " << ... 
                  << "cost of airtime: " << ... ;
        /* Assignment:
         * VAT is charged at 12.5% for each customer.
         */
        vat = ... ;
        add vat to the previously defined 'something';
        std::cout << "VAT: " << ...
        bill = ... ;    // add vat
        add bill to the previously defined 'something';
        std::cout << "total: " << ... ;
    } // 10-iteration loop

    /* Assignment:
     * The program should also output the total bill and total VAT of the ten
     * (10) customers.
     */
    use 'something' to print out the following two values:
    std::cout << "Total bill of the ten customers is " << ...
              << "Total VAT of the ten customers is " << ... ;
}

Thanks man for your help.
Topic archived. No new replies allowed.