You are using a version without Ads of this website. Please, consider donating:

### 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

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include 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
 ``123456`` ``````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:
 ``123456789101112131415161718`` `````` 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!" <
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 :)

 ``1234567891011121314151617`` ``````// 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.

 ``123456`` ``````// 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990`` ``````#include #include #include 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" <> 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,"<
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.

 ``12345678910111213`` ``````#include 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.

You are using a version without Ads of this website. Please, consider donating: