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

Thread: Help looking over

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Help looking over

    A friend asked me to get someone to look over this but the guy is offline, so I'm asking around here... here's his project:


    Project for my intro to programming class, someone wanna read over it and see if they're are any glaring holes?
    Code:
    //Travis C     Page 226 Problem 23 Internet Provider
    //October 9th, 2012
    //
    //
    /*
    DESIGN BLOCK
    
    */
    
    #include <iostream>
    using namespace std;
    int main ()
    {
    int choice;
    int hours;
    double total;
    double A = 9.95;
    double B = 14.95;
    double C = 19.95;
    double D = 2.00;
    double E = 1.00;
    
    cout << " Travis' Internet Service Provider Subscription\n";
    cout << "1. $9.95 per month. 10 hours access. Additional Hours are $2.00\n";
    cout << "2. $14.95 per month. 20 hours access. Additional hours are $1.00\n";
    cout << "3. $19.95 per month. Unlimited access.\n";
    cout << "Please enter the subscription number of your choice: ";
    cin >> choice;
    if (choice >=1 && choice <=3)
    {
    cout << "How many hours would you access: ";
    cin >> hours;
     
    switch (choice)
    {
    case 1:
    {
    total = A + (hours - 10)*D;
    break;
    }
    case 2:
    {
    total = B + (hours - 20)*E;
    break;
    }
    case 3:
    {
    total = C;
    break;
    }
    default:
    cout<<"You entered a wrong value!"<<endl;
    cout << "The valid choices are 1 through 3. \n Run the program again.";
    }
    cout << "The total for your subcription is:$ "<<total<<endl;
    }
     
    else if (choice !=3)
    {
    cout << "The valid choices are 1 through 3. \n Run the program again.";
    }
    return 0;
    }
    
    /*
    Output
     Travis' Internet Service Provider Subscription
    1. $9.95 per month. 10 hours access. Additional Hours are $2.00
    2. $14.95 per month. 20 hours access. Additional hours are $1.00
    3. $19.95 per month. Unlimited access.
    Enter your choice: 1
    How many hours would you access: 12
    The total for your subscription is:$ 13.95
    Press any key to continue . . .
    
    */

  2. #2
    Join Date
    Jan 2009
    Posts
    596

    Re: Help looking over

    Quote Originally Posted by cwarden12 View Post
    Project for my intro to programming class, someone wanna read over it and see if they're are any glaring holes?
    Code:
    ...
    int main ()
    {
    ...
    double A = 9.95;
    ...
    double D = 2.00;
    ...
    cout << " Travis' Internet Service Provider Subscription\n";
    cout << "1. $9.95 per month. 10 hours access. Additional Hours are $2.00\n";
    cout << "Please enter the subscription number of your choice: ";
    cin >> choice;
    ...
    
    cout << "How many hours would you access: ";
    cin >> hours;
    ...
    
    switch (choice)
    {
    case 1:
    {
    total = A + (hours - 10)*D;
    break;
    }
    ...
    (Irrelevant code deleted)

    What happens if you choose option 1) and only have 1 hour usage?

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,355

    Re: Help looking over

    One thing that you need to do is to format your code, e.g., to indent it.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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

    Re: Help looking over

    As Peter pointed out, your calculations don't work.

    You don't really need the default in the switch statement as you're already weeding out invalid choices in the if statement.

    Typically, you'll verify input in a while loop, repeating until valid data is entered, rather than exiting the program and asking the user to rerun it.

    Next time, please use code tags.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center