Thread: My very first own C program (I need a hand!)

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    15

    My very first own C program (I need a hand!)

    Well hello all, I am new to this forum and this is my first post!

    I just started learning some C with a 7-days/lessons e-book and I completed the first lesson. So at the end, they asked to compose a program of our own which would basically do something like : c=a+b (2+3=c=5), you see...

    I needed to make something useful to me, so I immediatly went with * and /.

    If someone wants to test it, it compiles, but the results aren't quite fitting the equation (so far).

    Thank you!

    Code:
    #include <stdio.h>
    main() //This program is to calculate a certain DELAY value for a given tempo
    {
    int e,m,x,y;
    printf("Enter value for x :"); //That would be the BPM's
    scanf("%d",&x); //I enter let's say.. 120PBM
    y=m/x; //I need to divide a minute by the BPM's value
    m=60; //Here is where I define the value of a minute in seconds
    e=y/2; //E is the result I want (The result of the 1st equation above divided by 2)
    printf("Echo = %d\n",e);
    system("pause");
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    1) You use m before it's defined.
    2) Fix the indentation.
    3) int main(void)
    4) system("pause") is not standard.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Be nice to your user:
    Code:
    int e,m,bpm,y;
    printf("Enter the Beats Per Minute (BPM) :"); //That would be the BPM's
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I would also suggest you use data types that support fractions since you are dividing quite a bit.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    15
    Yes I wanted to change the sentence once I got it to work.
    Other data type to get the decimals, would that be "float" ?

    Quote Originally Posted by Memloop View Post
    1) You use m before it's defined.
    2) Fix the indentation.
    3) int main(void)
    4) system("pause") is not standard.
    I tried reversing them this morning (I didnt post the updated code), but it didn't work.

    system("pause") is not standard? I saw a quick tuto using "getchar", is that more standard?
    Looks like tutos differ a lot for a simple "Hello Word!"...
    Last edited by Exutus; 11-09-2009 at 01:35 PM.

  6. #6
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Quote Originally Posted by Exutus View Post
    system("pause") is not standard? I saw a quick tuto using "getchar", is that more standard?
    Looks like tutos differ a lot for a simple "Hello Word!"...
    I am just a beginner myself but as far as I understood it (I was told to use getchar()), using "PAUSE" results in a security issue.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Quote Originally Posted by Exutus View Post
    Yes I wanted to change the sentence once I got it to work.
    Other data type to get the decimals, would that be "float" ?



    I tried reversing them this morning (I didnt post the updated code), but it didn't work.

    system("pause") is not standard? I saw a quick tuto using "getchar", is that more standard?
    Looks like tutos differ a lot for a simple "Hello Word!"...
    Sorry, the answer was supposed to be that you use m before it's initialized, i.e. before you've assigned a value to it. Try moving the line where you assign 60 to m to before you calculate y=m/x.

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    15
    damn it won't work
    I love it LOL

    Gives me 0
    Code:
    #include <stdio.h>
    main()
    {
      double e,m,BPM,y;
      m=60;
      y=m/BPM;
      e=y/2;
      printf("Enter your BPM :");
      scanf("%if",&BPM);
      printf("Echo = %if\n",e);
      system("pause");
    }
    And why is sytem("pause") a security issue ??
    Thanks a lot for you help !

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Exutus View Post
    damn it won't work
    I love it LOL

    Gives me 0
    Code:
    #include <stdio.h>
    main()
    {
      double e,m,BPM,y;
      m=60;
      y=m/BPM;
      e=y/2;
      printf("Enter your BPM :");
      scanf("%if",&BPM);
      printf("Echo = %if\n",e);
      system("pause");
    }
    And why is sytem("pause") a security issue ??
    Thanks a lot for you help !
    Take a moment, and read what your code does. Line by line.
    Code:
    double e,m,BPM,y;
    declare some variables of type double. They values are completely random at this point.
    Code:
    m=60;
    Assign the value of 60 to m. No problems so far.
    Code:
    y=m/BPM;
    Divide m by BPM. What a second... what does BPM equal? You are essentially saying, "divide m by a completely random number, and assign the result to y". This doesn't work.
    bit∙hub [bit-huhb] n. A source and destination for information.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    15
    Quote Originally Posted by bithub View Post
    What a second... what does BPM equal? You are essentially saying, "divide m by a completely random number, and assign the result to y". This doesn't work.
    BPM is the value it will ask when running the program.
    Let's say it's 120. Then (60/120)/2= 0.25

    I need it to give me 0.25
    I updated the code a little bit, but it only gives me 0.00

    Code:
    #include <stdio.h>
    main() //This program is to calculate a certain DELAY value for a given tempo
    {
    
    float e,m,x,y; e = m = x= y = 0;
    
    printf("Enter value for x :"); //That would be the BPM's
    scanf("%f",&x); //I enter let's say.. 120PBM
    
    if(x != 0) y = m/x; //I need to divide a minute by the BPM's value
    
    e=y/2.0f; //E is the result I want (The result of the 1st equation above divided by 2)
    printf("Echo = %.3f\n");
    
    system("pause");
    }
    It should be soon working, a little more help please, I really want to understand my mistakes.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Change your initial assignment into a float value - 0.0, instead of 0.

    It sounds stupid, but compilers are not the smartest knife in the drawer when it comes to handling floats.

    Two other points:
    1) Comparing a float to a number, like if(x != 0), is not good, because floats can't be perfectly represented for each value - so they'll be close, but not spot on, for many values. That makes your comparison not work as you think it should, and it's frustrating.

    Instead, think of a range, like if(x > 1). Those kind of range comparisons, work fine with floats, in general.

    2) It's tempting, and I do it myself sometimes, but the habit of including two lines of code on the same line of text, is generally a very bad habit.

    The problem with system("pause"), is that not all systems have a "pause" command - and some smart cracker can replace the pause command program with their own viral version of a "pause", command, and jack your whole system into the middle of next week.

    Just a simple :
    Code:
    printf("\n\n\t\t\t     press enter when ready");  //center a short message
    int or char variable name = getchar();            //hold the console window open until enter pressed
    return 0;
    is what I use, commonly.
    Last edited by Adak; 11-09-2009 at 09:59 PM.

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You initialize m to zero, therefore y will be zero in the divide, and thus your answer will be zero.

    Also, your last printf is missing a variable to use to complete the formatting you are asking it to do.
    Mainframe assembler programmer by trade. C coder when I can.

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    15
    Thanks Adak
    Unfortuatly, it still give me 0.000

    Code:
    #include <stdio.h>
    main()
    {
    double e,m,x,y; e = m = x= y = 0.0;
    printf("Enter value for x :");
    scanf("%lf",&x);
    if(x != 0.0) y = m/x;
    e=y/2.0f;
    printf("Echo = %.3lf\n");
    system("pause");
    }
    Last edited by Exutus; 11-09-2009 at 09:54 PM.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What is the value of 0.0 divided by some number? m is still just 0.0, in your program.

    And you're not using a range value for x, but that's not the problem just now. m = 0.0 is the problem

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Exutus View Post
    Thanks Adak
    Unfortuatly, it still give me 0.000

    Code:
    #include <stdio.h>
    main()
    {
    double e,m,x,y; e = m = x= y = 0.0;
    printf("Enter value for x :");
    scanf("%lf",&x);
    if(x != 0.0) y = m/x;
    e=y/2.0f;
    printf("Echo = %.3lf\n");
    system("pause");
    }
    1) You really should write int main(). The use of functions without an explicit return value like this is depreciated. The implicit return value is int anyway.
    2)m should be 60, as per original code. You just need to set it to 60 before you use it.
    3)all lines withing the brackets of the function should be indented. It makes the code easy to read for those trying to help you.
    4)Only use 1 semicolon per line, aside from for loops. Again, its easier to read.
    5)The 2.0f should just be 2.0.
    6)You don't pass a variable to printf. You need to print e.
    7)Use descriptive variable names. Single letter names are rarely descriptive. Again, it makes it easier for us and you to follow your code.
    8)You need to put return 0 at the end of main. The 0 is used to indicate to the OS that your program ran successfully.
    9)the if and parenthesis should have their own line. Indent the line that follows to indicate that it is part of the if statements. Again, this is a legibility convention.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM