Thread: Variable troubles

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    13

    Variable troubles

    I'm really new to C, and this isn't working, can someone tell me what I'm doing wrong? I'm having trouble with the variables, which I had in global before, but my teacher wants them to be local to main. My floats are
    Code:
    float T, speed;
    And this program works fine when "float T, speed;" is directly under the include statements.

    Code:
    /* Calculating speed of sound from temperature 6/18/12 (CSC-150) */
    #include <stdio.h>
    #include <math.h>
    
    
    int direct(void)
    {
    //directions
    printf("In this program, you will need to enter the current temperature before you can get an output.\n\n"); /* Directions */
    }
    
    
    float temp(T)
    {
    //values
      printf("What is the temperature? "); /* store variable */
      scanf("%f", &T);
      return (T);
    }
    
    
    float calc(speed)
    {
    float T;
    T = temp();
    //calculation
    speed=1086*(sqrt((5*T+297)/(247))); /* calculation */
    return (speed);
    }
    
    
    int main()
    {
    float T, speed;
    direct();
    temp();
    T = temp();
    calc();
    speed = calc(T);
    
    
    //print
    
    
    printf("\nThe speed of sound in the current temperature (%.1f) is %.1f.\n\n", T, speed); /*display result*/
    
    
    return (0);
    }



    Any help would be greatly appreciated. Thanks!


    ~SW
    Last edited by SofaWolf; 06-25-2012 at 01:09 PM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Search for a tutorial on writing "user defined functions".

    Also, search for an "indentation style" guide because your code is repellant.

    Soma

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    13
    Surprisingly, I can't find much documentation on C with google. Also, this was formatted for Reddit's code display.

    And yes, I know there are tutorials, what is the point of this forum if you just say, "Look it up."
    Last edited by SofaWolf; 06-25-2012 at 12:54 PM.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I can't find much documentation on C with google.
    Then your ability to search the web is horribly broken.

    I suggest you start with that. I would tell you to search for "How to use Google?", but honestly if you can't find C tutorials you'd probably mess that up.

    Also, this was formatted for Reddit's code display.
    I don't care if it was formatted by the gods; it is still repellant and at least a few people here will refuse to help until your code looks reasonable.

    Soma

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It looks like you're not fully grasping how to work with functions. You're using variables in your functions that aren't declared in them or passed to them (such as 'T' and 'speed').

    Here's a tutorial from this site that might give you some guidance.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You are asked to declare the variables local to each function. For example, in temp() should have float T. Then return T. Oh the function header should be float temp()
    In main when you call it, you can do T = temp(); Then speed = calc(T); having declared speed and T in main. calc() should also return float.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    13
    Quote Originally Posted by phantomotap View Post
    Then your ability to search the web is horribly broken.

    I suggest you start with that. I would tell you to search for "How to use Google?", but honestly if you can't find C tutorials you'd probably mess that up.



    I don't care if it was formatted by the gods; it is still repellant and at least a few people here will refuse to help until your code looks reasonable.

    Soma
    Well gee, you're helpful.

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    13
    Quote Originally Posted by Matticus View Post
    It looks like you're not fully grasping how to work with functions. You're using variables in your functions that aren't declared in them or passed to them (such as 'T' and 'speed').

    Here's a tutorial from this site that might give you some guidance.
    Yeah, I didn't put the declarations back in, but I had them before at the top of the code. I'm trying to figure out how to declare them in main. Thanks for the help.

    Quote Originally Posted by nonoob View Post
    You are asked to declare the variables local to each function. For example, in temp() should have float T. Then return T. Oh the function header should be float temp()
    In main when you call it, you can do T = temp(); Then speed = calc(T); having declared speed and T in main. calc() should also return float.
    Thanks, I'll start editing it in the original post.

    Quote Originally Posted by phantomotap View Post
    O_o

    What's the point of tutorials if one expects concepts to be freshly explained every time they are questioned?

    What's the point of search engines if not to reveal answers to questions already provided?

    o_O

    I win.

    Soma
    K.
    Last edited by SofaWolf; 06-25-2012 at 01:00 PM.

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    And yes, I know there are tutorials, what is the point of this forum if you just say, "Look it up."
    O_o

    What's the point of tutorials if one expects concepts to be freshly explained every time they are questioned?

    What's the point of search engines if not to reveal answers to questions already provided?

    o_O

    I win.

    Soma

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Sounds like the teacher did not do an adequate job explaining basic concepts such as local variables, parameter passing, and value returning.

  11. #11
    Registered User
    Join Date
    Jun 2012
    Location
    Madrid
    Posts
    9
    I think that the only thing you want is the program working:
    Code:
    /* Calculating speed of sound from temperature 6/18/12 (CSC-150) */
    #include <stdio.h>
    #include <math.h>
    void direct()
    { 
    /*directions*/
    printf("In this program, you will need to enter the current temperature before you can get an output.\n\n"); /* Directions */
    }
    float temp()
    {
    /*values*/
      float T;
      printf("What is the temperature? "); /* store variable */
      scanf("%f", &T);
      return T;
    }
    float calc(float T)
    { 
    /*calculation*/
    float speed;
    speed=1086*(sqrt((5*T+297)/(247))); /* calculation */  
    return speed;
    }
    int main()
    {
    direct();
    float T;           T=temp();
    float speed;       speed=calc(T);
    printf("\nThe speed of sound in the current temperature (%.1f) is %.1f.\n\n", T, speed); /*display result*/
    return (0);
    }
    Dont leave so many unwritten lines haha makes reading a program complicated XD

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Sounds like the teacher did not do an adequate job explaining basic concepts such as local variables, parameter passing, and value returning.
    Considering that the originator's efforts to solve his problem consisted of literally applying your comments without thought to his code I'd say they he only wants a solution instead of education.

    Dont leave so many unwritten lines haha makes reading a program complicated
    You are an idiot.

    Don't do peoples work for them at this forum. If you enjoy that sort of thing find yourself a place for that.

    This, again, is why I'm not a moderator; you'd already be temp-banned.

    Soma

  13. #13
    Registered User
    Join Date
    Jun 2012
    Posts
    13
    Okay, I updated the code in the original post.

  14. #14
    Registered User
    Join Date
    Jun 2012
    Posts
    13
    Quote Originally Posted by phantomotap View Post
    I'd say they he only wants a solution instead of education.
    I would like a solution. A solution would serve to benefit the education.

    Anyway, I'm used to people like you. Someone always has 2-7k posts on a forum and is a complete ass. They usually try to post in every topic, and more likely than not, they get banned eventually.

  15. #15
    Registered User
    Join Date
    Jun 2012
    Location
    Madrid
    Posts
    9
    Sorry I thought that I may helped him by making some changes in his program so he could see them and realise what his problem was.
    It´s obvious I am new to this, but not an idiot...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RPG troubles
    By MartinThatcher in forum Game Programming
    Replies: 3
    Last Post: 10-02-2009, 08:57 PM
  2. troubles in using GDB
    By leiming in forum C Programming
    Replies: 7
    Last Post: 04-18-2008, 01:54 AM
  3. GTK troubles...again...
    By cornholio in forum Linux Programming
    Replies: 4
    Last Post: 01-16-2006, 01:37 AM
  4. .AVI troubles
    By Untitled1 in forum Tech Board
    Replies: 6
    Last Post: 10-26-2003, 10:39 AM
  5. having troubles
    By neandrake in forum C++ Programming
    Replies: 7
    Last Post: 03-07-2002, 09:31 PM