| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Declaration of variables in a structure using an array
I am not sure what is wrong with this code can anyone help me
Code:
#include <iostream>
#include <fstream>
using namespace std;
//define structure Employee
struct Employee {
char FirstName[50];
char SurName[50];
int GraduationYear;
double AvergaeMark;
};
int main()
{
//declaration of variables
Employee EnteredEmpolyee[2]; //I think this line is incorrect
//declaration of ifstream name of infile
ifstream infile("file.txt",ios::in);
//check we are able to open the file
if (!infile){
cout<<"error unable to open file.txt file\n";
exit (1);
}
else {
for (int i=0; i<2; i++){
infile>>EnteredEmpolyee[i]->FirstName;
infile>>EnteredEmployee[i]->Surname;
infile>>EnteredEmployee[i]->GraduationYear;
infile>>EnteredEmployee[i]->AverageMark;
}
//check if we have reached end of file
if (infile.get() !=EOF){
cout<<"Something wrong with input: there is still some data in the file\n";
}
//check if data has been entered corectly
cout<<"Data entered from file: \n"
for (int i=0; i<2; i++){
cout<<EnteredEmpolyee[i].FirstName<<" "
<<EnteredEmployee[i].Surname<<" "
<<EnteredEmployee[i].GraduationYear<<" "
<<EnteredEmployee[i].AverageMark<<"\n"
}
//close the opened file
infile.close();
return(0)
}
|
|
#2
|
|||
|
|||
|
Quote:
the code tries to get 2 records of structure Employee and prints them out on console.What errors are you facing? The line where you declared array of Employee is fine. Problem can occur with the condition of if(infile.get != EOF) the error. It is not wrong but this cond can produce force error as it may be the case that your infile has more room for info and that first two elements of Employee structure cover a part of infile. |
|
#3
|
|||
|
|||
|
I have got further but i am stuck again
Code:
#include <iostream>
#include <fstream>
using namespace std;
//define structure Employee
struct Employee {
char FirstName[50];
char Surname[50];
int GraduationYear;
double AverageMark;
double SalaryOfEmployee;
char University[50];
};
int main()
{
//1. declaration of variables
Employee **PointerEmployee;
//2. dynamic memory allocation
PointerEmployee=new Employee *[2];
for (int i=0; i<2; i++)
PointerEmployee[i]=new Employee;
//3&4. initialisation and Usage
//declaration of ifstream name of infile
ifstream inFile("info.txt",ios::in);
//check we are able to open the file
if (!inFile){
cout<<"error unable to open info.txt file\n";
exit (1);
}
else{
for (int i=0; i<2; i++){
inFile>>PointerEmployee[i]->FirstName;
inFile>>PointerEmployee[i]->Surname;
inFile>>PointerEmployee[i]->GraduationYear;
inFile>>PointerEmployee[i]->AverageMark;
inFile>>PointerEmployee[i]->SalaryOfEmployee;
inFile>>PointerEmployee[i]->University;
}
//check if we have reached end of file
if (inFile.get() !=EOF)
cout<<"Something wrong with input: there is still some data in the file\n";
}
//check if data has been entered corectly
cout<<"Data entered from file: \n";
for (int i=0; i<2; i++)
{
cout<<PointerEmployee[i]->FirstName<<" "
<<PointerEmployee[i]->Surname<<" "
<<PointerEmployee[i]->GraduationYear<<" "
<<PointerEmployee[i]->AverageMark<<" "
<<PointerEmployee[i]->SalaryOfEmployee<<" "
<<PointerEmployee[i]->University<<"\n";
}
//close the opened file
inFile.close();
cout<<"\n";
//get employee information
//write record to file
ofstream outFile("EmployeeInfo.txt",ios::app);
if (outFile.is_open()){
for (int i=0; i<2; i++){
outFile<<PointerEmployee[i]->FirstName<<" "
<<PointerEmployee[i]->Surname<<" "
<<PointerEmployee[i]->GraduationYear<<" "
<<PointerEmployee[i]->AverageMark<<" "
<<PointerEmployee[i]->SalaryOfEmployee<<" "
<<PointerEmployee[i]->University<<" ";
}
//close file
outFile.close();
}
else{
cout<<"File could not be opened.\n";
cin.get();
}
//5. dynamic memory de-allocation
for (i=0;1<2;i++)
delete PointerEmployee[i];
delete PointerEmployee;
cin.get();
return(0);
}
i need to modify the program so that it would initialise an array of anysize depending on the number of records in the text file |
|
#4
|
|||
|
|||
|
_____
|
|
#5
|
|||
|
|||
|
why are you declaring PointerEmployee as an Employee**? Declare it as an Employee* and allocate it with new Employee[2]. then you can skip the loop where you allocate two Employees.
also, Cirus said it tries to get TWO records, but in your loop 0-1-2 is actually THREE records. just making sure you realize that. |
|
#6
|
|||
|
|||
|
Quote:
the loop is a less than 2, not a less than or equal to |
|
#7
|
|||
|
|||
|
whoops...my bad, cirus was right
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Declaration of variables in a structure using an array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|