Thread: New programmer - please help!

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    22

    New programmer - please help!

    I've only just started a course in C Programming, and I've never done any programming before. My exercise asks me to write a program that converts litres into pints and fluid ounces. I have written what I thought was correct, but when running the program it just gives an output of zero for both pints and fluid ounces. The code is:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main (void)
    {
        const litres2floz=35.1950652;
        float a;    //litres
        float b=a*litres2floz;     //fluid ounces
        int c=(b/20);   //pints
    
    
        printf("Enter the number of litres\n");
        scanf("%f",&a);
    
        printf("%f litres = %d pints and %f fluid ounces\n",a,c,b-(c*20));
    
        system("pause");
        return 0;
    
    
     }
    If anyone can see where I have gone wrong in this it would be a big help.

    Thanks guys.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Does your compiler not emit any errors or warnings? What type, exactly, do you think the variable litres2floz is? What value is the variable, a initially? What value, therefore, does the variable, b, take?
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    When compiled the compiler tells me that there are no errors in the code. It just doesn't do the conversions.

    With regards to a haven't I defined it so that I can enter a value for it and all other values will change correspondingly?

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Then you should turn up the warnings level on your compiler.

    const litres2floz doesn't specify a type for the variable litres2floz, only that it is constant. What type (int, float, double char etc) do you expect it to be?

    Place a printf statement after you have declared all your variables(litres2floz, a,b and c) to display what each is. Do they take the values you expect them to?
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    The only value that takes the one I expect it to is a. The others just display themselves as 0.

    Any idea how I go about changing the warnings level on Dev C++?

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Project menu --> project options, select the 'parameters' tab and enter -Wall in the 'compiler' field.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    int main (void)
    {
        const float litres2pints=1.76;
        const float pints2floz=20;
        float a;
        float b;
        int c;
        float d;
    
    
        printf("Enter the number of litres\n");
        scanf("%f",&a);
        b=a*litres2pints;
        c = (int)b;
        d=(b-c)*pints2floz;
    
        printf("%f litres = %d pints and %f fluid ounces\n",a,c,d);
    
        system("pause");
        return 0;
    
    
     }
    This seems to work. Does it look correct to you?

  8. #8
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    There's a lot of weird stuff in your code.

    Here's what I picked up :

    Code:
    const litres2floz=35.1950652;
    Well, as specified before....

    Code:
    float a;    //litres
    float b=a*litres2floz;     //fluid ounces
    int c=(b/20);   //pints
    Since, a is not given a value, who knows what value b is going to end up getting. And since you'll have no clue what b will be getting, how would you know what value c is getting ? Further, by divding b (a float), by 20, you'll get a float, and upcasting it to an integer may result in precision errors.

    Code:
    system("pause");
    The system command is very OS specific. If you ever intend to write portable code, learn to avoid it all together.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For the system("pause"): http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    You also don't need <math.h>.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What game programmer should I be? need some advice.
    By m3rk in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-20-2009, 11:12 PM
  2. When are you REALLY a programmer?
    By m.mixon in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 09:08 PM
  3. Senior 3D Programmer - Moab Studios™ LLC
    By moab in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 08-30-2005, 10:19 PM
  4. Me as a programmer?
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 03-31-2002, 06:19 PM
  5. I need to interview professional programmer.....please
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 01-05-2002, 02:46 PM