Local variable not initialized

This is a discussion on Local variable not initialized within the C Programming forums, part of the General Programming Boards category; Sorry im new here.. i cant seem to get the function working Code: void calc(int num) { int inch, rem_inch, ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    6

    Local variable not initialized

    Sorry im new here.. i cant seem to get the function working

    Code:
    void calc(int num)
    {
    	
    	int inch, rem_inch, feet;
    	
    	feet = inch/12;
    	rem_inch = inch % 12;
    
    
    	 printf("%d inches = %d feet %d inches\n", inch, feet, rem_inch);
    	
    }
    
    void main()
    {
    
    	
    	int i, inch;
    
    	
    
    	for(i=0;i<5;i++)
    	{
    	printf("\nPlease Enter Inches:");
    	scanf("%d", &inch);
    	
    
    	calc(inch);
    	
    	
    	}
    
    }
    help?? argh

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > feet = inch/12;
    > rem_inch = inch &#37; 12;
    Use the parameter you pass, not an uninitialised local variable.

    Also, main returns int - see the FAQ.
    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.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by countpuchi View Post
    Sorry im new here.. i cant seem to get the function working
    Could you tell us what it's supposed to be doing? If you give us something broken, we can't look at what it does and assume that's what you want. We have to assume what you are giving us is not what you want, since you wouldn't be asking for help otherwise.

    In short, what's the problem?

    help?? argh
    I'm guessing, but I think this is what you want:

    Code:
    #include <stdio.h>
    
    void calc(int inch)
    {
    	int rem_inch, feet;
    
    	feet = inch/12;
    	rem_inch = inch % 12;
    
    	printf("%d inches = %d feet %d inches\n", inch, feet, rem_inch);
    }
    
    int main(int argc, char *argv[])
    {
    	int i, inch;
    
    	for(i=0;i<5;i++)
    	{
    		printf("\nPlease Enter Inches:");
    		scanf("%d", &inch);
    		calc(inch);
    	}
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    6
    Sorry sorry about that..

    well i need to
    1) Using a for loop, do the following:
    2) Get the amount of inches from the user, 5 times. (store in an array 5 integer elements)
    3) For each element, call a function to convert the inches into foot. (1 foot is 12 inches). Pass the element to the function. Function prototype is given here:

    But even without storing using array its not working or am i missing something lol.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Did you try the code I provided?

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    6
    Yes i did.. and it helped thank you. Ok i got the program working, although its not looping anymore

    Code:
    #include<stdio.h>
    #include<math.h>
    
    
    void calc(int inch[5])
    {
    	
    	int rem_inch, feet;
    	
    	{
    	feet = inch[5]/12;
    	rem_inch = inch[5] &#37; 12;
    
    
    	 printf("%d inches = %d feet %d inches\n", inch[5], feet, rem_inch);
    	}
    }
    
    void main()
    {
    
    	
    	int i, inch[5];
    	
    	for(i=0;i<6;i++)
    	{
    	printf("\nPlease Enter Inches:");
    	scanf("%d", &inch[5]);
    	
    
    	calc(inch);
    	}
    
    }
    But if i input in 1 - 9 it will loop for 5 times, it wont loop if i input more then 2 digits.. Argh..

    p.s im quite new in C so.. what does int argc, char *argv[] do??

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    p.s im quite new in C so.. what does int argc, char *argv[] do??
    They contain the parameters passed to the program. See this FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    Incidentally, the same FAQ also tells you why not to use void main().

    Alternatively, you could just use
    Code:
    int main() {
        /* ... */
    
        return 0;
    }
    [edit]
    Code:
    scanf("&#37;d", &inch[5]);
    One small step backwards for a programmer . . . [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Change:

    Code:
    scanf("%d", &inch[5]);
    to this:

    Code:
    scanf("%d", &inch[i]);
    And change:

    Code:
    calc(inch);
    to this:

    Code:
    calc(inch[i]);
    Also, your loop condition is wrong. You should be looping < 5, not < 6. You only have 5 elements in your array. Starting index of an array is 0, not 1. So you go from 0 to 4.

    main() needs to return an int. Even if you declare it to be void, it will return an int (well actually the calling environment will wrestle one from main(), but that's a little more in depth of a topic). Just remember that main() is a special function, and you should get into the habbit of manually returning a value from it. argc and argv are just ways of obtaining command line arguments. You don't need to use them for this program.

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    6
    Okay i changed em, though the program crashed right after i input the numbers.

    And i got 2 warnings:

    warning C4047: 'function' : 'int *' differs in levels of indirection from 'int '
    warning C4024: 'calc' : different types for formal and actual parameter 1

    did i do something wrong?? Should i change the func prototype to inch[i]?? or just inch[5]

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Use the version of calc() I posted earlier.

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    6
    Yup i used the calc u posted earlier

    Code:
    int main()
    {
    
    	
    	int i, inch[5];
    	
    	for(i;i<5;i++)
    	{
    	printf("\nPlease Enter Inches:");
    	scanf("&#37;d", &inch[i]);
    	
    
    	calc(inch[i]);
    	}
    
    }

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    *sigh*

    I'm referring to this:

    Code:
    void calc(int inch)
    {
    	int rem_inch, feet;
    
    	feet = inch/12;
    	rem_inch = inch &#37; 12;
    
    	printf("%d inches = %d feet %d inches\n", inch, feet, rem_inch);
    }

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    6
    LoL Sorry if this is getting annoying.. cant help it im kinda slow and thank you for being patient with me.

    Okay i changed it (again) theres no warning or error its fine. Although its still crash right after i typed in the numbers. Could this crash caused by calc[i] in main() coz if its calc[5] it wont crash. Though im not sure myself.. *sigh*

    Code:
    void calc(int inch)
    {
    	
    	int rem_inch, feet;
    	
    	
    	feet = inch/12;
    	rem_inch = inch &#37; 12;
    
    
    	 printf("%d inches = %d feet %d inches\n", inch, feet, rem_inch);
    	
    }
    
    int main()
    {
    
    	
    	int i, inch[5];
    	
    	for(i;i<5;i++)
    	{
    	printf("\nPlease Enter Inches:");
    	scanf("%d", &inch[i]);
    	
    
    	calc(inch[i]);
    	}
    
    }

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    for(i=0;i<5;i++)

  15. #15
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    With all the "int main" advice floating around in this topic, you'd think someone would have gotten it right by now:
    Code:
    int main(void)
    If you do not use command line args and you want to be standards compliant, you do it that way and no other.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Local variable Question
    By WDT in forum C# Programming
    Replies: 2
    Last Post: 04-02-2009, 05:39 PM
  2. Should "static" variable always be local to a file?
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 06-22-2008, 11:51 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. Printing part of a local variable
    By Phan in forum C Programming
    Replies: 10
    Last Post: 09-26-2005, 05:51 PM
  5. return local variable
    By sangi in forum C Programming
    Replies: 17
    Last Post: 10-22-2004, 03:40 AM

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