Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 2 Weeks Ago   #1 (permalink)
4.0 GHz
 
31337's Avatar
 
intel nvidia

Join Date: Aug 2007
Location: The darkness of your mind
Posts: 785

Rep: 49 31337 is acknowledged by some
Unique Rep: 46
Trader Rating: 4
Default Help with error in my C++ homework... (program done, error fixed :D, 1 little bug..)

Ok, I have the program done, and down to 1 error, but I have no clue what it could be. It'd be great if you guys could help me find this error. (oh, and in case you're wondering, yes the chapter is on functions :P )

Here's the source:

Code:
/*
Name:		Nathan Peterson
Date:		November 4, 2009
Assignment: Chapter 5 Programming Exercise 14
*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

void reset(string& name, int& t1, int& t2, int& t3, int& t4, int& t5, double& avgT);
void readStudent(ifstream inData, string& name, int& t1, int& t2, int& t3, int& t4, int& t5);
void calculateAverage(int t1, int t2, int t3, int t4, int t5, double& avgT);
int calculateGrade(double avgT);
void writeDisplay(ofstream& outData, string name, int t1, int t2, int t3, int t4, int t5, double avgT, char grade);

int main()
{
	int t1=0;
	int t2=0;
	int t3=0;
	int t4=0;
	int t5=0;
	string name="";
	double avgT=0.0;
	int count=0;
	double sumAvg=0.0;
	double classAvg=0.0;
	char grade=' ';

	ifstream inData;
	ofstream outData;
	inData.open("C:\\Ch5PE14in.txt");
	outData.open("C:\\Ch5PE14out.txt");

system("cls");

	cout << "Student" << setw(5) << "Test1" << setw(3) << "Test2" << setw(3) << "Test4" << setw(3) << "Test5" << setw(3) << "Average" << setw(8) << "Grade" <<endl;

	while(inData.eof())
	{	
		reset(name, t1, t2, t3, t4, t5, avgT);
		readStudent(inData, name, t1, t2, t3, t4, t5);
		calculateAverage(t1, t2, t3, t4, t5, avgT);
		grade=(calculateGrade(avgT));
		static_cast<char>(grade);
		writeDisplay(outData, name, t1, t2, t3, t4, t5, avgT, grade);
		sumAvg=sumAvg+avgT;
		count++;
	}

	classAvg=(sumAvg/count);

	cout << "Class Average= " << classAvg <<endl;

	inData.close();
	outData.close();

return(0);
}

void reset(string& name, int& t1, int& t2, int& t3, int& t4, int& t5, double& avgT)
{
	t1=0;
	t2=0;
	t3=0;
	t4=0;
	t5=0;
	name="";
	avgT=0.0;
}

void readStudent(ifstream inData, string& name, int& t1, int& t2, int& t3, int& t4, int& t5)
{
	inData >> name >> t1 >> t2 >> t3 >> t4 >> t5;
}

void calculateAverage(int t1, int t2, int t3, int t4, int t5, double& avgT)
{
	avgT=((t1+t2+t3+t4+t5)/5);
}

int calculateGrade(double avgT)
{
	char grade=' ';

	if(avgT>=.9)
		grade='A';
	else if(avgT>=.8)
		grade='B';
	else if(avgT>=.7)
		grade='C';
	else if(avgT>=.6)
		grade='D';
	else
		grade='F';

	return grade;
}

void writeDisplay(ofstream& outData, string name, int t1, int t2, int t3, int t4, int t5, double avgT, char grade)
{
	outData << name << t1 << t2 << t3 << t4 << t5 <<endl;

	cout << name << t1 << t2 << t3 << t4 << t5 <<endl;
}
The program is supposed to read a name and a series of five test scores from a file, then calculate the average for that student's tests and their grade, as well as the overall class average. Then it needs to output it to a text file, and to the screen. (Oh, and the functions "calculateAverage" and "calculateGrade" are required by the assignment)

The data it's reading is in this format:

Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63

Here's the error I keep getting:
Code:
c:\program files (x86)\microsoft visual studio 9.0\vc\include\fstream(676) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\ios(151) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        This diagnostic occurred in the compiler generated function 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const std::basic_ifstream<_Elem,_Traits> &)'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
Thanks for any help!

System: Obelisk
CPU
E8600
Motherboard
ASUS Rampage Formula X48
Memory
2x2GB G.Skill DDR2-1000
Graphics Card
EVGA 9800GTX+
Hard Drive
PERC5: 2x160GBVelociRaptors(RAID0) 2xWD500GB(RAID0
Sound Card
X-Fi Xtreme Music
Power Supply
Corsair 750TX
Case
Thermaltake Armor
CPU cooling
TRUE Black 120 (2x Ultra Kaze 3000)
GPU cooling
Stock
OS
Windows 7 Ultimate RTM x64
Monitor
Samsung 2253BW

Last edited by 31337 : 2 Weeks Ago at 09:09 AM
31337 is offline   Reply With Quote
Old 2 Weeks Ago   #2 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Nov 2009
Posts: 18

Rep: 2 Nakurusin Unknown
Unique Rep: 2
Trader Rating: 0
Default

You need to pass the ifstream by reference, that is change in both copies of ifstream inData to ifstream& inData & it'll compile!
__________________
System: Primary Comp
CPU
i7 860 @ 3.6GHz (1.31v)
Motherboard
MSI P55-GD65
Memory
Corsair XMS3 4x2GB
Graphics Card
XFX GTX 275 XXX Core Edition
Hard Drive
2x 750GB Samsung Spinpoint F1 + 500GB SG 7200.10
Sound Card
Asus D1
Power Supply
Corsair TX650W
Case
Coolermaster HAF922
CPU cooling
Prolimatech Megahalem
GPU cooling
Stock
OS
Windows 7 x64
Monitor
22" Samsung SyncMaster 2223nw @1680x1050
Nakurusin is offline   Reply With Quote
Old 2 Weeks Ago   #3 (permalink)
4.0 GHz
 
31337's Avatar
 
intel nvidia

Join Date: Aug 2007
Location: The darkness of your mind
Posts: 785

Rep: 49 31337 is acknowledged by some
Unique Rep: 46
Trader Rating: 4
Default

Oh!! Thanks dude! That is awesome, it works now. +REP! Just one last thing it's doing. It seems to be running through the while loop one more time and outputing a line of six zeros and an F to the screen and a line of six zeros to the output file. Shouldn't "while(!inData.eof())" make it stop when it hits the end of the file, so it doesn't try to read a null value? Thanks.

Updated code:
Code:
/*
Name:		Nathan Peterson
Date:		Novembet 4, 2009
Assignment: Chapter 5 Programming Exercise 14
*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

void reset(string& name, int& t1, int& t2, int& t3, int& t4, int& t5, double& avgT);
void readStudent(ifstream& inData, string& name, int& t1, int& t2, int& t3, int& t4, int& t5);
void calculateAverage(int t1, int t2, int t3, int t4, int t5, double& avgT);
int calculateGrade(double avgT);
void writeDisplay(ofstream& outData, string name, int t1, int t2, int t3, int t4, int t5, double avgT, char grade);

int main()
{
	int t1=0;
	int t2=0;
	int t3=0;
	int t4=0;
	int t5=0;
	string name="";
	double avgT=0.0;
	int count=0;
	double sumAvg=0.0;
	double classAvg=0.0;
	char grade=' ';

	ifstream inData;
	ofstream outData;
	inData.open("C:\\Ch5PE14in.txt");
	outData.open("C:\\Ch5PE14out.txt");

system("cls");

	cout << "Student" << setw(8) << "Test1" << setw(6) << "Test2" << setw(6) << "Test4" << setw(6) << "Test5" << setw(6) << "Average" << setw(12) << "Grade" <<endl<<endl;

	while(!inData.eof())
	{	
		reset(name, t1, t2, t3, t4, t5, avgT);
		readStudent(inData, name, t1, t2, t3, t4, t5);
		calculateAverage(t1, t2, t3, t4, t5, avgT);
		grade=(calculateGrade(avgT));
		static_cast<char>(grade);
		writeDisplay(outData, name, t1, t2, t3, t4, t5, avgT, grade);
		sumAvg=sumAvg+avgT;
		count++;
	}

	classAvg=(sumAvg/count);

	cout <<endl;
	cout << "Class Average= " << classAvg <<endl<<endl;

	inData.close();
	outData.close();

return(0);
}

void reset(string& name, int& t1, int& t2, int& t3, int& t4, int& t5, double& avgT)
{
	t1=0;
	t2=0;
	t3=0;
	t4=0;
	t5=0;
	name="";
	avgT=0.0;
}

void readStudent(ifstream& inData, string& name, int& t1, int& t2, int& t3, int& t4, int& t5)
{
	inData >> name >> t1 >> t2 >> t3 >> t4 >> t5;
}

void calculateAverage(int t1, int t2, int t3, int t4, int t5, double& avgT)
{
	avgT=((t1+t2+t3+t4+t5)/5);
}

int calculateGrade(double avgT)
{
	char grade=' ';

	if(avgT>=90)
		grade='A';
	else if(avgT>=80)
		grade='B';
	else if(avgT>=70)
		grade='C';
	else if(avgT>=60)
		grade='D';
	else
		grade='F';

	return grade;
}

void writeDisplay(ofstream& outData, string name, int t1, int t2, int t3, int t4, int t5, double avgT, char grade)
{
	outData << name << t1 << t2 << t3 << t4 << t5 <<endl;

	cout << name << t1 << t2 << t3 << t4 << t5 << avgT << grade <<endl;
}
After I get the while thing sorted, all I've gotta do is work on is formatting

EDIT: I remember my teacher showing us a way to have a word in a char value I think... or at least it was a way that the name (like in my function writeDisplay) would be detected as one character by the program so that setw() would work regardless of the input. Any ideas on that? I mean I could just monkey around with setw() until it works for these values, but I'd like to make it independent if I can.

System: Obelisk
CPU
E8600
Motherboard
ASUS Rampage Formula X48
Memory
2x2GB G.Skill DDR2-1000
Graphics Card
EVGA 9800GTX+
Hard Drive
PERC5: 2x160GBVelociRaptors(RAID0) 2xWD500GB(RAID0
Sound Card
X-Fi Xtreme Music
Power Supply
Corsair 750TX
Case
Thermaltake Armor
CPU cooling
TRUE Black 120 (2x Ultra Kaze 3000)
GPU cooling
Stock
OS
Windows 7 Ultimate RTM x64
Monitor
Samsung 2253BW

Last edited by 31337 : 2 Weeks Ago at 09:18 AM
31337 is offline   Reply With Quote
Old 2 Weeks Ago   #4 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Nov 2009
Posts: 18

Rep: 2 Nakurusin Unknown
Unique Rep: 2
Trader Rating: 0
Default

The one time where it is seeing null values is the run where it decides EOF is reached after that it sets the flag & then the next check the while evaluates false & the loop ends. Personally I don't like using EOF when handling files at all, I tend to use a construct of checking if the stream is ready to use. I generally always check if the file is open before attempting any read or write operations with if (file.is_open()) then it depends on use, for example in some cases I'll use while (getline(file, container)) & othertimes while (file.is_good()) although you can in essence use is_good() to determine whether both the file is open & available for operation it's more a personal preference for me to keep them separate. Again though it depends what you want from it still I tend to steer clear of EOF in general.

Nothing springs to mind except for setting stream width before use with cout.width(<size>); where <size> is number added between the parentheses to which is the width size. In essence though that is what setw() calls anyway. But perhaps I'm just misunderstanding, I'm pretty tired at the mo.
__________________
System: Primary Comp
CPU
i7 860 @ 3.6GHz (1.31v)
Motherboard
MSI P55-GD65
Memory
Corsair XMS3 4x2GB
Graphics Card
XFX GTX 275 XXX Core Edition
Hard Drive
2x 750GB Samsung Spinpoint F1 + 500GB SG 7200.10
Sound Card
Asus D1
Power Supply
Corsair TX650W
Case
Coolermaster HAF922
CPU cooling
Prolimatech Megahalem
GPU cooling
Stock
OS
Windows 7 x64
Monitor
22" Samsung SyncMaster 2223nw @1680x1050
Nakurusin is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 06:05 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.10764 seconds with 8 queries