# Undeclared?

• 11-21-2011
Shazarul
Undeclared?
Hey I've got this problem 'uninitialized local variable 'weight' used' . . The error is in red

Code:

``` #include <iostream> #include <iomanip> #include <cmath> void computeAlpha(); void computeBravo(); void computeCharlie(); void computeDelta(); using namespace std; int main() {     char choice;        double weight;         cout << setprecision(0) << fixed << "Please enter the weight (w) in gm : ";     cin >> weight;     cout << "\n\nPlease enter the destination code (A to D) : ";     cin >> choice;     if ( choice == 'A' )         computeAlpha();        else if ( choice == 'B' )         computeBravo();     else if ( choice == 'C' )         computeCharlie();     else if ( choice == 'D' )         computeDelta();     else         cout << "\n\nInvalid Choice!!!" << endl;         system("pause");     return 0; } void computeAlpha() {     double weight, PostageCost1, PostageCost2, PostageCost3;     const double r1A=2.75, r2A=23.00;     PostageCost1 = (1/2*r1A)+(1/4*r2A);     PostageCost2 = r1A+(1/2*r2A);     PostageCost3 = r1A+r2A;         if ( weight <= 300 )         cout << setprecision(2) << fixed << "\n\nPostage Cost = \$ " << PostageCost1                                 << "\n\nRemark = LIGHT" << endl << endl;         else if ( weight <= 600 )             cout << "\n\nPostage Cost = \$ " << PostageCost2                 << "\n\nRemark = AVERAGE" << endl << endl;         else             cout << "\n\nPostage Cost = \$ " << PostageCost3                 << "\n\nRemark = HEAVY" << endl << endl;             return; } void computeBravo () {     double weight, PostageCost1, PostageCost2, PostageCost3;     const double r1B=1.50, r2B=10.00;     PostageCost1 = (1/2*r1B)+(1/4*r2B);     PostageCost2 = r1B+(1/2*r2B);     PostageCost3 = r1B+r2B;     if ( weight <= 300 )         cout << setprecision(2) << fixed << "\n\nPostage Cost = \$ " << PostageCost1                                 << "\n\nRemark = LIGHT" << endl << endl;         else if ( weight <= 600 )             cout << "\n\nPostage Cost = \$ " << PostageCost2                 << "\n\nRemark = AVERAGE" << endl << endl;         else             cout << "\n\nPostage Cost = \$ " << PostageCost3                 << "\n\nRemark = HEAVY" << endl << endl;             return; } void computeCharlie () {     double weight, PostageCost1, PostageCost2, PostageCost3;     const double r1C=2.50, r2C=12.50;     PostageCost1 = (1/2*r1C)+(1/4*r2C);     PostageCost2 = r1C+(1/2*r2C);     PostageCost3 = r1C+r2C;     if ( weight <= 300 )         cout << setprecision(4) << fixed << "\n\nPostage Cost = \$ " << PostageCost1                                 << "\n\nRemark = LIGHT" << endl << endl;         else if ( weight <= 600 )             cout << "\n\nPostage Cost = \$ " << PostageCost2                 << "\n\nRemark = AVERAGE" << endl << endl;         else             cout << "\n\nPostage Cost = \$ " << PostageCost3                 << "\n\nRemark = HEAVY" << endl << endl;             return; } void computeDelta () {     double weight, PostageCost1, PostageCost2, PostageCost3;     const double r1D=2.95, r2D=33.50;     PostageCost1 = (1/2*r1D)+(1/4*r2D);     PostageCost2 = r1D+(1/2*r2D);     PostageCost3 = r1D+r2D;     if ( weight <= 300 )         cout << setprecision(2) << fixed << "\n\nPostage Cost = \$ " << PostageCost1                                 << "\n\nRemark = LIGHT" << endl << endl;         else if ( weight <= 600 )             cout << "\n\nPostage Cost = \$ " << PostageCost2                 << "\n\nRemark = AVERAGE" << endl << endl;         else             cout << "\n\nPostage Cost = \$ " << PostageCost3                 << "\n\nRemark = HEAVY" << endl << endl;             return; }```
Problem is my print screen need to be
"Please enter the weight (w) in gm : 700
Please enter the destination code (A to D) : A

