Displaying Data from a Nested Structure

This is a discussion on Displaying Data from a Nested Structure 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

    Displaying Data from a Nested Structure

    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.

    I am trying to display date using:

    Code:
    void displayAllRecords()
    { //print the data from the aray of records under suitable header
    
    	int index;
    
    	system("cls");
    
    	displayHeading();
    	cout<<setiosflags(ios::left);
    
    	for(index = 0; index<currentSize; index++) 
                   {
                      cout<<setw(15)<<flightList[index].date.day,flightList
                      [index].date.month,flightList[index].date.year;	
    	cout<<setw(15)<<flightList[index].departTime;
    When I display records on screen only the day of depature date is displayed.

    Its one problem after another for us programming newbies!!

    Any help would be gratefully appreciated

    p.s. Prelude - thanks for pointing me in the right direction.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >cout<<setw(15)<<flightList[index].date.day,flightList
    >[index].date.month,flightList[index].date.year;
    Change the commas to insertion operators:
    Code:
    cout<<setw(15)
      <<flightList[index].date.day
      <<flightList[index].date.month
      <<flightList[index].date.year;
    It would also be a good idea to format everything nicely so that the values are separated:
    Code:
    cout<< flightList[index].date.day <<'-'
      << flightList[index].date.month <<'-'
      << flightList[index].date.year;
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static data structure in a library
    By melight in forum C Programming
    Replies: 6
    Last Post: 01-10-2009, 11:12 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  5. Dynamic Data Structure -- Which one is better?
    By Yin in forum C++ Programming
    Replies: 0
    Last Post: 04-11-2002, 12:38 AM

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