Question : C++ uninitialized local variable

Guys,

I'm just learning C++...and help would be GREATLY appreciated!!! Below is the code and the output errors I am receiving.

thanks!
Mike

Code:

// This program calculates an ISP customers bill based on package choice and hours used.
#include <iostream>
using namespace std;

int main()
{
      double      hours,            // Number of hours used
                  savingsB,      // Monthly savings for package B
                  savingsC,      // Monthly savings for package C
                  monthly;      // Monthly cost
      int            choice,            // Package choice
                  January, February, March, April, May, June, July, August, September, October, November, December;
      char      month;            // Month selection
                  

      // Display request for month
      cout << "\nPlease enter the month for the billing cycle: ";
      cin >> month;

      // Get hours and calculate monthly charge for Package A
            cout << "\nHow many hours did you use? ";
            cin >> hours;

            if (month = January && hours > 744)
            if (month = February && hours > 672)
            if (month = March && hours > 744)
            if (month = April && hours > 720)
            if (month = May && hours > 744)
            if (month = June && hours > 720)
            if (month = July && hours > 744)
            if (month = August && hours > 744)
            if (month = September && hours > 720)
            if (month = October && hours > 744)
            if (month = November && hours > 720)
            if (month = December && hours > 744)
            {
                  cout << "\nYou have entered an invalid amount of hours,\n"
                         <<      "please restart the program and try again.\n\n";
                  return 0;
            }

      // Display package options and read selection
      cout << "\nPlease select the number for your package.\n";
      cout << "\n1. Package A\n";
      cout << "\n2. Package B\n";
      cout << "\n3. Package C\n";
      cout << "\nEnter the correct number: ";
      cin >> choice;

      if (choice != 1)
      if (choice != 2)
      if (choice != 3)
      {
            // Produce error if valid choice isn't made and restart program
                  cout << "\nYou have made an invalid selection, please restart the program and try again.\n\n";
            return 0;
      }

      if (choice == 1)

            if (hours > 10)
                  monthly = ((hours - 10) * 2.00) + 9.95;
            else
                  monthly = 9.95;

                  // Display total due
                  cout << "\nYour total due is $" << monthly << "\n\n";
            
                  // Calculate saving with higher rate plans
                  if (hours >= 13)
                  {
                        savingsB = monthly - (((hours - 20) * 1.00) + 14.95);
                        cout << "If you upgrade to Package B you would save $" << savingsB << "\n\n";
                        
                        if (hours >= 16)
                        {
                              savingsC = monthly - 19.95;
                              cout << "If you upgrade to Package C you would save $" << savingsC << "\n\n";
                        }
                  }
      
      if (choice == 2)
      {
            // Get hours and calculate monthly charge for Package B
            cout << "\nHow many hours did you use? ";
            cin >> hours;

            if (hours > 744)
            {
                  cout << "\nYou have entered an invalid amount of hours,\n"
                         <<      "please restart the program and try again.\n\n";
                  return 0;
            }


            if (hours > 20)
                  monthly = 14.95 + ((hours - 20) * 1.00);
            else
                  monthly = 14.95;

                  // Display total due
                  cout << "\nYour total due is $" << monthly << "\n\n";

                  // Calculate saving with higher rate plans
                  if (hours >= 26)
                  {
                        savingsC = monthly - 19.95;
                        cout << "If you upgrade to Package C you would save $" << savingsC << "\n\n";
                  }
      }
      
      if (choice == 3)
      {
            // Get hours and calculate monthly charge for Package C
            cout << "\nHow many hours did you use? ";
            cin >> hours;

            if (hours > 744)
            {
                  cout << "\nYou have entered an invalid amount of hours,\n"
                         <<      "please restart the program and try again.\n\n";
                  return 0;
            }

            monthly = 19.95;

                  // Display total due
                  cout << "\nYour total due is $" << monthly << "\n\n";
      }
      return 0;
}

Output:

