CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Do While Issues

  1. #1
    Join Date
    Apr 2012
    Posts
    3

    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
    Code:
    #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;
    }

  2. #2
    Join Date
    Aug 2009
    Posts
    440

    Re: Do While Issues

    In both the if and else of the first do while loop, you have a break. This breaks out of the do while loop.

  3. #3
    Join Date
    Apr 2012
    Posts
    3

    Re: Do While Issues

    no for example when the user inputs an r or R it calculates the bill then goes on to the condition as if it was premium service

  4. #4
    Join Date
    Aug 2009
    Posts
    440

    Re: Do While Issues

    Right...that is what a do while loop. It executes at least once. Then, depending on the condition it may execute again or fall to the next statement. You probably want a do while loop before the calculations. In this loop you get the input from the user and verify it is valid.

  5. #5
    Join Date
    Apr 2012
    Posts
    3

    Re: Do While Issues

    yea i figured it out thank you

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Do While Issues

    while(service= 'r' || 'R' ) ;

    That condition is always true for several reasons.

  7. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Do While Issues

    [ Moved thread ]
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured