I have run into an issue with my program. The basis of the program, in the grand scheme of things, is to read input about a student from files. This includes their name, ID, homework scores and test scores. It is then supposed to calculate all the scores and produce a letter grade. I'm currently only working on printing out the name and ID. My issue is i get errors for all my variables saying they are not declared in this scope. I also get a warning about mismatched types for my string (no idea what that means). Example of not declared in scope error:
part1.cpp:38:2: error: ‘infile’ was not declared in this scope
infile.open (out);
^
part1.cpp:38:15: error: ‘out’ was not declared in this scope
infile.open (out);
mismatched types warning:
part1.cpp:11:15: note: mismatched types ‘std::basic_ostream<_CharT, _Traits>’ and ‘int’
outfile << name << " "
^
In file included from /usr/include/c /4.8.2/string:52:0,
from /usr/include/c /4.8.2/bits/locale_classes.h:40,
from /usr/include/c /4.8.2/bits/ios_base.h:41,
from /usr/include/c /4.8.2/ios:42,
from /usr/include/c /4.8.2/ostream:38,
from /usr/include/c /4.8.2/iostream:39,
from part1.cpp:1:
here is the overall code:
#include <iostream>
#include <fstream>
#include <string>
void printOutput (std::string name, std::string Id, float HPfinalscore, float testFinalscore,
float overallScore, string LetterGrade, ostream& outfile)
{
outfile << name << " "
<< Id << " ";
/*
<< HPfinalscore << " "
<< testFinalscore << " "
<< overallScore << " "
<< LetterGrade;
*/
}
int main()
{
ofstream outfile;
ifstream infile;
int num;
std::string file_name;
std::string name;
std::string Id;
std::string LetterGrade;
float HPfinalscore = 0, testFinalscore=0, overallScore=0;
std::cout<<"How many files are you processing: ";
cin>>num;
infile.open (out);
for(int i=0;i<num;i )
{
int count[10];
for(int i=0;i<10;i )
count[i]=0;
char c,s[1000];
std::cout<<"Please input the name of the file: ";
cin>>file_name;
infile.open(file_name.c_str());
if ( !infile)
{
continue;
return 0;
}
outfile.open("Result");
if ( !outfile)
{
std::cout << "Could not open output file \n";
return 0;
}
infile >> name;
while(!infile.eof())
{
infile >> Id;
/*
HPfinalscore = getHPScores(infile);
testFinalscore = getTestscores(infile);
overallScore = EndingScore(HPfinalscore, testFinalscore);
LetterGrade= Grade(overallScore);
*/
printOutput (name, Id, HPfinalscore, testFinalscore, overallScore,
LetterGrade, outfile);
infile >> name; //try to read another name
}
infile.close();
outfile.close();
return 0;
}
}
So my question is how do I get my variables to be declared correctly? Also any info on the warning and how to handle it would be greatly appreciated.
CodePudding user response:
Change ifstream, ostream, and ofstream to std::ifstream, std::ostream, and std::ofstream. Also, get rid of this line:
infile.open(out);