1>------ Build started: Project: ISP, Configuration: Debug Win32 ------
1>Build started 6/28/2010 10:55:33 AM.
1>InitializeBuildStatus:
1>  Creating "Debug\ISP.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  ISP - 24.cpp
1>c:\users\harrisms\documents\visual studio 2010\projects\isp\isp - 24.cpp(24): warning C4700: uninitialized local variable 'January' used
1>c:\users\harrisms\documents\visual studio 2010\projects\isp\isp - 24.cpp(25): warning C4700: uninitialized local variable 'February' used
1>c:\users\harrisms\documents\visual studio 2010\projects\isp\isp - 24.cpp(26): warning C4700: uninitialized local variable 'March' used
1>c:\users\harrisms\documents\visual studio 2010\projects\isp\isp - 24.cpp(27): warning C4700: uninitialized local variable 'April' used
1>c:\users\harrisms\documents\visual studio 2010\projects\isp\isp - 24.cpp(28): warning C4700: uninitialized local variable 'May' used
1>c:\users\harrisms\documents\visual studio 2010\projects\isp\isp - 24.cpp(29): warning C4700: uninitialized local variable 'June' used
1>c:\users\harrisms\documents\visual studio 2010\projects\isp\isp - 24.cpp(30): warning C4700: uninitialized local variable 'July' used
1>c:\users\harrisms\documents\visual studio 2010\projects\isp\isp - 24.cpp(31): warning C4700: uninitialized local variable 'August' used
1>c:\users\harrisms\documents\visual studio 2010\projects\isp\isp - 24.cpp(32): warning C4700: uninitialized local variable 'September' used
1>c:\users\harrisms\documents\visual studio 2010\projects\isp\isp - 24.cpp(33): warning C4700: uninitialized local variable 'October' used
1>c:\users\harrisms\documents\visual studio 2010\projects\isp\isp - 24.cpp(34): warning C4700: uninitialized local variable 'November' used
1>c:\users\harrisms\documents\visual studio 2010\projects\isp\isp - 24.cpp(35): warning C4700: uninitialized local variable 'December' used
1>ManifestResourceCompile:
1>  All outputs are up-to-date.
1>Manifest:
1>  All outputs are up-to-date.
1>LinkEmbedManifest:
1>  All outputs are up-to-date.
1>  ISP.vcxproj -> c:\users\harrisms\documents\visual studio 2010\Projects\ISP\Debug\ISP.exe
1>FinalizeBuildStatus:
1>  Deleting file "Debug\ISP.unsuccessfulbuild".
1>  Touching "Debug\ISP.lastbuildstate".
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:01.68
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Answer : C++ uninitialized local variable

Ok I botched this one. I didn't look at the variable declarations...  I assumed you were comparing strings, which is inaccurate from what I can see now. My apologies.

If we revert the code to your original post, you declared variables for each month (using the names of the months as the variable names), but you never gave the variables an initial value. I assume January is supposed to be 1, so continue assigning values for the rest of the variables:

    int            choice,            // Package choice
                  January = 1, February = 2, March = 3, April = 4, May = 5, June = 6, July = 7, August = 8, September = 9, October = 10, November = 11, December = 12;

The parts I said about the "ifs" and the equality operator remain, though. Ignore strcmp()--it is irrelevant to this situation. The modified code using the integer comparisons for the months would be as below.

Again, my apologies for jumping the gun and leading you down the wrong road  :)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
if ((month == January && hours > 744) ||
    (month == February && hours > 672) ||
    (month == March && hours > 744) ||
    (month == April && hours > 720) ||
    (month == May && hours > 744) ||
    (month == June && hours > 720) ||
    (month == July && hours > 744) ||
    (month == August && hours > 744) ||
    (month == September && hours > 720) ||
    (month == October && hours > 744) ||
    (month == November && hours > 720) ||
    (month == December && hours > 744))
{
    cout << "\nYou have entered an invalid amount of hours,\n"
        <<      "please restart the program and try again.\n\n";
    return 0;
}
Random Solutions  
 
programming4us programming4us