|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Help with error in my C++ homework... (program done, error fixed :D, 1 little bug..)
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
4.0 GHz
![]()
Join Date: Aug 2007
Location: The darkness of your mind
Posts: 785
Rep: 49
![]() Unique Rep: 46
Trader Rating: 4
|
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 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> ]
Last edited by 31337 : 2 Weeks Ago at 09:09 AM |
|||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
New to Overclock.net
|
You need to pass the ifstream by reference, that is change in both copies of ifstream inData to ifstream& inData & it'll compile!
__________________
|
|||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||||
|
4.0 GHz
![]()
Join Date: Aug 2007
Location: The darkness of your mind
Posts: 785
Rep: 49
![]() Unique Rep: 46
Trader Rating: 4
|
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;
}
![]() 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.
Last edited by 31337 : 2 Weeks Ago at 09:18 AM |
|||||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
New to Overclock.net
|
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.
|
|||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|