Thread: Need help with C programming exercise.

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    5

    Need help with C programming exercise.

    In this exercise you are to extend this program - call it helloData.c - so that it prompts the user
    for their age and and a dollar amount between 0.00 and 100.00 (floating point number). It then prints out
    the following:

    Hi <name>. You said your age was <age> and your number was <number>

    -------------------------------
    This is what I've had so far....and I'm stuck!!!

    insert
    Code:
    #include <stdio.h>
    
    
    int main (void) {
        char    message[31];
    
    
        printf("Hi. My name is program. What is your name? ");
        scanf("%s", message);
        printf("Hi %s\n", message);
        printf("How old are you? ");
        scanf("%s", message);
        printf("Your number is %s%d%.2f", message);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for their age and and a dollar amount between 0.00 and 100.00 (floating point number). It then prints out
    What data type would you store the age in?
    Now look up the corresponding scanf() format to read a value into a variable of that type.

    What data type would you store the dollar amount in (there's a hint in the question)?
    Now look up the corresponding scanf() format to read a value into a variable of that type.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    I just start learning programming....Can you give me more details?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    C data types - Wikipedia, the free encyclopedia
    Your book should have the same list.
    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.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    There are different data types for holding guess what - different types of data.

    So you need to find out what these 'primitive' data types are how to declare them, and for your program, how to use scanf() to read values into them.

    You already have some understanding of this as shown in your printf() statement
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    I still don't get the out put that between 0.00 and 100.00 .....
    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main (void) {
        char    message[31];
    
    
        printf("Hi. My name is program. What is your name? ");
        scanf("%s", message);
        printf("Hi %s\n", message);
    	printf("How old are you? ");
    	scanf("%d", message);
    	int age;
    	printf("Your number is %d%.2f", age);
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The following lines are alright:

    Line 9: You print a prompt
    Line 10: You scan a string into a char array
    Line 11: You print the string
    Line 12: You print another prompt

    The following lines are a little screwy:

    Line 13: You try to scan an integer, but pass the address of the char array - you need an integer variable to store an integer type
    Line 14: You declare an integer called age, but don't give it a value. Hmm, wasn't there a line above where you were trying to scan for an integer?
    Line 15: You try to print "age", but you never stored a value to it. Furthermore, you have two format specifiers (%d for int, and %f for float) but only one argument. You should provide two arguments, even if they're the same one.

  8. #8
    Registered User
    Join Date
    Oct 2013
    Posts
    19
    You can think this way.
    1. within the scanf function a. data type (i.e. "%d" for the integer) b. assign the value to the variable (i.e. if age is variable, &age).
    2. declare the variable type before use it (i.e, int age)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming Language Exercise
    By serg_yegi in forum C Programming
    Replies: 17
    Last Post: 11-30-2010, 04:36 AM
  2. TCP/IP Socket Programming Exercise
    By Martin_T in forum C Programming
    Replies: 9
    Last Post: 11-10-2009, 04:20 PM
  3. c programming exercise
    By Pulock2009 in forum C Programming
    Replies: 3
    Last Post: 10-30-2009, 02:48 AM
  4. programming exercise
    By mashour06 in forum C Programming
    Replies: 1
    Last Post: 06-01-2009, 06:22 AM
  5. The C programming language exercise
    By refuser in forum C Programming
    Replies: 10
    Last Post: 11-20-2008, 03:57 PM