Phone Plan Comparison

I am in a beginning C++ class and for our program we have to allow the user to enter two cell phone plans including its minutes allowed, and monthly payment. The output will be which plan is better and how much the user will save yearly. Theres no rollover minutes and the over charge is .35 cents. Loops should be used to solve the problem. Also i cannot allow the user to input negative quantities for the monthly fee, minutes allowed, or minutes used.

This is the code I have so far, but im stuck on the loop to be able to enter in the minutes the user uses each month.

This is not my whole program but its as far as i could get

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
#include <iostream>
using namespace std;

int main ()
{

	int planA, planB, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12;
	double costA, costB;

	// do while loops to make sure no negative numbers are entered
	do
	{
		cout << "Enter in the cost for Plan A: $";
		cin >> planA;
	}
	while(planA <= 0);
	
	// do while loops to make sure no negative numbers are entered
	do
	{
		cout << "Enter the monthly minutes allowed for Plan A: ";
		cin >> costA;
	}
	while(costA <= 0);
	
	// do while loops to make sure no negative numbers are entered
	do 
	{
		cout << "Enter in the cost for Plan B: $";
		cin >> planB;
	}
	while(planB <= 0);
	
	// do while loops to make sure no negative numbers are entered
	do
	{
		cout << "Enter the monthly minutes allowed for Plan B: ";
		cin >> costB;
	}
	while(costB <= 0);
	
	
	//this is where im stuck. i know this is not right, but im not sure
	//what loop to use 
	for (int i = 0; i >= m12; m1++)
		cout >>m1>>endl;
		cin >> m1 >> m2 >> m3 >> m4 >> m5 >> m6 >> m7 >> m8 >> m8 >> m9
                    >> m10 >> m11 >> m12;
	//the loop must be able to read in each input by the user for each month
	
system("pause");
return 0;	
}
Last edited on
1
2
3
4
5
6
int months[12] = {0};

for (unsigned i = 0; i < 12; ++i) {
 cout << "Enter minutes used for month " << i << ":";
 cin >> months[i];
}
Last edited on
thank you very much! at least i know i was in the right direction. but why did you use

unsigned i = 0?

we havent learned that yet so im just curious
int can be both positive and negative. Since I know that in this instance it'll never be a negative I use an unsigned (which is unsigned int) to express this. Just makes the code a bit easier to understand.
oh okay i see what your saying.

