Structs to Functions

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 12
Reputation: mathrules is an unknown quantity at this point 
Solved Threads: 0
mathrules mathrules is offline Offline
Newbie Poster

Structs to Functions

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
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 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Structs to Functions

 
0
  #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 Quick reply to this message  
Join Date: Oct 2008
Posts: 12
Reputation: mathrules is an unknown quantity at this point 
Solved Threads: 0
mathrules mathrules is offline Offline
Newbie Poster

Re: Structs to Functions

 
0
  #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 Quick reply to this message  
Join Date: Feb 2002
Posts: 12,047
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: Structs to Functions

 
0
  #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.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 12
Reputation: mathrules is an unknown quantity at this point 
Solved Threads: 0
mathrules mathrules is offline Offline
Newbie Poster

Re: Structs to Functions

 
0
  #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 Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Structs to Functions

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

Re: Structs to Functions

 
0
  #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 Quick reply to this message  
Join Date: Oct 2008
Posts: 12
Reputation: mathrules is an unknown quantity at this point 
Solved Threads: 0
mathrules mathrules is offline Offline
Newbie Poster

Re: Structs to Functions

 
0
  #8
Oct 11th, 2008
Okay how can I access my instance cstudents[50] from my getline function? This is what I have:

  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include<string>
  6. using namespace std;
  7.  
  8.  
  9. //void Totalinfo( fstream & information, Class & academic)
  10.  
  11.  
  12. // This struct hold information about the name of person, id number, phone #
  13. struct Person
  14. {
  15. string name;
  16. int id;
  17. int iphone;
  18. };
  19.  
  20. // Struct tells what time the class is
  21. struct Time
  22. {
  23. string day;
  24. string hour;
  25.  
  26. };
  27.  
  28. struct Course
  29. {
  30. string cname;
  31. int icourseid;
  32. };
  33.  
  34. //struct puts all information together and gives a concise output
  35. struct Class
  36. {
  37. Person cstudents[50];
  38. Person cinstructor;
  39. Course curriculum;
  40. //string building;
  41. Time nameday;
  42. string room;
  43. };
  44.  
  45. // int number;
  46.  
  47. int main ()
  48. {
  49. fstream information; // command to open file
  50. Class * ptr;
  51. ptr= new Class [70];
  52.  
  53.  
  54. //Open the file
  55. information.open ("lab05-mar.txt", ios:: in);
  56. if (!information)
  57. { // Validate / Return Error if file won't open
  58. cout<< " There is an error! Cannot open the file properly.";
  59. return -1;
  60. }
  61. Class academic ;
  62.  
  63. getline (information, academic.curriculum.cname);
  64. cout<< "Course name : " << academic.curriculum.cname << endl;
  65.  
  66.  
  67. information >> academic.curriculum.icourseid;
  68.  
  69. cout<< "Course number : " ;
  70. cout << academic.curriculum.icourseid<< endl;
  71.  
  72.  
  73. information >> academic.room;
  74.  
  75. cout<< "Room number: ";
  76. cout<< academic.room << endl;
  77.  
  78. information >> academic.nameday.day;
  79.  
  80. cout<< "Day: ";
  81. cout<< academic.nameday.day<< endl;
  82.  
  83. information >> academic.nameday.hour;
  84. cout<< "Time: ";
  85. cout<< academic.nameday.hour<< endl;
  86.  
  87. cout<< "Enrolled students: ";
  88.  
  89. b/ this is displaying anything/b
  90. information >> academic.cstudents[50].name;
  91. cout<< academic.cstudents[50].name;
  92.  
  93.  
  94.  
  95. //void Totalinfo(fstream & information , Class & adademic)
  96.  
  97.  
  98.  
  99. return 0;
  100.  
  101. }
Last edited by mathrules; Oct 11th, 2008 at 7:21 pm. Reason: forgot to add array
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC