Like Tree1Likes

first program has me stumped

This is a discussion on first program has me stumped within the C Programming forums, part of the General Programming Boards category; hey, this is my first c program (yeah, helloworld wasn't challenging enough). But it gets down to a linking error... ...

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

    first program has me stumped

    hey, this is my first c program (yeah, helloworld wasn't challenging enough). But it gets down to a linking error... could anyone let me know what I am doing wrong?

    Code:
    #include <stdio.h>
     
    int main()
    {
       float rt=0;
       float y=0;
       float yt=0;
       float p=0;
       float r=0;
       float t=0;
       char s[10];
       float i=0;
       
        printf("How much money will you pay per week?\n");
        gets(s);
        p=atoi(s);
        printf("What is the interest rate?\n");
        gets(s);
        r=atoi(s);
        printf("How many years will you make payments?\n");
        gets(s);
        yt=atoi(s);
        yt=yt+1;
        for(y=0;y<yt;y++)
        {
                         i=((p*52)+t)*r;
                         printf("You will make %.2d in interest in %d years.\n",i,y);
                         rt=i=(p*52);
                         t=t+rt;
                         printf("You will make %.2d total in %d years.\n",t,y);
                         printf("Press Enter.\n");
                         getchar();
        }
                         return(0);
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,415
    could anyone let me know what I am doing wrong?
    O_o

    Well, the first thing that I noticed that you are doing wrong is not posting the error messages you got along with your source.

    Start by correcting that by posting the errors you are getting.

    Soma

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Problems:
    #1) Not telling us what the error was (as phantomotap suggested.
    #2) Using gets() (that is a link to be clicked)
    #3) Not adding the header file for atoi()
    Salem likes this.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Lemme guess, you're a "TurbidC" user, and you're getting "floating point formats not linked"?

    See here
    c - How to enable linking floating point library in TurboC? - Stack Overflow

    The better answer is to get a decent compiler!
    Download codeblocks-10.05mingw-setup.exe from Downloads

    One of the really nice things about the GCC compiler is that it will tell you when you make a mess of passing parameters to printf and scanf.

    > printf("You will make %.2d in interest in %d years.\n",i,y);
    Because you are making a mess!
    %d is NOT used to print a float.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    Okay, I caught the stdlib.h exclusion, downloaded codeblox, and built the program. It actually ran with codeblox, but it returns 0 every time, and the number of years is returned as a 8 digit number. I don't understand the reference to misusing gets(). I am learning this all on my own, so I'm sorry if this stuff sounds dumb.

    How much money will you pay per week?
    1
    What is the interest rate?
    1
    How many years will you make payments?
    5
    You will make 00 in interest in 1078591488 years.
    You will make 00 total in 1078591488 years.
    Press Enter.
    You will make 00 in interest in 1079640064 years.
    You will make 00 total in 1079640064 years.
    Press Enter.
    You will make 00 in interest in 1080262656 years.
    You will make 00 total in 1080262656 years.
    Press Enter.
    You will make 00 in interest in 1080688640 years.
    You will make 00 total in 1080688640 years.
    Press Enter.
    You will make 00 in interest in 1081098240 years.
    You will make 00 total in 1081098240 years.
    Press Enter.
    You will make 00 in interest in 1081311232 years.
    You will make 00 total in 1081311232 years.
    Press Enter.

    Process returned 0 (0x0) execution time : 7.804 s
    Press any key to continue.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       float rt=0;
       float y=0;
       float yt=0;
       float p=0;
       float r=0;
       float t=0;
       char s[10];
       float i=0;
       
        printf("How much money will you pay per week?\n");
        gets(s);
        p=atoi(s);
        printf("What is the interest rate?\n");
        gets(s);
        r=atoi(s);
        printf("How many years will you make payments?\n");
        gets(s);
        yt=atoi(s);
        yt=yt+1;
        for(y=0;y<yt;y++)
        {
                         i=((p*52)+t)*r;
                         printf("You will make %.2d in interest in %d years.\n",i,y);
                         rt=i=(p*52);
                         t=t+rt;
                         printf("You will make %.2d total in %d years.\n",t,y);
                         printf("Press Enter.\n");
                         getchar();
        }
                         return(0);
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Like I said....
    Code:
    $ gcc -W -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:28: warning: format ‘%.2d’ expects type ‘int’, but argument 2 has type ‘double’
    foo.c:28: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘double’
    foo.c:31: warning: format ‘%.2d’ expects type ‘int’, but argument 2 has type ‘double’
    foo.c:31: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘double’
    /tmp/cc6Yri4k.o: In function `main':
    foo.c:(.text+0x61): warning: the `gets' function is dangerous and should not be used.
    Fix your printf formats!
    %d is NOT FOR FLOATS
    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.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,695
    Quote Originally Posted by cybalicist View Post
    I don't understand the reference to misusing gets().
    Why gets() is bad

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    okay, yeah, I knew about that, but I don't know about the formatting stuff for the alternative yet. I figured this was okay just for testing purposes and practice.

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    so, changed the %.2d to %.2f, I knew that, but had forgotten, changed the y and yt variables to int's, and it runs fine, but it completely ignores the interest, the i variable...

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    You should post your latest code if you're still having problems.
    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.

  11. #11
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       float rt=0;
       int y=0;
       int yt=0;
       float p=0;
       float r=0;
       float t=0;
       char s[20];
       float i=0;
    
        printf("How much money will you pay per week?\n");
        gets(s);
        p=atoi(s);
        printf("What is the interest rate?\n");
        gets(s);
        r=atoi(s);
        printf("How many years will you make payments?\n");
        gets(s);
        yt=atoi(s);
        for(y=0;y<yt;y++)
        {
                         i=(p*52+t)*r;
                         printf("You will make %.2f in interest in %d years.\n",i,y+1);
                         rt=i+(p*52);
                         t=t+rt;
                         printf("You will make %.2f total in %d years.\n",t,y+1);
                         printf("Press Enter.\n");
                         getchar();
        }
        return(0);
    }

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    2,769
    The reason you think this is a valid problem domain equation "rt=i+(p*52)" is what?

    Say what each C variable means and how the really add up to the correct answer.

    NOTE: I am guessing they result in a very incorrect answer.

    Edit: It would be easier if you used descriptive variable names.
    Edit2: What is the value of t in this equation "i=(p*52+t)*r"

    Tim S.
    Last edited by stahta01; 06-09-2012 at 10:53 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  13. #13
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    When I wrote this I planned on using descriptive var names, but it was faster to abbreviate on paper, and I didn't know about this forum, hindsight is 2020. Rt is running total, t is total, y is years, yt is years total, p is payment amount, r is rate of interest, s is placeholder for string input, I is interest accrued. In I=(p*52+t)*r, it means multiply the payment amount times 52, for weeks in a year, add the total, which I assume on the first run would be 0 since I set it at zero earlier, (although I wonder if it's acting as a local var instead of a global), and multiply the result by the rate of interest then store it in I. For rt=I+(p*52) is interest accrued plus the product of the payments for a year goes into rt.

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    Ok, you might not have know about the forum, fair enough, but you ought to know about your editor, and your editor should have auto-completion to help you type those lenghty variable names. It is critical to use good variable names. Later on you might want to rename a variable and doing a search and replace on variable r is nasty as opposed to search and replace on rate_of_interest.
    Also, don't hard codes values like 52. Use #define WEEKPERYEAR 52.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    2,769
    Look up what atoi does. What data type is r; what value are you putting in it.

    "r=atoi(s);"

    EDIT: You need to post input and output for your program and how it differs from the correct output.

    Note: In this case the output is obviously wrong; but, I am not wasting my time figuring out the correct output.

    I suggest you learn to debug your code or place printf statements in the code to find the problem.

    Tim S.

    Code changes I did too see what your program was doing.
    I still think you have a math problem; but, you also have a coding problem because r was zero when I entered .1 for 10 percent rate.
    Code:
                         printf("payment amount; p:= %f\n",p);
                         printf("rate of interest; r:= %f\n",r);
                         printf("?total?; t:= %f\n",t);
                         i=(p*52+t)*r;
                         printf("interest accrued; i:= %f\n",i);
    Last edited by stahta01; 06-09-2012 at 03:49 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

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

Similar Threads

  1. K&R 5.6, stumped again
    By The111 in forum C Programming
    Replies: 2
    Last Post: 08-17-2011, 09:43 PM
  2. C program crash... I'm stumped.
    By Dino in forum C Programming
    Replies: 7
    Last Post: 03-16-2008, 09:27 AM
  3. Replies: 20
    Last Post: 05-24-2007, 02:06 AM
  4. Bubble sort program stumped
    By matthughes in forum C Programming
    Replies: 12
    Last Post: 05-17-2007, 02:28 PM
  5. stumped
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2001, 01:27 PM

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