Thread: Printing From A structure.

  1. #1
    Registered User
    Join Date
    Jul 2007

    Printing From A structure.

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

    #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",
    struct Copenhagen_Airport {
    char FMoscow[60];
    char FCairo[60];
    } Copenhagen_Airport = {
    "Moscow, 17:30",
    "Cairo, 23:34",
    printf("Enter Airport: 1=Reno, 2=Copenhagen ");
    switch (input) {
    case 1:
    printf("Departures: %c, %c, %c",FNewYork, FSacramento, FMontreal);
    case 2:
    printf("Departures: %c, %c",FMoscow, FCairo);
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

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

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

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

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    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
    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, 09:12 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM