Trouble with a Do While loop

//Dan Masella Calculating the most cost efficient phone payment plan for the customer
#include<iostream>
#include<iomanip>
#include<stdlib.h>
using namespace std;

int main()

{


//Declare Variables
char name[20];
double dayminutes;
double nightminutes;
double regularprice;
double premiumprice;
double premiumdaycost;
double premiumnightcost;
char choice;

do {
//Clears the screen for the next customer
system("CLS");

//Input
cout<<"What is your name: ";
cin>>name;
cout<<"Enter the daytime minutes: ";
cin>>dayminutes;
cout<<"Enter the nighttime minutes: ";
cin>>nightminutes;

//Process
//Calculates the price for the regular plan

if (dayminutes+nightminutes > 50)
{
regularprice = (((dayminutes+nightminutes)-50) * .2) + 10;
}
else
{
regularprice = 10;
}


//Calculates the price for the premium plan

if (dayminutes > 75)
{
premiumdaycost = ((dayminutes * .1)-(75*.1));
}
else
{
premiumdaycost = 0;
}



if (nightminutes > 100)
{
premiumnightcost = ((nightminutes * .05)-(100*.05));
}
else
{
premiumnightcost = 0;
}


premiumprice = (premiumdaycost + premiumnightcost) + 25;




//Output
cout<<endl<<"Regular plan cost: $";
cout<< fixed;
cout.precision(2);
cout<<regularprice;

cout<<endl<<"Premium plan cost: $";
cout<< fixed;
cout.precision(2);
cout<<premiumprice;

if(regularprice < premiumprice)
{
cout<<endl<<name<<", the regular plan is more cost efficient for you"<<endl;
}

if(regularprice > premiumprice)
{
cout<<endl<<name<<", the premium plan is more cost efficient for you"<<endl;
}

if(regularprice == premiumprice)
{
cout<<endl<<name<<", either plan would be suitable for you"<<endl;
}

cout<<endl<<"Is there another customer to process: (y/n) ";
cin>>choice;
if(choice=='n'||choice=='N')
{
exit(1);
}

} while(choice == 'y'||choice=='Y');

return 0;
}
Last edited on
I feel there is a problem with your for loops;I have not understood why ur using that <=1.
Does this help?

1
2
3
4
do {
   /* Do stuff */
   /* Ask user if they want to continue */
} while( /* user said 'yes' */ );


Can you translate that algorithm to code?
Hmm, I just added that in as a counter maybe that's what's screwing me up?
In your case all it means is that you do the same calculation twice with the given input (which is of course redundantly unnecessary, but is also harmless, except perhaps to your grade).

Consider my previous reply. Think algorithmically. What are the logical steps you want to perform?

1. Ask the user for name and usage times;
2. Compute which plan is best given the user's input;
3. Output the best plan to the user;
4. Ask the user if they want to continue;
5. Repeat steps 1-5 if they want to continue;
6. Exit program.
Ah, I see my redundancy now with the for loop. Only problem I'm having now is figuring out how to get the while function to read a character such a "y"
I'm not an expert but this is how I usually do it and it works every time...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
do{
     clrscr();

     //This is your code starting
     //from cout<<"What is your name: ";
     //...then you can add something
     //like this at the end...

     cout<<"\n\nWould you like to try again? [y/n]";
     cin>>response;
     if(response=='n' || response=='N')
          {
          exit(1);
          }
}while(response=='y' || response=='Y');


Just add header file <stdlib.h> to use function exit(); and <conio.h> to use clrscr();. You can also try using gets(name); instead of using cin.get(name, 15);. I think that's the cause of the redundancy. Again, I am not an expert, but I think that's where the problem is coming.

Goodluck!
Thank you! I've now gotten it to run the program and repeat only once, the only problem is for some reason it skips through the name input and jumps to the input for the daytime minutes on the second loop.

I wonder if it's because I couldn't get the clrscr(); commmand to work. I kept getting an error say the clrscr() identifier was not found
Last edited on
NOT use ||! Else it will return ever true...
Got the clear screen to work had to use system("CLS"); instead but still for some reason it skips over the name input right to daytime minute input
Last edited on
Worked it out, and got everything to work. Thank you very much everybody the help is much appreciated! :D
Topic archived. No new replies allowed.