943,513 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 973
  • C++ RSS
Oct 11th, 2008
0

Structs to Functions

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathrules is offline Offline
12 posts
since Oct 2008
Oct 11th, 2008
0

Re: Structs to Functions

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...

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Oct 11th, 2008
0

Re: Structs to Functions

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathrules is offline Offline
12 posts
since Oct 2008
Oct 11th, 2008
0

Re: Structs to Functions

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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is online now Online
13,645 posts
since Feb 2002
Oct 11th, 2008
0

Re: Structs to Functions

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathrules is offline Offline
12 posts
since Oct 2008
Oct 11th, 2008
0

Re: Structs to Functions

You need something like this?
C++ Syntax (Toggle Plain Text)
  1. void Totalinfo( fstream & information, Class& academic)
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 11th, 2008
0

Re: Structs to Functions

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

mathrules
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathrules is offline Offline
12 posts
since Oct 2008
Oct 11th, 2008
0

Re: Structs to Functions

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathrules is offline Offline
12 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Junk Input Causes the Program to Ignore "cin.ignore(1000, '\n');"
Next Thread in C++ Forum Timeline: Returning ifstream object??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC