Like Tree1Likes
  • 1 Post By anduril462

"the variable ' ' is being used without being initialized" HELP a noob

This is a discussion on "the variable ' ' is being used without being initialized" HELP a noob within the C Programming forums, part of the General Programming Boards category; Question: so it says that "the variable hgtInches is being used without being initialized. At the top I initialized it, ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    "the variable ' ' is being used without being initialized" HELP a noob

    Question: so it says that "the variable hgtInches is being used without being initialized. At the top I initialized it, "int hgtInches". What went wrong?



    Code:
    #include<stdio.h>
    
    int main()
    
    {
    
    	char firstName;
    	int hgtInches;
    	float hgtFeet;
    
    	hgtFeet=INCH_TO_FEET;
    
    	printf("Please enter your first name:\n");
    	scanf("%s", &firstName);
    	fflush(stdin);
    
    	printf("Please enter your height in inches:\n");
    	scanf("%d", &hgtInches);
    
    	printf("%s, you are %.2f\n", firstName, hgtFeet);
    
    	return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,486
    Please provide all relevant code in the future. I'm guessing you forgot to include the definition of INCH_TO_FEET, which is probably a macro defined something like:
    Code:
    #define INCH_TO_FEET    (hgtInches / 12)
    #defines are just text substitution, not a funciton call, so here you're using hgtInches before you read it in. Also, it would be really great if you posted a line number for the error and highlighted it for us in your post, so we can more easily help you.

    Continuing on, defining a macro to use an explicit variable is not a good idea. You should do:
    Code:
    #define INCH_TO_FEET(x)    ((x) / 12.0)
    ...
    hgtFeet = INCH_TO_FEET(hgtInches);
    That way you can reuse it for other calculations.

    Or better yet, make it a regular function. This allows better type checking, parameter checking, etc, and any decent, optimizing compiler will in-line it if necessary, making it as fast as your macro:
    Code:
    double inches_to_feet(int inches)
    {
        return inches / 12.0;
    }
    EDIT: removed incorrect "lf" suffix from floating point literals.
    Last edited by anduril462; 09-01-2011 at 01:51 PM.
    stahta01 likes this.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Also, your incorrect use of fflush()
    Cprogramming.com FAQ > Why fflush(stdin) is wrong
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,570
    Quote Originally Posted by sstteeweenn View Post
    Code:
    	char firstName;
    Code:
    	
    	scanf("%s", &firstName);
    You declare firstName as an single char; then you try to use it as a char array.

    I suggest something like the below to declare firstName to hold a 12 letter name.

    Code:
            #define MAX_LENGTH_NAME 12
    	char firstName[MAX_LENGTH_NAME+1];
    FYI: I always use +1 to add space for the trailing C-String zero byte.
    I use C-String to mean char array with trailing zero byte.

    Tim S.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by stahta01 View Post
    I suggest something like the below to declare firstName to hold a 12 letter name.

    Code:
            #define MAX_LENGTH_NAME 12
    	char firstName[MAX_LENGTH_NAME+1];
    FYI: I always use +1 to add space for the trailing C-String zero byte.
    I use C-String to mean char array with trailing zero byte.

    Tim S.
    I don't:
    Code:
    for( x = 0; x < MAX_LENGTH_NAME; x++ )
        firstName[ x ] = tolower( firstName[ x ] );
    Otherwise, I have to start doing +1 on everything, or start using <=, which I'm not a fan of.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,570
    Quote Originally Posted by quzah View Post
    Code:
    for( x = 0; x < MAX_LENGTH_NAME; x++ )
        firstName[ x ] = tolower( firstName[ x ] );
    Your code is not a good example of why it is a bad idea; because it would work without any change in it, using either method.
    FYI: Mine would just not copy the trailing zero byte; while yours would.
    Note: I do seem to be in the minority doing the plus 1; but, you need a better example.
    The fgets() function is a case where my method is not as good as yours.

    Tim S.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    thanks for the input, but it's still giving me the "variable being used without being initialized". I'm starting out so, I'll wait to use the for, while, etc. later on when we cover it.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    What is your current code?
    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

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by sstteeweenn View Post
    Question: so it says that "the variable hgtInches is being used without being initialized. At the top I initialized it, "int hgtInches". What went wrong?



    Code:
    #include<stdio.h>
    
    int main()
    
    {
    
    	char firstName;
    	int hgtInches;
    	float hgtFeet;
    
    	hgtFeet=INCH_TO_FEET;
    
    	printf("Please enter your first name:\n");
    	scanf("%s", &firstName);
    	fflush(stdin);
    
    	printf("Please enter your height in inches:\n");
    	scanf("%d", &hgtInches);
    
    	printf("%s, you are %.2f\n", firstName, hgtFeet);
    
    	return 0;
    
    }
    What line number does your compiler report? As everyone has said you are either not posting the entire code or you are using a non-standard compiler.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,570
    Quote Originally Posted by sstteeweenn View Post
    thanks for the input, but it's still giving me the "variable being used without being initialized". I'm starting out so, I'll wait to use the for, while, etc. later on when we cover it.
    Quote Originally Posted by anduril462 View Post
    Please provide all relevant code in the future. I'm guessing you forgot to include the definition of INCH_TO_FEET
    The best guess is anduril462; which is the INCH_TO_FEET macro is wrong.
    But, without more information all we can do is guess.

    The error message normally gives the line number with the error.

    Read and answer anduril462 questions in all of his replies.

    Tim S.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    	printf("Please enter your height in inches:\n");
    	scanf("%d", &hgtInches);
    
    	printf("%s, you are %.2f\n", firstName, hgtFeet);
    And by what magic does the hgtInches get transformed to hgtFeet between these two statements?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++: Variable not initialized within "while" loop
    By iceburg_2487 in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2011, 10:17 AM
  2. Replies: 8
    Last Post: 11-29-2008, 10:45 PM
  3. system("pause") in C#? (warning: noob question)
    By Wintersun in forum C# Programming
    Replies: 20
    Last Post: 03-28-2008, 02:26 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

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