need help fixing this program

This is a discussion on need help fixing this program within the C++ Programming forums, part of the General Programming Boards category; im trying to run this program but i keep getting the error "the variable 'b' is being used without being ...

  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
    21,313
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,266
    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
    21,313
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21