C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 12th, 2006, 11:52 AM
poly87 poly87 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 6 poly87 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 m 21 sec
Reputation Power: 0
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)
}

Reply With Quote
  #2  
Old February 12th, 2006, 12:18 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Quote:
Originally Posted by poly87
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)
}


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.

Reply With Quote
  #3  
Old February 12th, 2006, 12:42 PM
poly87 poly87 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 6 poly87 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 m 21 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old February 12th, 2006, 06:36 PM
poly87 poly87 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 6 poly87 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 m 21 sec
Reputation Power: 0
_____

Reply With Quote
  #5  
Old February 13th, 2006, 12:11 AM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
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.

Reply With Quote
  #6  
Old February 13th, 2006, 08:51 AM
poly87 poly87 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 6 poly87 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 42 m 21 sec
Reputation Power: 0
Quote:
Originally Posted by ubergeek
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.

the loop is a less than 2, not a less than or equal to

Reply With Quote
  #7  
Old February 13th, 2006, 10:10 AM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
Red face

whoops...my bad, cirus was right

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Declaration of variables in a structure using an array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT