You are here:

C++/Nested Structures

Advertisement


Question
Hi, I am not sure if I am doing anything wrong. But I can't get out the value from my arrays. Here is what I have done so far.....
The code I have placed is something like to get the values out is...

cout << "Name: " << NFL.play.name << endl;

but it doesnt seem to work?

==================
#include <iostream>
#include <fstream>
using namespace std;



struct Player
{
char name[40];
char league[5];
};

struct Team
{
Player play;
char position[5];
char owner[5];
};

Team NFL;

void read_file();



void main()
{
read_file();
cout << "Name: " << NFL.play.name << endl;
}

void read_file()
{
ifstream file_in;
file_in.open ("A:\\fantasy.dat");


while(!file_in.eof())
{
file_in.getline(NFL.play.name, 40, ' ' );
file_in.getline(NFL.play.league, 5, ' ' );
file_in.getline(NFL.position, 5, ' ' );
file_in.getline(NFL.owner, 5, '\n' );
}

file_in.close();

}

Answer
Shirley Valencia , Thank you for your question.

The problem may be in statements like

file_in.getline(NFL.play.league, 5, ' ' );

This statement will read in text from the input file, stopping at the first space character. If your data contains space characters, such statements will read data incorrectly. You probably want to use

file_in.getline(NFL.play.league, 5);

David Spector

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


David Spector

Expertise

Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.

Experience

I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Publications
Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

Education/Credentials
ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 About.com. All rights reserved.