Write your question here.
Hi
Using Visual Studio for a C++ homework Ch 9 PE 2 Malik. I can't see how to get the red lined input and output txt files to allow program to run. When hover over red lines error is (argument of type "const char*" is incompatible with parameter of type "char*") I am going on my 6th hour into this. Have tried two other program variations and unable to run. My test PC is Win 10 8gb and VStudio 2017. Thanks
Hi thanks so much for replying. I replaced the code and definitions and got a couple errors. Can you help me understand?
Severity Code Description Project File Line Suppression State
Error (active) E0304 no instance of overloaded function "std::basic_ofstream<_Elem, _Traits>::open [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list C++ Ch 9 PE 2 x:\CMC\Fall 2018\Courses\C++\Homework\Due Nov 16\C++ Ch 9 PE 2\C++ Ch 9 PE 2\C++ Ch 9 PE 2.cpp 164
Error (active) E0304 no instance of overloaded function "std::basic_ifstream<_Elem, _Traits>::open [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list C++ Ch 9 PE 2 x:\CMC\Fall 2018\Courses\C++\Homework\Due Nov 16\C++ Ch 9 PE 2\C++ Ch 9 PE 2\C++ Ch 9 PE 2.cpp 74
Error C2664 'void std::basic_ifstream<char,std::char_traits<char>>::open(const char *,std::ios_base::open_mode)': cannot convert argument 1 from 'const char *[]' to 'const std::experimental::filesystem::v1::path &' C++ Ch 9 PE 2 x:\cmc\fall 2018\courses\c++\homework\due nov 16\c++ ch 9 pe 2\c++ ch 9 pe 2\c++ ch 9 pe 2.cpp 74
Error C2664 'void std::basic_ofstream<char,std::char_traits<char>>::open(const char *,std::ios_base::open_mode)': cannot convert argument 1 from 'const char *[]' to 'const std::experimental::filesystem::v1::path &' C++ Ch 9 PE 2 x:\cmc\fall 2018\courses\c++\homework\due nov 16\c++ ch 9 pe 2\c++ ch 9 pe 2\c++ ch 9 pe 2.cpp 164
Where can I find out how to use std::string? Is it simple?
When I changed the prototype and function definition to std::string the errors went away.
Since you have included the C++ "string" header file you should use "std:string" and not the character array. From C++11 on the open statements can use a "std::string" for the file name.
Repeater's solution will work for what you have written.
The next problem that arose was with "displayData". Between the prototype, function call and function definition you need to check your spelling.
Hi Andy, here's what I have so far. putting std::string in for filename gave some errors. If you could advise further.
Thanks, Pete
Error List:
Severity Code Description Project File Line Suppression State
Error C2751 'std::string': the name of a function parameter cannot be qualified C++ Ch 9 PE 2 e:\cmc\fall 2018\courses\c++\homework\due nov 16\c++ ch 9 pe 2\c++ ch 9 pe 2\c++ ch 9 pe 2.cpp 36
Error C2751 'std::string': the name of a function parameter cannot be qualified C++ Ch 9 PE 2 e:\cmc\fall 2018\courses\c++\homework\due nov 16\c++ ch 9 pe 2\c++ ch 9 pe 2\c++ ch 9 pe 2.cpp 35
Error C2664 'bool openInputFile(std::ifstream &,const char *[])': cannot convert argument 2 from 'const char [10]' to 'const char *[]' C++ Ch 9 PE 2 e:\cmc\fall 2018\courses\c++\homework\due nov 16\c++ ch 9 pe 2\c++ ch 9 pe 2\c++ ch 9 pe 2.cpp 48
Error C2664 'void openOutputFile(std::ofstream &,const char *[])': cannot convert argument 2 from 'const char [11]' to 'const char *[]' C++ Ch 9 PE 2 e:\cmc\fall 2018\courses\c++\homework\due nov 16\c++ ch 9 pe 2\c++ ch 9 pe 2\c++ ch 9 pe 2.cpp 51
Error C2751 'std::string': the name of a function parameter cannot be qualified C++ Ch 9 PE 2 e:\cmc\fall 2018\courses\c++\homework\due nov 16\c++ ch 9 pe 2\c++ ch 9 pe 2\c++ ch 9 pe 2.cpp 74
Error C2275 'std::string': illegal use of this type as an expression C++ Ch 9 PE 2 e:\cmc\fall 2018\courses\c++\homework\due nov 16\c++ ch 9 pe 2\c++ ch 9 pe 2\c++ ch 9 pe 2.cpp 76
Error C2751 'std::string': the name of a function parameter cannot be qualified C++ Ch 9 PE 2 e:\cmc\fall 2018\courses\c++\homework\due nov 16\c++ ch 9 pe 2\c++ ch 9 pe 2\c++ ch 9 pe 2.cpp 164
Error C2275 'std::string': illegal use of this type as an expression C++ Ch 9 PE 2 e:\cmc\fall 2018\courses\c++\homework\due nov 16\c++ ch 9 pe 2\c++ ch 9 pe 2\c++ ch 9 pe 2.cpp 166
Program:
// C++ Ch 9 PE 2.cpp : Defines the entry point for the console application.
//
//Declare a structure variable of type studentType
struct studentType
{
// Declare four components
string studentFName, studentLName;
char grade;
int testScore;
}
//Method definition of openInputFile
bool openInputFile(ifstream& inputFile, const char* std::string[])
{
inputFile.open(std::string);
if (inputFile.fail())
{
cout << "File did not open. Check it one again.\n";
system("pause");
return false;
}
return true;
}
//Method definition of readStudentData
int readStudentData(ifstream& inputFile,
studentType students[])
{
int n = 0;
inputFile >> students[n].studentFName
>> students[n].studentLName >> students[n].testScore;
while (inputFile)
{
n++;
inputFile >> students[n].studentFName >>
students[n].studentLName >> students[n].testScore;
}
return n;
}
//Method definition of getGrade
void getGrade(studentType students[], int n)
{
int i;
for(i=0; i < n; i++)
switch ((int)(students[i].testScore / 10))
{
case 10:
case 9: students[i].grade = 'A';
break;
case 8: students[i].grade = 'B';
break;
case 7: students[i].grade = 'C';
break;
case 6: students[i].grade = 'D';
break;
default: students[i].grade = 'F';
break;
}
}
//Method definition of displayData
void displayData(ofstream& out, studentType students[],
int n)
{
out << "last_name, First_name,Score\t\n";
out << "-----------------------------\n";
for (int i = 0; i < n; i++)
{
out << left << students[i].studentLName << ",\t"
<< students[i].studentFName << " \t";
out << students[i].testScore << "\t\t"
<< students[i].grade << endl;
}
}
//Method definition of highestTestScore
void highestTestScore(ofstream& out, studentType
students[],int n)
{
int max = 0, i;
for(i = 1; i < n; i++)
if(students[i].testScore > students[max].testScore)
max = i;
out << "\n\nHigest test score: "
<<students[max].testScore << endl;
out << "\nStudent names having the highest test score:"
<<endl;
for(i = 0; i < n; i++)
if(students[i].testScore == students[max].testScore)
out << students[i].studentLName << ","
<<students[i].studentFName<< endl;
Amith Bob 66
Charles De 99
Enrich Frank 55
Gavin Hasty 99
Iris James 88
Krish Lee 25
Martin Nash 91
Omran Peter 77
Qlark Rachel 86
Stev Tea 70
Umran Veris 25
Wallt Xioms 44
Yeswan Zion 66
Lee Clark 54
Josh Philip 89
Mark Mike 76
Thomas Joe 30
Bob Robert 89
Antny Joe 82
Matt Tom 89
//Declare a structure variable of type studentType
struct studentType
{
// Declare four components
string studentFName, studentLName;
char grade;
int testScore;
}
//Main method
int main()
{
// Declare variables
int n;
bool status;
//Create the input and output files
ifstream inputFile;
ofstream outputFile;
status = openInputFile(inputFile, "input.txt");
if (status)
{
openOutputFile(outputFile, "output.txt");
//Method definition of openInputFile
bool openInputFile(ifstream& inputFile, string filename)
{
inputFile.open(filename);
if (inputFile.fail())
{
cout << "File did not open. Check it one again.\n";
system("pause");
return false;
}
return true;
}
//Method definition of readStudentData
int readStudentData(ifstream& inputFile,
studentType students[])
{
int n = 0;
inputFile >> students[n].studentFName
>> students[n].studentLName >> students[n].testScore;
while (inputFile)
{
n++;
inputFile >> students[n].studentFName >>
students[n].studentLName >> students[n].testScore;
}
return n;
}
//Method definition of getGrade
void getGrade(studentType students[], int n)
{
int i;
for (i = 0; i < n; i++)
switch ((int)(students[i].testScore / 10))
{
case 10:
case 9: students[i].grade = 'A';
break;
case 8: students[i].grade = 'B';
break;
case 7: students[i].grade = 'C';
break;
case 6: students[i].grade = 'D';
break;
default: students[i].grade = 'F';
break;
}
}
//Method definition of displayData
void displayData(ofstream& out, studentType students[],
int n)
{
out << "Last name, First name Score Grade\t\n";
out << "-------------------------------------------------\n";
for (int i = 0; i < n; i++)
{
out << left << students[i].studentLName << ",\t"
<< students[i].studentFName << " \t\t";
out << students[i].testScore << "\t\t"
<< students[i].grade << endl;
}
}
//Method definition of highestTestScore
void highestTestScore(ofstream& out, studentType
students[], int n)
{
int max = 0, i;
for (i = 1; i < n; i++)
if (students[i].testScore > students[max].testScore)
max = i;
out << "\n\nHigest test score: "
<< students[max].testScore << endl;
out << "\nStudent names having the highest test score:\n"
<< endl;
for (i = 0; i < n; i++)
if (students[i].testScore == students[max].testScore)
out << students[i].studentLName << ","
<< students[i].studentFName << endl;
Last name, First name Score Grade
-------------------------------------------------
Amith, Bob 66 D
Charle, De 99 A
Enrich, Frank 55 F
Hasty, Gavin 99 A
James, Iris 88 B
Lee, Krish 25 F
Nash, Martin 91 A
Omran, Peter 77 C
Qlark, Rachel 86 B
Tea, Stev 70 C
Veris, Umran 25 F
Xioms, Wallt 44 F
Zion, Yeswan 66 D
Clark, Lee 54 F
Philip, Josh 89 B
Mike, Mark 76 C
Thomas, Joe 30 F
Robert, Bob 89 B
Antny, Joe 82 B
Matt, Tom 89 B
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Sorry for the delay. My computer did a restart yesterday and I a still trying to find everything I was working on.
By now I hope that you have learned that "std::string" or just "string" is a type like "int" or "char". And maybe I was nor completely clear when I said:
Since you have included the C++ "string" header file you should use "std:string" and not the character array. From C++11 on the open statements can use a "std::string" for the file name.
I was trying to say the line openInputFile(ifstream& inputfile, char filename[]); should be openInputFile(ifstream& inputfile, std::string filename);. Which I see that you did figure out.
in the "displayData" function you might like the way this looks in the output file:
Last name, First name Score Grade
-------------------------------------------------
Bob, Amith 66 D
De, Charles 99 A
Frank, Enrich 55 F
Hasty, Gavin 99 A
James, Iris 88 B
Lee, Krish 25 F
Nash, Martin 91 A
Peter, Omran 77 C
Rachel, Qlark 86 B
Tea, Stev 70 C
Veris, Umran 25 F
Xioms, Wallt 44 F
Zion, Yeswan 66 D
Clark, Lee 54 F
Philip, Josh 89 B
Mike, Mark 76 C
Joe, Thomas 30 F
Robert, Bob 89 B
Joe, Antny 82 B
Tom, Matt 89 B
Higest test score: 99
Student names having the highest test score:
De,Charles
Hasty,Gavin