#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Help with a C++ Homework problem!


    Hello all, I am having problems figuring out a C++ homework problem. He wanted us to include a flag controlled while loop - which is the part I'am struggling with. He wants an invalid out put for lower case options. I am using Microsoft VS 2012. The program is not crashing but when I get to the hours, it just keeps prompting the user to 'Enter hours" I think I have specified what my true and false scenarios are for the "while loops" My program compiles and builds I just cant get past the hours option. I am new so I am sure there are errors, I described this in as much detail as I could. Thank you in advance for yall's help!

    The problem description is :

    An Internet service provider has three different subscription
    packages for its customers:

    Package A: For $15 per month with 50 hours of access provided.
    Additional hours are $2.00 per hour over 50 hours.
    Assume usage is recorded in one-hour increments,

    Package B: For $20 per month with 100 hours of access provided.
    Additional hours are $1.50 per hour over 100 hours.

    Package C: For $25 per month with 150 hours access is provided.
    Additional hours are $1.00 per hour over 150 hours

    Assume a 30-day billing cycle.

    Code:
    //Internet Service Providers Program3
    
    #include <iomanip>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	//Variables
    	char pkg(0);
    	int hrs(0);
    	double total, charges(0);
    
    	//Input
    	cout<<"Enter the Internet Package you choose: "; //Select Package
    		cin>>pkg;
    
    		bool flag = true;
    			if(pkg == 'A'||pkg == 'B'|| pkg == 'C')
    			{
    				flag = true;
    			}
    			else  if(pkg == 'a' || pkg =='b' || pkg== 'c')
    			{
    				flag = false;
    			}
    
    				while (flag == true)
    				{
    					if(pkg == 'A'|| pkg == 'B'|| pkg == 'C')
    					cout<<"Enter hours: ";
    					cin>>hrs;
    				}
    				while (flag == false)
    				{
    					if(pkg == 'a' || pkg =='b' || pkg== 'c')
    					cout<<"Invalid package option: Enter valid package: ";
    					cin>>pkg;
    				}
    				//Plan A
    				if(pkg == 'A' && hrs>=50)
    				{
    					charges = 15.00*50;
    					total = charges + (hrs-50)*2.00;
    					cout<<"Your total for Plan A is: $ "<<total << "\n\n";
    				}
    				//Plan B
    				else if (pkg == 'B' && hrs>=100)
    				{
    					charges=20.00*100;
    					total = charges + (hrs-100)*1.50;
    					cout<<"Your total for Plan B is: $ "<<total << "\n\n";
    				}
    				//Plan C
    				else if (pkg == 'C' && hrs>=150)
    				{
    					charges=25.00*150;
    					total = charges + (hrs-150*1.00);
    					cout<<"Your total for Plan C is: $ "<<total
    					<<endl<<endl;
    				}
    			else;
    	return 0;
    		
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Code:
    			while (flag == true)
    				{
    					if(pkg == 'A'|| pkg == 'B'|| pkg == 'C')
    					cout<<"Enter hours: ";
    					cin>>hrs;
    				}
    				while (flag == false)
    				{
    					if(pkg == 'a' || pkg =='b' || pkg== 'c')
    					cout<<"Invalid package option: Enter valid package: ";
    					cin>>pkg;
    				}
    In the first while loop, you will enter it if flag == true and you will only exit it if flag gets changed to false. You never change the flag to false.

    Similarly, in the second while loop, you also never change the flag.

    PS

    Your indentation is out of control with everything marching off the right side of the screen. Keep items at the same level of indentation actually indented the same.

    Comments on this post

    • ericcooper agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by dwise1_aol
    Code:
    			while (flag == true)
    				{
    					if(pkg == 'A'|| pkg == 'B'|| pkg == 'C')
    					cout<<"Enter hours: ";
    					cin>>hrs;
    				}
    				while (flag == false)
    				{
    					if(pkg == 'a' || pkg =='b' || pkg== 'c')
    					cout<<"Invalid package option: Enter valid package: ";
    					cin>>pkg;
    				}
    In the first while loop, you will enter it if flag == true and you will only exit it if flag gets changed to false. You never change the flag to false.

    Similarly, in the second while loop, you also never change the flag.

    PS

    Your indentation is out of control with everything marching off the right side of the screen. Keep items at the same level of indentation actually indented the same.
    Okay thanks Dwise! Good to hear from you again. - Hopefully I am on the right track here but not sure... I tried to clean everything up with respect to the indentations. When I run the program the output after the "Enter hours: " prompt is says invalid package option , etc. I am stumped with this program, and have confused myself trying to figure out these while loops.

    "Enter the Internet Package you choose: A
    Enter hours: 59
    Invalid package option: Enter valid package:"

    Code:
    bool flag = true;
    			if(pkg == 'A'||pkg == 'B'|| pkg == 'C')
    			{
    				flag = true;
    			}
    			else if(pkg == 'a' || pkg =='b' || pkg== 'c')
    			{
    				flag = false;
    			}
    
    			while (flag)
    			{
    				if(flag == true)
    				{
    				cout<<"Enter hours: ";
    				cin>>hrs;
    				}
    				else;
    				{
    				cout<<"Invalid package option: Enter valid package: ";
    				cin>>pkg;
    				}
    			}
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Look at the red semicolon at the end of the else:
    Code:
    bool flag = true;
        if(pkg == 'A'||pkg == 'B'|| pkg == 'C')
        {
            flag = true;
        }
        else if(pkg == 'a' || pkg =='b' || pkg== 'c')
        {
            flag = false;
        }
    
        while (flag)
        {
            if(flag == true)
            {
                cout<<"Enter hours: ";
                cin>>hrs;
            }
            else;
            {
                cout<<"Invalid package option: Enter valid package: ";
                cin>>pkg;
            }
        }
    That tells the compiler that the else case is an empty statement, that there is no code associated with the else. That means that that the code I highlighted in blue will always execute.

    You want to remove that extraneous semicolon after the else, the red one.

    Do you understand what you were erroneously telling the compiler?

    My question for the entire program is, what is it supposed to do? Is it run one time, accept as input the package plan validating that input, accept as input the number of hours used, and then output the amount owed? Or are you supposed to do it multiple times? Or are you supposed to repeatedly request a package until the user gives you a valid one? Or are you supposed to accept a valid package plan and then loop repeatedly asking for hours and outputting billings until some entry for quitting is entered whereupon you exit the loop?

    Basically, my question is: where are you supposed to use a while loop? From your description, I'm very uncertain of what is required.

    Also, first write a list, in English, of steps that the program is supposed to go through. That should be your first step in all designing all your programs.

    PS

    Here is how I reformatted your original code:
    Code:
    #include <iomanip>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        //Variables
        char pkg(0);
        int hrs(0);
        double total, charges(0);
    
        //Input
        cout<<"Enter the Internet Package you choose: "; //Select Package
        cin>>pkg;
    
        bool flag = true;
    
        if(pkg == 'A'||pkg == 'B'|| pkg == 'C')
        {
            flag = true;
        }
        else  if(pkg == 'a' || pkg =='b' || pkg== 'c')
        {
            flag = false;
        }
    
        while (flag == true)
        {
            if(pkg == 'A'|| pkg == 'B'|| pkg == 'C')
            cout<<"Enter hours: ";
            cin>>hrs;
        }
    
        while (flag == false)
        {
            if(pkg == 'a' || pkg =='b' || pkg== 'c')
            cout<<"Invalid package option: Enter valid package: ";
            cin>>pkg;
        }
    
        //Plan A
        if(pkg == 'A' && hrs>=50)
        {
            charges = 15.00*50;
            total = charges + (hrs-50)*2.00;
            cout<<"Your total for Plan A is: $ "<<total << "\n\n";
        }
        //Plan B
        else if (pkg == 'B' && hrs>=100)
        {
            charges=20.00*100;
            total = charges + (hrs-100)*1.50;
            cout<<"Your total for Plan B is: $ "<<total << "\n\n";
        }
        //Plan C
        else if (pkg == 'C' && hrs>=150)
        {
            charges=25.00*150;
            total = charges + (hrs-150*1.00);
            cout<<"Your total for Plan C is: $ "<<total
            <<endl<<endl;
        }
        else;
    
        return 0;
    }
    I normally set my tabs to 4 and then have the editor use spaces instead of tab characters. If I used tab characters, then when I'd post here or anywhere outside of my editor the tab size would jump up to the default value of 8. Visual Studio defaults to using tab characters, but I believe that can be changed. I cannot think of any benefit to tab characters over spaces, but if you continue to use tab characters that would be no problem as long as you don't add extra tabs as you were doing.
    Last edited by dwise1_aol; September 16th, 2013 at 06:20 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by dwise1_aol
    Look at the red semicolon at the end of the else:
    Code:
    bool flag = true;
        if(pkg == 'A'||pkg == 'B'|| pkg == 'C')
        {
            flag = true;
        }
        else if(pkg == 'a' || pkg =='b' || pkg== 'c')
        {
            flag = false;
        }
    
        while (flag)
        {
         if(flag == true)
         {
             cout<<"Enter hours: ";
             cin>>hrs;
          }
            else;
            {
                cout<<"Invalid package option: Enter valid package: ";
                cin>>pkg;
            }
        }
    That tells the compiler that the else case is an empty statement, that there is no code associated with the else. That means that that the code I highlighted in blue will always execute.

    You want to remove that extraneous semicolon after the else, the red one.

    Do you understand what you were erroneously telling the compiler?

    My question for the entire program is, what is it supposed to do? Is it run one time, accept as input the package plan validating that input, accept as input the number of hours used, and then output the amount owed? Or are you supposed to do it multiple times? Or are you supposed to repeatedly request a package until the user gives you a valid one? Or are you supposed to accept a valid package plan and then loop repeatedly asking for hours and outputting billings until some entry for quitting is entered whereupon you exit the loop?

    Basically, my question is: where are you supposed to use a while loop? From your description, I'm very uncertain of what is required.

    Also, first write a list, in English, of steps that the program is supposed to go through. That should be your first step in all designing all your programs.
    Sorry Dwise - So the program is supposed run one time, accept as input the package plan validating that input, accept as input the number of hours used, and then output the amount owed?

    However, if the input package selection is 'a' 'b' 'c' (lower case) then it is suppose to throw back an "invalid input" Which is where the while loop comes in, because it wants 'A' 'B' or 'C' (upper case selection)

    That makes sense about the red semicolon, I now understand why my program was always executing that statement. I have gone through and wrote down what this program is to do step by step and am just lost when it comes to incorporating this loop.

    1. Gather package selection
    2. validate uppercase selection; if lower case throw error and gather selection again.
    3. Gather hours of access.
    4. Based on hours determine amount due.(30 day billing cycle)
    5. If hours exceed the base then determine additional charges for each plan.

    I am not sure how to declare (or if i even need to) the false and true statements and then based off that, how to tell the program if the input is valid being uppercase characters to go on to calculate the data. Im sure i have it all out of order. I hope this makes more sense. MY apologies again..

    Here is what i have edited so far - It is recongnizing the correct plan and calculating an amount due, but then goes on to prompt for more hours...

    Enter the package you'd like: A
    Enter hours of access: 59
    Your total for Plan A is: $ 768

    Enter hours of access:


    Code:
    //Validate Package Selection
    	bool flag = true;
    
    	if (flag = 'a'||'b'||'c') //False Selection
    	{
    		flag = false;
    	}
    	else(flag = 'A'||'B'||'C'); //True Selection
    	{
    		flag = true;
    	}
    	while (flag)
    	{
            if (flag == true)
    	{
    		cout<<"Enter hours of access: ";
    		cin>>hrs;
    	}
    	else if(flag == false)
    	{
    	        cout<<"Invalid Selection; Please enter new plan: ";
    		cin>>pkg;
    	}
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by ericcooper
    However, if the input package selection is 'a' 'b' 'c' (lower case) then it is suppose to throw back an "invalid input" Which is where the while loop comes in, because it wants 'A' 'B' or 'C' (upper case selection)

    . . .

    1. Gather package selection
    2. validate uppercase selection; if lower case throw error and gather selection again.
    3. Gather hours of access.
    4. Based on hours determine amount due.(30 day billing cycle)
    5. If hours exceed the base then determine additional charges for each plan.
    OK, then. Clearly the loop needs to be applied to the package selection and not to the calculation of the charges. So far, your code has had that reversed.

    Using flags has been around since the earliest days of programming and, I believe, even before that. I believe it started in digital electronics (eg, in the switching circuits of the 1930's as the phone company was developing automatic dialing) where the voltage levels on certain control lines between devices were used to synchronize the passing of data. The engineers seemed to be borrowing from the Navy's method of signalling between ships by raising flags, since they would describe the use of those control lines as "raising and lowering a flag."

    Then in programming, "flag variables" would be "raised or lowered" (AKA "set to true or reset to false") depending on a particular condition that was found and then used to "signal" the rest of the program of that condition. Or a better way of thinking about flag variables could be that the program is writing a short note to itself so that it can remember what had happened. Using a flag instead of just redoing the test is better because 1) you avoid the extra work, 2) the condition that was detected could have gone away in the meantime, and 3) the test could have side-effects that you do not want to create more than once (eg, in the test you read from the keyboard and tested the value you read, so redoing the test would read from the keyboard again).

    So, you could set up the while loop to run as long as the flag is false (assuming that flag == true means you have valid input) and initialize the flag to false so that you will run the loop at least once. Input the package plan, test it, and set the flag accordingly. Then test the flag to see what you should display to the user. Make it so that you will exit the loop if a valid package input was given and not exit if it's invalid.

    So you see, in using a flag in a loop, there are or can be at least three things involved:
    1. You make the loop part of the test for remaining in the loop.
    2. You can use the flag within the loop to decide other things to do or not do.
    3. With the flag being part of the conditional test for the loop, you need to be able to change the flag in order to ever exit the loop -- this is what you've been consistently violating so far.

    Think about that approach and think about the reasons for taking such an approach. To paraphrase from an old (to you, at least) movie and TV show about law school (Paper Chase), you need to learn to think like a programmer.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by dwise1_aol
    OK, then. Clearly the loop needs to be applied to the package selection and not to the calculation of the charges. So far, your code has had that reversed.

    Using flags has been around since the earliest days of programming and, I believe, even before that. I believe it started in digital electronics (eg, in the switching circuits of the 1930's as the phone company was developing automatic dialing) where the voltage levels on certain control lines between devices were used to synchronize the passing of data. The engineers seemed to be borrowing from the Navy's method of signalling between ships by raising flags, since they would describe the use of those control lines as "raising and lowering a flag."

    Then in programming, "flag variables" would be "raised or lowered" (AKA "set to true or reset to false") depending on a particular condition that was found and then used to "signal" the rest of the program of that condition. Or a better way of thinking about flag variables could be that the program is writing a short note to itself so that it can remember what had happened. Using a flag instead of just redoing the test is better because 1) you avoid the extra work, 2) the condition that was detected could have gone away in the meantime, and 3) the test could have side-effects that you do not want to create more than once (eg, in the test you read from the keyboard and tested the value you read, so redoing the test would read from the keyboard again).

    So, you could set up the while loop to run as long as the flag is false (assuming that flag == true means you have valid input) and initialize the flag to false so that you will run the loop at least once. Input the package plan, test it, and set the flag accordingly. Then test the flag to see what you should display to the user. Make it so that you will exit the loop if a valid package input was given and not exit if it's invalid.

    So you see, in using a flag in a loop, there are or can be at least three things involved:
    1. You make the loop part of the test for remaining in the loop.
    2. You can use the flag within the loop to decide other things to do or not do.
    3. With the flag being part of the conditional test for the loop, you need to be able to change the flag in order to ever exit the loop -- this is what you've been consistently violating so far.

    Think about that approach and think about the reasons for taking such an approach. To paraphrase from an old (to you, at least) movie and TV show about law school (Paper Chase), you need to learn to think like a programmer.
    So, Dwise - this program was due today, but I have been working on it and thinking about what you said. what do you think?
    It is working for me :) - is this thinking like a programmer?

    I set the flag = false so when it sees the false statement it drops out. If true, it keeps looping. I took out the lowercase letters because essentially, the only letters we want to see are 'A' ,, 'B' & 'C' (uppercase). - which is my true. Anyway, I got it working but i'm sure there is a better way. We learned about switch statements, which would have made this easier lol.

    Code:
    	bool flag= false;
    	while(!flag)
    	{
    		cout<<"Enter Selection: ";
    		cin>>pkg;
    
    		if(pkg== 'A' || pkg =='B'|| pkg== 'C')
    		{
    			flag = true;
    		}
    		else
    			flag = false;
    	}
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Basically, yes. I haven't used "bang" (exclamation point) with a bool, but I assume that it works and since you tested it then it must. And I assume that when flag == false then you display an error message telling the user that the input was invalid; from a user-interface perspective, just repeating a prompt without any explanation can be confusing.

    The main point of "thinking like a programmer" is to view a problem in a methodical manner, like having you write down the general steps, and then seeing in those steps the control structures that you would need.

    Gotta hit the road now.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by dwise1_aol
    Basically, yes. I haven't used "bang" (exclamation point) with a bool, but I assume that it works and since you tested it then it must. And I assume that when flag == false then you display an error message telling the user that the input was invalid; from a user-interface perspective, just repeating a prompt without any explanation can be confusing.

    The main point of "thinking like a programmer" is to view a problem in a methodical manner, like having you write down the general steps, and then seeing in those steps the control structures that you would need.

    Gotta hit the road now.

    Thanks Dwise for all your help, input and insight. We'll talk soon, have a good one.

IMN logo majestic logo threadwatch logo seochat tools logo