Printing From A structure.

This is a discussion on Printing From A structure. within the C Programming forums, part of the General Programming Boards category; Okay, I made a basic program detailing departures from 2 airports. Code: #include <stdio.h> int input; struct Reno_Airport { char ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    Printing From A structure.

    Okay, I made a basic program detailing departures from 2 airports.

    Code:
    #include <stdio.h>
    int input;
    struct Reno_Airport {
    char FNewYork[60];
    char FSacramento[60];
    char FMontreal[60];
    } Reno_Airport = {
    "New York, 3:00",
    "Sacramento, 1:23",
    "Cancelled."
    };
    
    struct Copenhagen_Airport {
    char FMoscow[60];
    char FCairo[60];
    } Copenhagen_Airport = {
    "Moscow, 17:30",
    "Cairo, 23:34",
    };
    
    main()
    {
    printf("Enter Airport: 1=Reno, 2=Copenhagen ");
    scanf("%d",&input);
    switch (input) {
    case 1:
    printf("Departures: %c, %c, %c",FNewYork, FSacramento, FMontreal);
    break;
    case 2:
    printf("Departures: %c, %c",FMoscow, FCairo);
    break;
    }
    }
    By the way, I already tried putting in Reno_Airport.FNewYork instead of FNewYork, and the same for Sacramento and Montreal, as well as copenhagen for Moscow and Cairo, so that's not it. When I run it with the structure/variable name in front of the characters it just gives me strange characters as an output.

    So, how do I get fields from structures and print them out?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Well picking the right format for printf would be a good start.
    &#37;c prints chars, %s is for strings.

    A good compiler with maximum warnings will tell you this, and was discussed very recently.
    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.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Perhaps your code needs a proper indendation. And you have already got the answer here.

    Code:
    Reno_Airport.FNewYork
    You just nned them to be printed in the right format. &#37;c -> %s.

    ssharish2005
    Last edited by ssharish2005; 08-17-2007 at 03:02 PM.

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I didn't know that a compiler could effectively compile this...

    Like salem said, use &#37;s instead of %c. %s are for string, %c are for char. See this if you want more information.

    And actually, what you are trying to do is to read the value of the variable "FNewYork", which doesn't exist; in fact "FNewYork" is a MEMBER of a variable of type struct Reno_Airport.

    Knowing you declared a variable name Reno_Airport of type struct Reno_Airport, what you should do is this
    Code:
    printf("Departures: %s, %s, %s",Reno_Airport.FNewYork, Reno_Airport.FSacramento, Reno_Airport.FMontreal);
    Note that this apply to variable of type struct Copenhagen_Airport too.

    Also, you should use local variables instead of globals one. Indentation wouldn't hurt too.


    [Damn, too slow]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. problem printing out structure after loading
    By pari in forum C Programming
    Replies: 17
    Last Post: 11-23-2002, 08:12 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

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