Nested Structures - User Input

This is a discussion on Nested Structures - User Input within the C Programming forums, part of the General Programming Boards category; I am working on a program that requires a nested structure to store a departure date (day, month, year) and ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    Nested Structures - User Input

    I am working on a program that requires a nested structure to store a departure date (day, month, year) and have declared it as such:-

    Code:
    struct departDate{
    	int day;
    	int month;
    	int year;
    	};
    
    struct flightDetails{
    	String flightNo;
    	String departAirport;
    	String arriveAirport;
    	departDate date;
    	String departTime;
    	int seats;
    	int reserved;
    	double price;
    	};
    
    const int listSize = 30;  //number of records in array
    int currentSize = 0;
    I sort of understand the concept of nested structures and how to access structure members but in my program I don't set 'departure date' values I accept user input. I am unsure on how to store the user input. All references I have checked seem to initialise values for nested structures. I would like to ask the user only to enter one 'departure date' comprising of day, month and year as opposed to entering all three seperately. I thought the following code was correct:-
    Code:
    	cout<<"Enter departure date:";
    	cin>>flightList[currentSize].date.day>>flightList
                    [currentSize].date.month>>flightList 
                    [currentSize].date.year;
    but since I am having problems displaying my records on screen (fomatting of) I cannot check if this is correct.

    Its one problem after another for us programming newbies!!

    Any help would be gratefully appreciated

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I thought the following code was correct
    Aside from being C++ and not C, the way you access the array of structures is correct. Let's break it down because the meat of your question is portable between C and C++.

    >flightList[currentSize].date.day
    Assuming flightList was declared as
    Code:
    flightDetails flightList[listSize];
    The expression works as thus:

    flightList is an array of flightDetails

    flightList[currentSize] is a flightDetail object accessed by the subscript operator. flightList is an array, so subscripting is legal.

    flightList[currentSize].date is a departDate object accessed by the member operator. flightList[currentSize] is a structure with a date member, so this is legal.

    flightList[currentSize].date.day is an integer accessed by the member operator. date is a structure with a day member, so this is also legal.

    >but since I am having problems displaying my records on screen (fomatting of)
    That's your problem then. Start a new thread with that problem on the C++ forum and we'll help you out.
    My best code is written with the delete key.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 12:09 PM
  4. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  5. Getting user input for filename loop
    By jpchand in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2003, 07:37 AM

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