1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

My program keeps closing after typing in minutes

Discussion in 'C, C++, C#' started by Godziwa, Sep 24, 2014.

  1. Godziwa

    Godziwa Regular Member

    Joined:
    Jun 18, 2014
    Messages:
    446
    Likes Received:
    220
    I have a problem with my code. So my homework asked this:
    A mobile phone service provide rhas three different subscription packages for its customers:
    Package A: For $39.99 per month 450 minutes are provided. Additional minutes are $0.45 per minute.
    Package B: For $59.99 per month 900 minutes are provided. Additional minutes are $0.40 per minute.
    Package C: For $69.99 per month unlimited minutes are provided.
    Write a program that calculates a customer's monthly bill. It should ask which package the customer has purchased and how many minutes were used. It should then display the total amount due.
    Input validation: Be sure the user only selects package A, B, or C.
    And this is my code for it:
    HTML:
    /*
    
    1. Set variables (chars, int, etc) for hours and fees.
    2. Ask user to select between A, B, or C.
    3. Ask user to input time.
    Also set a maximum amount of time for each case and setting a maximum amount of time for the     month.
    5.Use case switch for options
    6.calculate the customers bill for the month apprioprately.
    
    */
    
    
    #include <iostream> 
    #include <iomanip> 
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
    const double packageA = 39.99;//set variables for all three. They don't change. 
    const double packageB = 59.99;
    const double packageC = 69.99;
    char choices = ' '; // use "char" for the packages(choices)
    int time = 0; // set time as integer. Since some may be decimals, I use double. 
    double letter = 0.0; //use "letter" for whatever letter they choose. 
    cout << "Read choices below and select choice."<<endl;
    cout << "A.$39.99 per month gets 450 minutes. Additional minutes are $0.45 per minute." << endl;
    cout << "B.$59.99 per month gets 900 minutes. Additional minutes are $0.40 per minute." << endl;
    cout << "C.$69.99 per month gets you unlimited access" << endl;
    
    
    cout << "Select A, B, or C" << endl;
    cin >> letter;
    
    if (choices == 'A' || choices == 'B' || choices == 'C')//using switch case
    {
    
        cout << "Enter minutes:" << endl;//ask user to input time
        cin >> time;
    
    
        if (time>0 && time<43829)// 43829 is the max amount of minutes in a month. 0 is the least they person can have. If it fits the requirements, then it can continue. 
    
        {
    
            switch (choices)
            {
            case 'A':
                if (time<450)
                    letter = packageA;// if the time is less than required. Then no extra charge. 
                else
                    letter = ((time - 450)*0.45) + packageA;// if it exceeds maximum minutes and 45 cents is charged. Same for all cases below except its respective amount is charged. 
                break;
            case 'B':
                if (time<900)
                    letter = packageB;
                else
                    letter = ((time - 900)*.40) + packageB;
                break;
            case 'C':
                letter = packageC;// if not, then package C and no equation since time is unlimited. It is a one time fee for all time used. 
                break;
    
    
            default: cout << "Total amount due is: $" << letter << endl; // give total amount charged based on information entered. 
            }
        }
        system("pause");
        return 0;
    }
    
    }
    My problem is when I run it, it closes after I select a letter. If I was to select A, it closes automatically. I get this message after it closes:
    'ConsoleApplication7.exe' (Win32): Loaded 'C:\Users\Prince\Documents\Visual Studio 2013\Projects\ConsoleApplication7\Debug\ConsoleApplication7.exe'. Symbols loaded.
    'ConsoleApplication7.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
    'ConsoleApplication7.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
    'ConsoleApplication7.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
    'ConsoleApplication7.exe' (Win32): Loaded 'C:\Program Files\Bitdefender\Bitdefender 2015\active virus control\Avc3_00259_008\avcuf32.dll'. Cannot find or open the PDB file.
    'ConsoleApplication7.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp120d.dll'. Cannot find or open the PDB file.
    'ConsoleApplication7.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file.
    The program '[3476] ConsoleApplication7.exe' has exited with code 0 (0x0).

    The problem I have is after I run the program, it closes after I input the minutes.

     
  2. thejesus

    thejesus Registered Member

    Joined:
    Sep 29, 2008
    Messages:
    76
    Likes Received:
    22
    I believe the problem is that you have used the wrong variable type when asking for the package.

    From your code:
    Code:
    double letter = 0.0; //use "letter" for whatever letter they choose. 
    ........
    cout << "Select A, B, or C" << endl;
    cin >> letter;
    When the user enters "A", which is a string or char, to the variable "letter", which is a double, it crashes. It crashes because "A" cannot be a double.

    You need to change it to something like this:
    Code:
    cin >> choices;
    I hope this helps.
     
    • Thanks Thanks x 1