Thread: need help fixing this program

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    10

    Question need help fixing this program

    im trying to run this program but i keep getting the error "the variable 'b' is being used without being initialized". i really need help
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    int a,b;
    cout <<" a = " <<a <<" and b = " <<b;
    cout << "\n " <<b << "\n" <<a;
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, yes. It is probably a warning rather than an error, but it is indeed true that b (and a) is used without being initialised. What are you trying to do in this program?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by truetrini20 View Post
    im trying to run this program but i keep getting the error "the variable 'b' is being used without being initialized". i really need help
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    int a,b;
    cout <<" a = " <<a <<" and b = " <<b;
    cout << "\n " <<b << "\n" <<a;
    return 0;
    }
    you're displaying the value of both variables without ever assigning a value to them. C++ does not automatically initialize (assign a default value) to anything, so you have to do this yourself.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    10
    this question is part of an assignment, so i know i have to fix it so that it runs but im not sure

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by truetrini20
    this question is part of an assignment, so i know i have to fix it so that it runs but im not sure
    Without knowing what you are trying (or are supposed) to do, we cannot say any more than your compiler has told you. What I can do is give you an example of initialisation:
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        int a = 1, b = 2;
        cout << " a = " << a << " and b = " << b;
        cout << "\n " << b << "\n" << a;
        return 0;
    }
    Notice that I have indented the code to make it more readable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM