Thread: easy Question

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    easy Question

    In this code example
    what is this code doing?

    ("\n%s ",
    and
    ("%s\n",

    #include <stdio.h>
    #include <string.h>

    struct tag {
    char lname[20]; /* last name */
    char fname[20]; /* first name */
    int age; /* age */
    float rate; /* e.g. 12.75 per hour */
    };

    struct tag my_struct; /* declare the structure my_struct */

    int main(void)
    {
    strcpy(my_struct.lname,"Jensen");
    strcpy(my_struct.fname,"Ted");
    printf("\n%s ",my_struct.fname);
    printf("%s\n",my_struct.lname);
    return 0;
    }

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    Your printf() statement tries to print strings.....however your char arrays are not strings.
    "Jensen", needs to be "Jensen/0"
    give that a try.
    PHP and XML
    Let's talk about SAX

  3. #3
    First of all USE CODE TAGS!!!!

    second of all:

    Code:
    struct tag my_struct; //SCARY!!!!
    do this instead:

    Code:
    tag my_struct; //AWESOME!!!!
    And allso what is this all about:

    Code:
    ("\n%s ",
    and
    ("%s\n",
    Doesn't look right ot me.

    Code:
    #include <string.h> //SCARY!!!!
    do this:

    Code:
    #include <string> //AWESOME!!!!
    Code:
    int main()  //NO VOID GRRRR
    {
     cout<<"Name: ";
     cin<<my_struct.fname;
     cout<<"Last Name: ";
     cin<<my_struct.lname;
     if(!strcmpi(my_struct.fname, "Jhonson")) //notice the 'i' instead of 'y'
    {
     cout<<"Thats my name too!!";
    }
    return 0;
    }
    I hope that clears it a bit

    -Devouring One-

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    This should be moved to the C-Programming board.

    printf("\n%s ",my_struct.fname);
    printf("%s\n",my_struct.lname);

    \n prints a new line, and %s is a string field with is replaced by my_struct.fname and my_struct.lname

    For example:

    char *hw = "Hello World, i'm ";
    int age = 16;
    char *end = " years old!";

    printf("\n%s%d%s", hw, age, end);

    This expands to

    printf("\nHello World, i'm 16 years old!");

    See this for further info:
    http://osr5doc.ca.caldera.com:457/cg...n/man?printf+C

  5. #5
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    first of all, strcmp checks for comparison, he's trying to copy chars into the variable(that's the part he's having trouble with)...second of all, he's working with C, and you posted C++ code.
    PHP and XML
    Let's talk about SAX

  6. #6
    Yes i did because its a C++ board.

    Ohh i didn't know about that.... well i will learn it....hopefully
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy Vector contructor question
    By noodle24 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2006, 07:43 PM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 04:59 PM
  3. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 01:36 AM
  4. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  5. Easy Question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2002, 12:19 PM