Postage Cost = \$27.65

Remark = Heavy"

I can't figure out how to get my weight declared in my void menu, can someone point out?
• 11-21-2011
manasij7479
Put your letters within single quotes.
• 11-21-2011
JohnGraham
The weight variable you've defined in computeAlpha(), computeBravo() etc. is not the same as the one you've defined in main() - it refers to a different memory location, and so has a random value in your compute functions. Fix the problem by removing it from all your functions and declaring it once outside them all:

Code:

```// This is the only place you define weight. double weight; int main() {     // ... }```
• 11-21-2011
rags_to_riches
While JohnGraham is correct, I disagree with using the global variable solution. It's a bad habit to get into, and it's pretty simple given your program to do the right thing and declare and initialize the weight variable in main pass it to your functions as an argument.
• 11-21-2011
Shazarul
Blargh this void is difficult . . I decided to do just a normal if else but I'am having problems with else, I can build without any problem . . But if i enter any other alphabets beside A-D, i get an error stating "The variable 'r1' is being used without initialized" Can someone u point out where the error is?

Code:

``` #include <iostream> #include <iomanip> #include <cmath> #include <string> using namespace std; int main() {     char choice;        double weight, PostageCost, r1, r2;     string Remark;         cout << setprecision(0) << fixed << "Please enter the weight (w) in gm : ";     cin >> weight;     cout << "\n\nPlease enter the destination code (A to D) : ";     cin >> choice;     if ( choice == 'A' )         r1 = 2.75, r2 = 23.00;     else if ( choice == 'B' )         r1 = 1.50, r2 = 10.00;     else if ( choice == 'C' )         r1 = 2.50, r2 = 12.50;     else if ( choice == 'D' )         r1 = 2.95, r2 = 33.50;     else         cout << "\n\nInvalid Choice!!!";     if ( weight <= 300 )         {    PostageCost = (1/2*r1)+(1/4*r2),         Remark = "LIGHT";    }     else if ( weight <= 600 )         {    PostageCost = (r1+(1/2*r2)),         Remark = "AVERAGE";    }     else         {    PostageCost = (r1+r2),         Remark = "HEAVY";    }     cout << setprecision(2) << fixed << "\n\nPostage Cost = \$" << PostageCost << endl << endl;         system("pause");     return 0; }```
• 11-21-2011
whiteflags
>Blargh this void is difficult . . I decided to do just a normal if else but I'am having problems with else,

The problem is not with else.

> I can build without any problem . . But if i enter any other alphabets beside A-D, i get an error stating "The variable 'r1' is being used without initialized"

You should be getting a warning, not an error. An error stops builds while a warning doesn't.

> Can someone u point out where the error is?

Actually, your thinking is correct. If you don't pick a letter between A or D, r1 and r2 don't get assigned values...

Code:

```if ( choice == 'A' )         r1 = 2.75, r2 = 23.00;     else if ( choice == 'B' )         r1 = 1.50, r2 = 10.00;     else if ( choice == 'C' )         r1 = 2.50, r2 = 12.50;     else if ( choice == 'D' )         r1 = 2.95, r2 = 33.50;     else         cout << "\n\nInvalid Choice!!!";```

Invalid choice!!! Then what? You calculate postage anyway, using r1 and r2. Using an uninitialized variable causes undefined behavior, which is why you get the warning. You should ask the user to make another correct choice. Use a loop to keep asking until the user makes a good choice. Likewise, if the user doesn't choose a good weight, the program has problems calculating good results. You need to make sure the program is using acceptable data first.
• 11-22-2011
Elysia
Quote:

Originally Posted by whiteflags
> I can build without any problem . . But if i enter any other alphabets beside A-D, i get an error stating "The variable 'r1' is being used without initialized"

You should be getting a warning, not an error. An error stops builds while a warning doesn't.

I do believe the problem stems from that if you run some code inside Visual Studio and you try to use an uninitialized variable, it will immediately break with an error, saying variable X is not initialized.
• 11-22-2011
whiteflags
OK, but that just means the OP doesn't know that debugging is not building. Which is awful for him.