C++ Code Blocks not running full program?

I have this program  and it will not run full it stops after the user enters the gigs. Why isn't it running fully? 

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

    cout << fixed << showpoint;

    cout << setprecision(2);

    int pack;

    int gigs;

    const double base1 = 39.99;

    const double base2 = 59.99;

    const double base3 = 69.99;

    double total1;

    double total2;

    double total3;

    cout << "Select a subscription package" << endl;

    cout << "1.Package A: For 39.99 a month, 4 gigabytes are provided. Additional cost per gigabyte is $10." << endl << endl;

    cout << "2.Package B: For 59.99 a month, 8 gigabytes are provided. Additional cost per gigabyte is $5." << endl << endl;

    cout << "3.Package C: for 69.99 per month, unlimited data is provided." << endl << endl;

    cout << "4.Quit" << endl << endl;

    cout << "Please enter a package between 1 and 3.";

    cin >> pack;

    cout << "Please enter number of additional gigabytes used.";

    cin >> gigs;

    total1 = base1+10*gigs;

    total2 = base2+5*gigs;

    total3 = base3;

    if(pack==1)

    {

         cout << "The total amount due is " << total1 << endl;

    }

   else if(pack==2)

        {

         cout << "The total amount due is " << total2 << endl;

        }

   else if(pack==3)

     {

   cout << "The total amount due is " << total3 << endl;

     }

  else if(pack==4)

      {

       cout << "You have quit." << endl;

      }

return 0;

}

3 Answers

Relevance
  • 1 month ago

    I've run it on Code::Blocks 17.12 with the bundled MinGW compiler.

    It compiles with no warnings (I always use -Wall -Wextra and -pedantic to help find typos and to work on fixing bad habits), so congratulations on that! It also runs just fine when a valid input is given, except for option 4 asking for a gigabtye value that's not going to be used. Maybe you don't even need the "extra gigabytes" on option 3 either.

    Option 4 make little sense if you're not going to enclose most of that main function in a while or do-while loop. I added this by

    .... 1. Adding a "do {" line before the first cout<< line.

    .... 2. Adding a "} while (pack != 4);" line just before "return 0;"

    .... 3. Using the Plugins>Source code formatter tool to clean up the indentation.

    You still need to fix the prompt for gigabytes when it's not needed. The simplest way to do that is to only ask for the value if it's going to be used. You'll only need one total variable, which you can set to a zero value if nothing is to be printed at the end of the loop.

    One thing that will cause erratic behavior in your code is when the user enters an invalid integer, such as "16.5" of "seven". The cin>> operator will set an error in the stream, cin.good() will be false, and no further input operations will even be attempted until you issue a cin.clear() call. Usually, you'll also want to discard the rest of the line that had the error.

    .... if (!cin.good())

    .... {

    .... .... cout << "Invalid input\n";

    .... .... cin.clear();

    .... .... cin.ignore(9999, '\n'); // not the best way to do this, but the official way is UGLY

    .... .... continue; // skip rest of loop body

    .... }

    • EddieJ
      Lv 7
      1 month agoReport

      Two hours ago I asked him to show us what input he was using.

  • VP
    Lv 7
    1 month ago

    Make those IFs look like this: if (pack == 3)

    Don't jam things together... it makes it easier to read.

    Also, tell us what input you're using.

    For extra credit -- Assume your user is an idiot and enters a '5'. What should your program do?

    --- edit ---

    Your program, as written (by you), also worked for me. It just wasn't as pretty as mine. 😜

    Nice job, though.

    Also, I've always used the "fixed" and "setprecision" statements on the COUT I needed them on, like so:

    ...

    ... if (pack == 1)

    {

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

    ... ... cout << "The total amount due is " << total1 << endl;

    ... }

  • 1 month ago

    Sorry Eddie, what is wrong is it terminates the program before it runs fully, it ends it after I input the gigs.

    • EddieJ
      Lv 7
      1 month agoReport

      Well, like I said, it didn't do that for me. What, exactly did you input?

Still have questions? Get your answers by asking now.