I am a new user and I am looking for some help with one of my problem. I have to write a program that reads in from a file through structs and store the information in a single struct and write the code to display a single class. This needs to be in a function that I am passing the struct to. Then modify the code so the class struct is stored in an array of structs, and finally write the code to display the information you have stored in a formatted screen output. My question is: how do i pass info a struct? How do i pass info into any array of structs? I would like some guidance with this problem. I am trying my best to understand what I need to :confused: . Below is my code any help would be greatly appreciated.

Thanks,
mathrules

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

// This struct hold information about the name of person, id number, phone #
struct Person
{
    string name;
    int iid;
    int iphone;
};

// Struct tells what time the class is
struct Time
{
    int  iday;
    int  ihour;
    int  imin;
};

struct Course
{
    string cname;
    int icourseid;
};

//struct puts all information together and gives a concise output
struct Class
{
    Person  cstudent[50];
    Person cinstructor;
    Time  itime;
    string room;
    Course curriculum;

  };

int main ()
{
    fstream information; // command to open file
    Class * ptr;
    ptr= new Class [70];


    //Open the file
        information.open ("lab05-mar.txt", ios:: in);
        if (!information)
        {       // Validate / Return Error if file won't open
                cout<< " There is an error! Cannot open the file properly.";
                return -1;
        }
        Class academic ;
       
void Totalinfo( fstream & information, something)
           
       	cout<<"How many classes do you have? " ;
        cin << number;
       cout<< number;
        
        getline (information, academic.curriculum.cname);
        cout<< academic.curriculum.cname << endl;
        //getline(information,academic.curriculum.cname);\
        cout << flush;
        information >> academic.curriculum.icourseid;
        cout<< academic.curriculum.icourseid;
		


        return 0;

 }

Just a note...

I can already see some problems...

You're not implementing the <string> header...

On one of your later lines you're using something along the lines of...

cin << value

...and that just wont work.

Also please use proper indentation and code tags, or others may not be so willing to help you.


As for your main problem, what do you need to make an array of? People or Classes? And also, is the information for all of the Classes (or People) inside the file?

I thought i was using proper code tags? I forgot to fix that error with the cin. I need to make an array of Classes. I am suppoesd to get that info into a function and spit that out. I am not sure how to pass the info.

I fixed the code tags for you. You literally need to type out [code] like this:

[noparse][code=cplusplus]cout << "blah";
[/code][/noparse]

Sorry everyone. The code tags for my question have been fixed, thanks. Does anyone know of a way to pass the info into a function?

Thanks! I was half-way there. I think I am getting the hang of this. Thanks again.

mathrules

Okay how can I access my instance cstudents[50] from my getline function? This is what I have:

#include <iostream>
#include <iomanip>
#include <fstream>
#include<string>
using namespace std;


//void Totalinfo( fstream & information, Class & academic)


// This struct hold information about the name of person, id number, phone #
struct Person
{
    string name;
    int id;
    int iphone;
};

// Struct tells what time the class is
struct Time
{
    string day;
    string hour;

};

struct Course
{
    string cname;
    int icourseid;
};

//struct puts all information together and gives a concise output
struct Class
{
    Person  cstudents[50];
    Person cinstructor;
    Course curriculum;
    //string building;
    Time  nameday;
    string room;
  };

 // int number;

int main ()
{
    fstream information; // command to open file
    Class * ptr;
    ptr= new Class [70];


    //Open the file
        information.open ("lab05-mar.txt", ios:: in);
        if (!information)
        {       // Validate / Return Error if file won't open
                cout<< " There is an error! Cannot open the file properly.";
                return -1;
        }
        Class academic ;

        getline (information, academic.curriculum.cname);
        cout<< "Course name : " << academic.curriculum.cname << endl;


        information >> academic.curriculum.icourseid;

         cout<< "Course number : " ;
        cout << academic.curriculum.icourseid<< endl;


        information >> academic.room;

        cout<< "Room number: ";
        cout<< academic.room << endl;

        information >> academic.nameday.day;

        cout<< "Day: ";
        cout<< academic.nameday.day<< endl;

        information >> academic.nameday.hour;
        cout<< "Time: ";
        cout<< academic.nameday.hour<< endl;

        cout<< "Enrolled students: ";

b/ this is displaying anything/b
        information >> academic.cstudents[50].name;
        cout<< academic.cstudents[50].name;



 //void Totalinfo(fstream & information , Class & adademic)



        return 0;

 }
This article has been dead for over six months. Start a new discussion instead.