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

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Thumbs up C++ help with a coded program


    Hi C++ geniuses! I am stuck with this program that was assigned as a homework. I will post the question along side with my coded program. Here is the question as follows:
    An Internet service provider has three different subscription packages for its customers:
    Package A: For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour.
    Package B: For $14.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour.
    Package C: For $19.95 per month unlimited access is provided.
    Write a program that calculates a customer s monthly bill. It should ask which package
    the customer has purchased and how many hours were used. It should then display the total amount due.
    Input Validation: Be sure the user only selects package A, B, or C. Also, the number of hours used in a month cannot exceed 744.


    My code is as follows:
    #include <iostream>
    using namespace std;

    int main()
    {
    char package;
    double total_amount=0;
    int hours;

    cout << "Select a package: " << endl;
    cout << "Package\t\tCost\t\tHours Provided\t\tExtra Hours" << endl;
    cout << "A\t\t$9.95\t\t10\t\t\t$2.00 per hour" << endl;
    cout << "B\t\t$14.95\t\t20\t\t\t$1.00 per hour" << endl;
    cout << "C\t\t$19.95\t\tUnlimited\t\tUnlimited" << endl;

    cout << "Enter the package purchased:";
    cin >> package;
    while(package!= 'A' && package!='a' && package!='B' && package!='b'&&
    package!='C' && package!='c')
    {
    cout <<"Error! You must select package A, B, or C. ";
    cout << "Enter the package purchased: ";
    cin >> package;
    }

    if (package == 'A' || package == 'a')
    {if (hours <= 10)
    total_amount = 9.95;
    else
    total_amount = 9.95+((hours-10)*2);
    }
    if(package == 'B' || package == 'b')
    {if (hours <= 20)
    total_amount = 14.95;
    else
    total_amount= 14.95+ ((hours-20)*1);
    }
    if(package == 'C' && package == 'c')
    {total_amount=19.95;
    }

    cout << "Enter the number of hours used:";
    cin>>hours;
    while(hours < 0 || hours > 744)
    {
    cout << "Error! Hours cannot be negative or exceed 744. You must enter appopriate hours. ";
    cout << "Enter the number of hours used. ";
    cin >> hours;
    }

    if (package == 'A' || package == 'a')
    {if (hours <= 10)
    total_amount = 9.95;
    else
    total_amount = 9.95+((hours-10)*2);
    }
    if(package == 'B' || package == 'b')
    {if (hours <= 20)
    total_amount = 14.95;
    else
    total_amount= 14.95+ ((hours-20)*1);
    }
    if(package == 'C' && package == 'c')
    {total_amount=19.95;
    }

    system("pause");
    return 0;
    }


    For, my first and second para coding, I have used the while loop input validation and the third para, I have used the calculations which happen to be the actual coding. That is where I am not getting it to run or debug. does it have to be nested ? & how would I do that ? Please help. Also, since this is a project we were asked to do a psuedocode and flowchart. I am soo confused. Any ideas? Help will be appreciated :))
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,614
    Rep Power
    1945
    Please use code tags when posting code; this will keep the code format/indention and make it easier to read.

    Furthermore, if you get any error messages, please post these too.

    Why do you have two "blocks" calculation the total amount? the block between selecting package and number of hours should not be there. In there, you calculate with hours of which hasn't been assigned any value yet.

    pseudocode is more or less coding with logic instead of syntax, short example:
    If Package A
    total = packageprice1
    If Package B
    total = packageprice2

    If you don't know what a flowchart is, I suggest you make a search about it in google.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Use code tags. HTML strips out extra white space and so obliterates code's indentation and hence its formatting, making it unreadable. Code tags keep that from happening. Readable code gets read; unreadable code gets ignored or, at best, severely flamed.

    Retrieving your indentation via the Reply button:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char package;
    	double total_amount=0; 
    	int hours;
    
    	cout << "Select a package: " << endl;
    	cout << "Package\t\tCost\t\tHours Provided\t\tExtra Hours" << endl;
    	cout << "A\t\t$9.95\t\t10\t\t\t$2.00 per hour" << endl;
    	cout << "B\t\t$14.95\t\t20\t\t\t$1.00 per hour" << endl;
    	cout << "C\t\t$19.95\t\tUnlimited\t\tUnlimited" << endl;
    
    cout << "Enter the package purchased:";
    cin >> package;
    while(package!= 'A' && package!='a' && package!='B' && package!='b'&& 
    	  package!='C' && package!='c')
    	{
        cout <<"Error!  You must select package A, B, or C. ";
    	cout << "Enter the package purchased: "; 
        cin >> package;
    	}
    
    if (package == 'A' || package == 'a')
    	{if (hours <= 10)
    		total_amount = 9.95;
    	else 
    		total_amount = 9.95+((hours-10)*2);
    	}
    if(package == 'B' || package == 'b')
    	{if (hours <= 20)
    		total_amount = 14.95;
    	else 
    		total_amount= 14.95+ ((hours-20)*1);
    	}	
    if(package == 'C' && package == 'c')
    	{total_amount=19.95;
    	}
    
    cout << "Enter the number of hours used:";
    cin>>hours;
    while(hours < 0 || hours > 744)
    	{
    	cout << "Error! Hours cannot be negative or exceed 744. You must enter appopriate hours. ";
    	cout << "Enter the number of hours used. ";
    	cin >> hours;
    	}
    
    if (package == 'A' || package == 'a')
    	{if (hours <= 10)
    		total_amount = 9.95;
    	else 
    		total_amount = 9.95+((hours-10)*2);
    	}
    if(package == 'B' || package == 'b')
    	{if (hours <= 20)
    		total_amount = 14.95;
    	else 
    		total_amount= 14.95+ ((hours-20)*1);
    	}	
    if(package == 'C' && package == 'c')
    	{total_amount=19.95;
    	}
    
    system("pause");
    return 0;
    }
    Now that is some really crappy formatting. Who taught you to screw your code up so severely? You're defeating the purpose of indentation.

    Pick an indentation style and apply it consistently. I highly recommend the Allman style, which you can read about here: http://en.wikipedia.org/wiki/Indent_style#Allman_style.

    Comments on this post

    • jakotheshadows agrees
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    Hey. This is the error iam getting. Run-Time Check Failure #3 - The variable 'hours' is being used without being initialized.

    Here is the output debug error

    'help.exe': Loaded 'C:\Users\Nelson\Documents\Visual Studio 2010\Projects\help\Debug\help.exe', Symbols loaded.
    'help.exe': Loaded 'C:\WINDOWS\SysWOW64\ntdll.dll', Cannot find or open the PDB file
    'help.exe': Loaded 'C:\WINDOWS\SysWOW64\kernel32.dll', Cannot find or open the PDB file
    'help.exe': Loaded 'C:\WINDOWS\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
    'help.exe': Loaded 'C:\WINDOWS\SysWOW64\msvcp100d.dll', Symbols loaded.
    'help.exe': Loaded 'C:\WINDOWS\SysWOW64\msvcr100d.dll', Symbols loaded.
    Run-Time Check Failure #3 - The variable 'hours' is being used without being initialized.
    'help.exe': Loaded 'C:\WINDOWS\SysWOW64\apphelp.dll', Cannot find or open the PDB file
    'help.exe': Loaded 'ImageAtBase0x49e00000', Loading disabled by Include/Exclude setting.
    'help.exe': Unloaded 'ImageAtBase0x49e00000'
    The program '[2700] help.exe: Native' has exited with code 0 (0x0).
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > Hey. This is the error iam getting. Run-Time Check Failure #3 - The variable 'hours' is being used without being initialized.
    So initialise it then!

    So find where you have
    int hours;

    and make it
    int hours = 0;

    Or you might want to look at why you copy/pasted
    if (package == 'A' || package == 'a') ... 20+ more lines
    TWICE in the code

    Once BEFORE you input hours (where the error message comes from), and again after you have input hours.
    Last edited by salem; April 13th, 2013 at 12:45 AM.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  10. #6
  11. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,614
    Rep Power
    1945
    Did you read my last post?

IMN logo majestic logo threadwatch logo seochat tools logo