Do while issues

The following code seems to not work...after the first do while completes it goes on to the second one although the condition isn't met...i tried putting breaks in enclosing it in brackets...i don't know what else to do


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
#include <iostream>
#include <iomanip>
      using namespace std;
int main()
{
   int account_number;
   int minutes;
   char service;
   double amount_due;
   double day_min;
   double night_min;
   double regular;
   double premium;

               cout << fixed << showpoint;
               cout << setprecision(2);

       cout<<"Please enter your account number."<< endl;
       cin>> account_number;

       cout<<"Please enter account type.(R or r for regular service, P or p for premium service.)"<<endl;
       cin>>service;

	   
	   
	   do 
	   {
			   cout << "Enter the amount of minutes used." << endl;
               cin >> minutes;
	   
	   
	   

	  if (minutes <= 50)
      {
               amount_due=10.00;
               cout << "Amount due:$" << amount_due << endl;
               cout << "Account Number:" << account_number << endl;
			   cout << "Minutes used:" << minutes << "" <<endl;
               break;

       }
       
	   
	  else ( minutes > 50 );
       {
                amount_due= ((minutes-50 ) * .20 + 10.00);
                cout << "Amount due:$" << amount_due << endl;
                cout << "Account Number:" << account_number << endl;
				cout << "Minutes used:" << minutes << "" <<endl;
                break;

        }
	   
	   }
	  
	   while(service= 'r' || 'R' ) ;
	   
	   // this is where is goes wrong
	   
	   
	   do
	   {
		   cout<< "How many minutes were used during the day?";
           cin >> day_min;

           cout<< "How many minutes were used during the night?";
           cin >>night_min;

            if(day_min <= 75)
                      {
                             amount_due=25.00;
                             cout << "Amount due for day minutes:$" << amount_due << endl;
                             cout << "Account Number:" << account_number << endl;
							 cout << " Day minutes used:" << day_min << "" <<endl;
							 break;
                                                
                      }
                                          

 

			else if (day_min > 75)
                      {
                             amount_due = ((day_min - 75) *.10)+ 25.00;
                             cout << "Amount due for day minutes:$" << amount_due << endl;
                             cout << "Account Number:" << account_number << endl;
							 cout << " Day minutes used:" << day_min << "" <<endl;
							 break;
                                                                          

                      }
                                          

                                         

			else if (night_min <= 100)
                      {

                             amount_due=25.00;
                             cout << "Amount due for night minutes:$" << amount_due << endl;
                             cout << "Account Number:" << account_number << endl;
							 cout << " Night minutes used:" << night_min << "" <<endl;
							 break;

                      }
                                        

			else
                      {
                             amount_due=amount_due = ((night_min - 100) * .05)+ 25.00;
                             cout<<"Amount due for night minutes:$"<<amount_due<<endl;
                             cout << "Account Number:" << account_number << endl;
							 cout << " Night minutes used:" << night_min << "" <<endl;
                             break;                                            

                      }
                                          
                                      
	   
	   }
	   while ( service = 'p' || 'P');
	   

       
	                                
                                      
	cin.get();
	cin.get();
	return 0;
}
Do...while loops are guaranteed to execute at least once. If you want only one of your two do...while loops to execute (but not the other one), try an
1
2
if(some_condition) {...} 
else if (some_other_condition) {...}

statement instead.
Last edited on
i understand what you are say but my teacher of mine wants us to use do..while loops as the primary loop, (we already made the same code with if else statements and while loops)
then I would suggest enclosing the if{} else if{} statements within a do...while loop that terminates if the user enters 'q' or some char other than 'r' or 'p', or is that not allowed either?

Last edited on
Yeah, the way do/while loops work is basically it says "Okay, do this then test the expression for true or false, if true do the loop again but if false then go to the next line after the while expression". Where as the while loops tests the expression first and if it is true then executes the block, otherwise it skips over the loop.

Either use if statements or make minor changes to the code and use while loops instead if you don't need it to execute before the expression test.
Last edited on by closed account z6A9GNh0
@atropos- i guess ill do it that way i mean technically i am using a do...while loop...thanks a lot

@BHXSpecter- yea thanks
Topic archived. No new replies allowed.