954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Structs to Functions

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;

 }
mathrules
Newbie Poster
12 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Just a note...

I can already see some problems...

You're not implementing the 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?

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

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.

mathrules
Newbie Poster
12 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

I fixed the code tags for you. You literally need to type out [code] like this: &#91;code=cplusplus&#93;cout << "blah";&#91;/code&#93;

cscgal
The Queen of DaniWeb
Administrator
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

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?

mathrules
Newbie Poster
12 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

You need something like this?

void Totalinfo( fstream & information, Class& academic)
Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

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

mathrules

mathrules
Newbie Poster
12 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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;

 }
mathrules
Newbie Poster
12 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You