and for the calculations could i use unsigned int for the overage cost in a loop as well to keep from having to write so many lines of code for each month since there is no rollover minutes allowed. ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

	int months[12] = {0};
	int overageA[12] = {0};
	int overageB [12]

    for (unsigned i = 1; i <= 12; ++i) {
                  cout << "Enter the minutes used for month " << i << ":";
                  cin >> months[i];
                      if (months[i] > costA) {
                         overageA[12] = ((months[i] * .35) + costA));
                         overageB[12] = ((months[i] * .35) + costB));
                         {
                      else {
                           cout << " You have no over charges for this month!" <<endl;
                           }


Yes. You seem to be getting close to what you need. But you don't need to keep a cost of each month do you? A running total would be better? In which case, you don't even need to store the values of each month.

The idea is to store as little information as possible, and to write as little amount of complex code as possible. While keeping it easy to read and understand :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// You should call your variables planCostA, and monthlyMinutesA etc
float runningTotalA = planA * 12; // initial value is the yearly cost
float runningTotalB = planB * 12;
unsigned minutes = 0;

for (unsigned i = 0; i < 12; ++i) {
 cout << "Enter minutes used for month " << i << ":";
 cin >> minutes;

 if (minutes > costA) {
  runningTotalA += minutes * 0.35f; 
 } 
 if (minutes > costB) {
  runningTotalB += minutes * 0.35f;
 }

}


On a side note, in your code that would likely crash. When you define an array you define it with the maximum number of objects it can hold. int myArray[12]; will hold a maximum of 12 objects. The indexes for these objects will be valid from 0-11.

1
2
3
4
5
6
// create an array of 12 ints
int myArray[12] = {0}; // = {0} means set everything to 0

myArray[0] = 2; // valid
myArray[11] = 3; // valid
myArray[12] = 3; // invalid. 12 is not a valid index. This will compile but likely crash 

Last edited on
Man, you're awesome! Thank you so much!!! I think i have it now, if you don't mind would you look over it and see if it looks decent.

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
#include <iostream>
#include <string>
#include <limits>

using namespace std;

int main()
{
	int monthlymins = 0, overA = 0, overB = 0, month = 0, minA, minB;
	double planA, planB, costA = 0, costB = 0, totaloverA = 0, totaloverB, yearlyA = 0, yearlyB = 0;
	
	
	cout << "Welcome to WireMore. Here you will be able to compare two plans, enjoy!.\n";
	cout <<"Remember, there are no rollover minutes allowed, over minutes will be charged\n";
	cout <<"0.35 cents per minute you go over" <<endl;
	
	do
	{
		cout << "Please enter your monthly price for Plan A: $";
		cin >> planA;
		
		cout << "Enter the number of minutes allowed monthly for Plan A: ";
		cin >> minA;
	}while(planA <= 0 || minA <= 0 );
	
	do
	{
		cout << "Please enter your monthly price for Plan B: $";
		cin >> planB;
		
		cout << "Enter the number of minutes allowed monthly for Plan B: ";
		cin >> minA;
	}while(planA <= 0 || minA <= 0 );
	
		cout << "Please enter the number of minutes you used each month for the past 12 months:\n";
		
			for (month = 1; month <=12; ++month )
			{
				do
				{
				cout << "Month  " << month << ":  ";
				cin >> monthlymins;
				}while (monthlymins, 0);
				
					if (monthlymins > minA)
					{
						overA += (monthlymins - minA);
                  }
					
					if (monthlymins > minA)
					{
						overB += (monthlymins - minB);
					}
					
						}
						
		costA = planA * 12;
		totaloverA = overA * .35;
		
		costB =  planB * 12;
		totaloverB = overB * .35;
		
		yearlyA = costA + totaloverA;
		yearlyB = costB + totaloverB;
		
		cout.setf(ios::fixed);
		cout.setf(ios::showpoint);
		cout.precision(2);
		
			if(costA < costB)
			{
				cout << "The plan best suited for you is Plan A,"<<endl;
				cout <<"Which totals yearly to: $" << yearlyA <<endl;
				cout << " You will save,  " << ( yearlyB - yearlyA ) << "  ,by using Plan A instead!!";
				
            }
				else
			{
					cout << "The plan best suited for you is Plan B, "<<endl;
					cout <<"Which totals yearly to: $" << yearlyB <<endl;
					cout << " You will save " << (yearlyA-yearlyB) << " by using Plan B instead!!\n";
				}
			
   cout << "Thanks for using the Comparison Computer.\n";  
   cout << "Press ENTER to quit.";
  cin.get();
  getchar();
 
  return 0;
  }
Yes, I am awesome :P

Some issues:
Line 32: your usin minA, not minB
Line 33: wrong variables in the loop, A instead of B
Line 43: Invalid , operator
Okay, lol. I have it now. Thanks alot for your help!!
Good good, apart from that I think everything else looked reasonable. I'm taking it, this is an assignment for a beginner programming class?
Yes. Only our second program assignment, lol.
Yea, no worries. You could do a heap more around the validation of user input; but this is out of the scope of a beginner assignment. Good luck with it.
I wish i knew what you were talking about, lol. Today we just started learning about Functions..thats gonna be fun...blahh..
Functions are fairly easy. Just think of it as a way to break up useful pieces of code so you can use them multiple times without having to rewrite them :)
So its kinda like being able to only write a calc one time instead of repeating it over and over. as long as you declare it in your parameters?
Kind of,
Here is a simple example.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

void printString(string parameter) {
  std::cout << "You asked to print: " << parameter << std::endl;
}

int main() {

  printString("Hello");
  printString("World");

  return 0;
}


While you can see this example serves no purpose, imagine if the function you had written would load all of the contents of a file into a variable for you. Having it as a function called readFile() would be ideal.
Last edited on
Topic archived. No new replies allowed.