•
•
•
•
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
![]() |
•
•
Join Date: Oct 2008
Posts: 12
Reputation:
Rep Power: 1
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
cplusplus 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.
•
•
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 11,022
Reputation:
Rep Power: 33
Solved Threads: 117
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
Do you run a computer-related website? Feature it in our niche link directory!
Do you run a computer-related website? Feature it in our niche link directory!
You need something like this?
cplusplus Syntax (Toggle Plain Text)
void Totalinfo( fstream & information, Class& academic)
•
•
Join Date: Oct 2008
Posts: 12
Reputation:
Rep Power: 1
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;
}
Last edited by mathrules : Oct 11th, 2008 at 7:21 pm. Reason: forgot to add array
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
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??



Linear Mode