| | |
Structs to Functions
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 12
Reputation:
Solved Threads: 0
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
. Below is my code any help would be greatly appreciated.Thanks,
mathrules
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by cscgal; Oct 11th, 2008 at 3:32 am. Reason: Fixed code tags
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...
...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 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)
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.
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
You need something like this?
C++ Syntax (Toggle Plain Text)
void Totalinfo( fstream & information, Class& academic)
•
•
Join Date: Oct 2008
Posts: 12
Reputation:
Solved Threads: 0
Okay how can I access my instance cstudents[50] from my getline function? This is what I have:
C++ Syntax (Toggle Plain Text)
#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
![]() |
Similar Threads
- C++ array/structs project (C++)
- 2 Programs - Using structs-I/O-Incorrect Data (C++)
- array structs, size swap functions, the amount of days between to dates (C++)
- Functions (C++)
- working with array of structs (C++)
- GTK code uses pointer for structs and not normal instances why? (C)
- Struct in Functions (C)
- structs vs classes? (C++)
Other Threads in the C++ Forum
- Previous Thread: Junk Input Causes the Program to Ignore "cin.ignore(1000, '\n');"
- Next Thread: Returning ifstream object??
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






