Thread: Help with a Basic C Program!!

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    Question Help with a Basic C Program!!

    Hi All,

    I have just started Learning C programming at college and I'm quite stuck with a basic excercise we've been asked to complete, this is basically what we have to do.
    "Write a program to input your name, height in inches and weight in pounds, convert the height to centimeters and weight to kilograms and display the followoing results."
    1 inch = 2.54 cm and 1 pound = 0.4546kg.

    Now I know this is extremely basic to most peeps on here but i'd appreciate if someone could give me a few tips on how I should set this out and what syntax to use cause im just going round in circles. Thanks alot

    Kiwi Fella

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    First you need do try it youself. If you have no code to post you will not get any help from this site.

    Start by writing a simple "Hello world" program.

    Then use functions like scanf (for beginners) or fgets to read, and printf to print the name.

    If you run into problems, just post your code and we will help you!

    Good luck.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    Ok Guys the thing is I have got an idea of how I wanna set it out but try as I may cant get it down on paper or my PC, I am not sure when or where to declare variables, I know that sounds basic but we have just started learning that stuff, im pretty much stuck

    #include <stdio.h>

    int main

    {
    double centimeters,kilograms,pounds,inches;
    int a,b,c;

    printf("Please enter your name\n");
    scanf("name\n");

    Thats pretty much all I can think of now, Am I on the right track?? I know there is jack all code there but not sure how to declare the variables then apply them

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Ok we start with a message from kermi3: READ THIS FIRST

    A simple example:
    Code:
    int main(void)
    {
       char name[1024];
       printf("Enter your name: ");
       scanf("%s", name);
       printf("Your name is: %s\n", name);
       return 0;
    }
    When you get more experienced with C you will learn that the scanf function is not the best way to read from input. But for now it's ok to use it.

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Code:
    #include <stdio.h>
    
    int main()/*firstoff all its ()*/
    
    {
    	double centimeters,kilograms,pounds,inches;
    	int a,b,c;
    	char name[30];/* I dont know but if ne1 has a name larger then 30 letters plz notify me:-p*/
    
    	printf("Please enter your name\n");
    	gets(name);/*i think for now gets() would be fine*/
    	printf("Ur name is %s.",name);
    	return 0;/*dont write void main write int main() and return 0;*/
    
    }
    *first off all use code tags
    *second dont just copy this for height etc but have a look at it and try to understand it
    * and check the book u use to learn c coz u take lessons for it then i suppose i also have some books about it
    grtz
    Lamb

  6. #6
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Originally posted by KIwi_Fella
    Ok Guys the thing is I have got an idea of how I wanna set it out but try as I may cant get it down on paper or my PC, I am not sure when or where to declare variables, I know that sounds basic but we have just started learning that stuff, im pretty much stuck

    #include <stdio.h>

    int main

    {
    double centimeters,kilograms,pounds,inches;
    int a,b,c;

    printf("Please enter your name\n");
    scanf("name\n");

    Thats pretty much all I can think of now, Am I on the right track?? I know there is jack all code there but not sure how to declare the variables then apply them
    First of all your arnt familiar with ur basics ...ask you collegues and teacher how printf and scanf work ...secound you need a good understanding on variables ....Get some help...This isnt a tought thing to do search for some turtoials on the net that can help you with the basics......
    Well let me give u a hint:

    you use scanf() to store the persons input in a variable okay for example the person enters his height in inches , so the convert it to centimeters .This is what you do

    use the variable centimeters to store the conversion

    centimeters = inches /100;

    and then print it out using ur printf statment like print ( "%f", centimeters );
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    Thanks alot for your help guys, I have a pretty good idea of how I wanna set this out now, Appreciate all your responses

    Cheers

    Kiwi Fella

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>gets(name);/*i think for now gets() would be fine*/
    Personally I think gets() will never be fine. fgets() is slightly more complicated, but still very easy to understand, so why even consider gets() ?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    118
    I recommend buying, "Programming C for dummies" volume 1 and two. These two books will teach you the basics right from the very beginning. If I can learn it, anyone can. So, go to your book store and buy these books. I promise they WILL teach you the basics of C, or my name isn't - well, just get the books. You won't regret it!
    "The distinction between past, present and future is only an illussion, even if a stunning one."
    -Albert Einstein

  10. #10
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Originally posted by Tommaso
    I recommend buying, "Programming C for dummies" volume 1 and two. These two books will teach you the basics right from the very beginning. If I can learn it, anyone can. So, go to your book store and buy these books. I promise they WILL teach you the basics of C, or my name isn't - well, just get the books. You won't regret it!
    You won't regret it?? I think its the wort book ever created in the history of C ..The book is intended for dum people...reallyin a whole book they teach u about the basic like loop, if/else and thats it ..Sheesh a whole book for that ... I got that book when i was 13 years old ....
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  11. #11
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    <<Personally I think gets() will never be fine. fgets() is slightly more complicated, but still very easy to understand, so why even consider gets() ?>>

    i thought someone who even doesnt writes int main with () after it and has a strange way of declaring variables was just going to be happy if i post it with gets instaead of fgets

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. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 09:01 AM
  3. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. Help me with this basic c program
    By [ToXiC]-[LeaK] in forum C Programming
    Replies: 1
    Last Post: 01-28-2002, 11:44 PM