Thread: Undeclared?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    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?
    Last edited by Shazarul; 11-21-2011 at 11:47 AM.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Put your letters within single quotes.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    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() {
        // ...
    }

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    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;
    }
    Last edited by Shazarul; 11-21-2011 at 09:08 PM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >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.
    Last edited by whiteflags; 11-21-2011 at 10:26 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    > 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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    OK, but that just means the OP doesn't know that debugging is not building. Which is awful for him.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'jmp' undeclared
    By Wertmanzzz in forum Game Programming
    Replies: 2
    Last Post: 03-30-2007, 11:46 AM
  2. cout undeclared/cin undeclared
    By akbigchillin in forum C++ Programming
    Replies: 6
    Last Post: 04-24-2006, 04:06 PM
  3. undeclared?
    By seal in forum C Programming
    Replies: 2
    Last Post: 08-30-2005, 12:30 AM
  4. undeclared?
    By firefly in forum C++ Programming
    Replies: 9
    Last Post: 06-20-2005, 08:35 AM
  5. 'pch' undeclared
    By reRanger in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2004, 07:21 PM