Student Grade input.txt file red underline

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
 // C++ Ch 9 PE 2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

//Declare a structure variable of type studentType
struct studentType
{	
	// Declare four components
string studentFName, studentLName;
char grade;
int testScore;
}

students[20];

//Function prototypes
int readStudentData(ifstream& inputFile, studentType
	students[]);
void getGrade(studentType students[], int n);
void displayData(ofstream& outputFile, studentType
	students[], int n);
void highestTestScore(ofstream& outputFile, studentType
	students[], int n);
bool openInputFile(ifstream& inputfile,char filename[] );
void openOutputFile(ofstream& outputFile,char filename[] );

//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");

		//Function Calls
		n = readStudentData(inputFile, students);
		getGrade(students, n);
		displayData(outputFile, students, n);
		highestTestScore(outputFile, students, n);

		//Close output file
		outputFile.close();

		//Close input file
		inputFile.close();
	}

	//Pause the run
	system("pause");

	return 0;
}


	//Method definition of openInputFile
	bool openInputFile(ifstream& inputFile, char 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 displyData(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;

	}


	//Method definition of openOutputFile
	void openOutputFile(ofstream& outputFile, char filename[])
	{
		outputFile.open(filename);
	}
Change
1
2
bool openInputFile(ifstream& inputfile,char filename[] );
void openOutputFile(ofstream& outputFile,char filename[] );

to
1
2
bool openInputFile(ifstream& inputfile,const char* filename );
void openOutputFile(ofstream& outputFile, const char* filename );

and in their defintions.

And then stop using char* entirely and use std::string
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?

Thanks
Hello serenitytek,

Where can I find out how to use std::string? Is it simple?


Yes and offers much more than a character array.

You can start here: http://www.cplusplus.com/reference/string/string/


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.

Hope that helps,

Andy
Hello serenitytek,

Should you have any more problems please post the input file, or a fair sample if large, so everyone will be working with the same information.

Andy
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.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

//Declare a structure variable of type studentType
struct studentType
{
// Declare four components
string studentFName, studentLName;
char grade;
int testScore;
}

students[20];

//Function prototypes
int readStudentData(ifstream& inputFile, studentType
students[]);
void getGrade(studentType students[], int n);
void displayData(ofstream& outputFile, studentType
students[], int n);
void highestTestScore(ofstream& outputFile, studentType
students[], int n);
//bool openInputFile(ifstream& inputfile,char filename[] );
//void openOutputFile(ofstream& outputFile,char filename[] );
//bool openInputFile(ifstream& inputfile, const char* filename[]);
//void openOutputFile(ofstream& outputFile, const char* filename[]);
bool openInputFile(ifstream& inputfile, const char* std::string[]);
void openOutputFile(ofstream& outputFile, const char* std::string[]);

//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");

//Function Calls
n = readStudentData(inputFile, students);
getGrade(students, n);
displayData(outputFile, students, n);
highestTestScore(outputFile, students, n);

//Close output file
outputFile.close();

//Close input file
inputFile.close();
}

//Pause the run
system("pause");

return 0;
}


//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;

}


//Method definition of openOutputFile
void openOutputFile(ofstream& outputFile, const char* std::string[])
{
outputFile.open(std::string);
}


input.txt

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
Fixed with tutor's help. Thanks




#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

//Declare a structure variable of type studentType
struct studentType
{
// Declare four components
string studentFName, studentLName;
char grade;
int testScore;
}

students[20];

//Function prototypes
int readStudentData(ifstream& inputFile, studentType
students[]);
void getGrade(studentType students[], int n);
void displayData(ofstream& outputFile, studentType
students[], int n);
void highestTestScore(ofstream& outputFile, studentType
students[], int n);
bool openInputFile(ifstream& inputfile, string filename);
void openOutputFile(ofstream& outputFile, string filename);
//bool openInputFile(ifstream& inputfile, const char* filename[]);
//void openOutputFile(ofstream& outputFile, const char* filename[]);
//bool openInputFile(ifstream& inputfile, const char* std::string[]);
//void openOutputFile(ofstream& outputFile, const char* std::string[]);

//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");

//Function Calls
n = readStudentData(inputFile, students);
getGrade(students, n);
displayData(outputFile, students, n);
highestTestScore(outputFile, students, n);

//Close output file
outputFile.close();

//Close input file
inputFile.close();
}

//Pause the run
system("pause");

return 0;
}


//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;

}


//Method definition of openOutputFile
void openOutputFile(ofstream& outputFile, string filename)
{
outputFile.open(filename);
}


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


Higest test score: 99

Student names having the highest test score:

Charle,De
Hasty,Gavin
Last edited on
Hello serenitytek,

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
out << left << std::setw(14) << students[i].studentLName + ',' // <--
	<< std::setw(14) << students[i].studentFName
	<< std::right << ::setw(3) << students[i].testScore
	<< std::setw(9) << ' ' << students[i].grade << endl;
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


Hope that helps,

Andy
Topic archived. No new replies allowed.