User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 457,747 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,542 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 251 | Replies: 7
Reply
Join Date: Oct 2008
Posts: 12
Reputation: mathrules is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mathrules mathrules is offline Offline
Newbie Poster

Structs to Functions

  #1  
Oct 11th, 2008
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 . Below is my code any help would be greatly appreciated.

Thanks,
mathrules

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. // This struct hold information about the name of person, id number, phone #
  7. struct Person
  8. {
  9. string name;
  10. int iid;
  11. int iphone;
  12. };
  13.  
  14. // Struct tells what time the class is
  15. struct Time
  16. {
  17. int iday;
  18. int ihour;
  19. int imin;
  20. };
  21.  
  22. struct Course
  23. {
  24. string cname;
  25. int icourseid;
  26. };
  27.  
  28. //struct puts all information together and gives a concise output
  29. struct Class
  30. {
  31. Person cstudent[50];
  32. Person cinstructor;
  33. Time itime;
  34. string room;
  35. Course curriculum;
  36.  
  37. };
  38.  
  39. int main ()
  40. {
  41. fstream information; // command to open file
  42. Class * ptr;
  43. ptr= new Class [70];
  44.  
  45.  
  46. //Open the file
  47. information.open ("lab05-mar.txt", ios:: in);
  48. if (!information)
  49. { // Validate / Return Error if file won't open
  50. cout<< " There is an error! Cannot open the file properly.";
  51. return -1;
  52. }
  53. Class academic ;
  54.  
  55. void Totalinfo( fstream & information, something)
  56.  
  57. cout<<"How many classes do you have? " ;
  58. cin << number;
  59. cout<< number;
  60.  
  61. getline (information, academic.curriculum.cname);
  62. cout<< academic.curriculum.cname << endl;
  63. //getline(information,academic.curriculum.cname);\
  64. cout << flush;
  65. information >> academic.curriculum.icourseid;
  66. cout<< academic.curriculum.icourseid;
  67.  
  68.  
  69.  
  70. return 0;
  71.  
  72. }
Last edited by cscgal : Oct 11th, 2008 at 3:32 am. Reason: Fixed code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Location: WA, USA
Posts: 921
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Rep Power: 5
Solved Threads: 93
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Structs to Functions

  #2  
Oct 11th, 2008
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...

  1. 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?
Last edited by Alex Edwards : Oct 11th, 2008 at 1:25 am.
Reply With Quote  
Join Date: Oct 2008
Posts: 12
Reputation: mathrules is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mathrules mathrules is offline Offline
Newbie Poster

Re: Structs to Functions

  #3  
Oct 11th, 2008
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.
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 11,022
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 33
Solved Threads: 117
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: Structs to Functions

  #4  
Oct 11th, 2008
I fixed the code tags for you. You literally need to type out [code] like this: [code=cplusplus]cout << "blah";[/code]
Last edited by cscgal : Oct 11th, 2008 at 3:33 am.
Reply With Quote  
Join Date: Oct 2008
Posts: 12
Reputation: mathrules is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mathrules mathrules is offline Offline
Newbie Poster

Re: Structs to Functions

  #5  
Oct 11th, 2008
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?
Reply With Quote  
Join Date: Sep 2008
Posts: 272
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Rep Power: 2
Solved Threads: 41
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Structs to Functions

  #6  
Oct 11th, 2008
You need something like this?
  1. void Totalinfo( fstream & information, Class& academic)
Reply With Quote  
Join Date: Oct 2008
Posts: 12
Reputation: mathrules is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mathrules mathrules is offline Offline
Newbie Poster

Re: Structs to Functions

  #7  
Oct 11th, 2008
Thanks! I was half-way there. I think I am getting the hang of this. Thanks again.

mathrules
Reply With Quote  
Join Date: Oct 2008
Posts: 12
Reputation: mathrules is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mathrules mathrules is offline Offline
Newbie Poster

Re: Structs to Functions

  #8  
Oct 11th, 2008
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;

 }
Last edited by mathrules : Oct 11th, 2008 at 7:21 pm. Reason: forgot to add array
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:12 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC