Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Variable 'maxHours' being used without being initialized

So I’m developing a program where I need to get information about what month the user has purchased for a subscription package cellphone plan. Based on the hours of the chosen month and the number of hours the user used within that given month I need to calculate their total costs.
My problem arises when I try to use the month that the user had given and assign that month the maxHours in it. For example, January has 31 days therefore it has 744 hours. If they enter a value larger than maxHours, so in this case anything > 744, I want the program to terminate.

When using visual studio i get the error "The variable ‘maxHours’ is being used without being initialized. Although I initialized it following the conditions of the if statements.

#include <iostream>

using namespace std;
int main()
{
    char package;
    int month;
    float hours;
    int maxHours;
    float extraHoursA = 2.0, extraHoursB = 1.0;
    float costA, costB, costC;

    //Supplies the user with the details of each package option
    cout << "Which package plan did you purchase? Your options are:" << endl;
    cout << "Package A: For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour." << endl;
    cout << "Package B: For $14.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour." << endl;
    cout << "Package C: For $19.95 per unlimited access is provided." << endl << endl;

    //Asks for the chosen package option
    cout << "Please enter the letter of your chosen package: ";
    cin >> package;

    //Validates the chosen package option
    if (package != 'a' && package != 'A' && package != 'b' && package != 'B' && package != 'c' && package != 'C')
    {
        cout << "You have entered an invalid option, please try again.";
        return 0;
    }

    //Confirms with user the chosen package option
    else if (package == 'a' || package == 'A');
    {
        cout << endl << "You chose package A. For $9.95 per month 10 hours of access were provided. Additional hours were $2.00 per hour." << endl << endl;
    }

    //Asls for the month in which the package was used
    cout << "Which month did you utilze your plan for? Please enter the month: ";
    cin >> month;
    if (month <= 0 || month > 12)
    {
        cout << endl << "You have entered an invalid month, please try again.";
        return 0;
    }
    if (month == 1 || month ==  3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 11)
    {
        maxHours = 744;
    } 
    else if (month == 4 || month == 6 || month == 9 || month == 11)
    {
        maxHours = 720;
    }
        cout << endl << "You utilized the plan in the month of " << month << endl;

    cout << "How many hours did you use? Please enter the amount of hours you utilized ";
    cin >> hours;


    if (hours > maxHours)
    {
        cout << "Error: Hours used cannot exceed " << maxHours << " in the " << month << "th month!";
            return 0;
    }
    if (hours < 0)
    {
        cout << "Error: Hours used cannot be less than 0";
        return 0;
    }

    return 0;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

maxHours is initialized under the if and else if block. If neither of the condition is met, maxHours will stay un-initialized. That is the reason you are getting this error. If you try to print the value of a variable that is not initialized, then you will get a garbage value.

Better initialize it with int number

At the declaration, do the initialization:

int maxHours = 0;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading