Error on comparing 2 string char arrays in C++
I'm working on a assignment that involve with fstream, loops, arrays, and functions from my introduction to C++ class. THe program is about comparing the users muliple choice answers to the Real test answers to see how many corrects the user got. THe program will open the user_input_answers_file and store the user answers in one of the variable array memory location. THen Compare to the real answers array. The problem is I keep getting error compiling relating to the comparing 2 arrays strings command. I read an article that I couldn't use the == operator when comparing strings. Anyone that can help me?
#include <iostream> //require for i/o stream
#include <fstream> //require for file i/o stream
#include <cstdlib> //require for the exit function
#include <string> //require to use strings functions
using namespace std; //accessing to C++ standard library members and to avoid naming conflicts
ifstream get_file_input( "userAnswers.txt" );
for (int i = 0; i < 4; i++ )
{
getline( get_file_input, userAnswers[i]);
}
//display output
cout << "testAnswer0 is " << testAnswers[0] << endl;
cout << "testAnswer1 is " << testAnswers[1] << endl;
cout << "userAnswer0 is " << userAnswers[0] << endl;
cout << "userAnswer1 is " << userAnswers[1] << endl;
cout << "userAnswer2 is " << userAnswers[2] << endl;
cout << "userAnswer3 is " << userAnswers[3] << endl;
int totalCorrect = 0;
for (int x = 0; i < 4; x++)
{
if (strcmp(userAnswers[x],testAnswers[x]) == 0)
totalCorrect = totalCorrect + 1;
}
//result
cout <<"Total number of corrects: " << totalCorrect << endl;
//Output error if the file cannot open
if ( !get_file_input )
{
cout << "Error: The File could not be opened." << endl;
}
return 0;
}
________________________________________
Got an error
Compiling...
Arrays.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\assignment9\Arrays.cpp(42) : error C2664: 'strcmp' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
Arrays.exe - 1 error(s), 0 warning(s)
______________________________________________________
if I try to replace
if (strcmp(userAnswers[x],testAnswers[x]) == 0) with
if (userAnswers[x] == testAnswers[x])
I get this error
Compiling...
Arrays.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\assignment9\Arrays.cpp(40) : error C2676: binary '==' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a conversion to a
type acceptable to the predefined operator
Error executing cl.exe.
strcmp() is used to compare C strings (nul terminated strings stored in a char array). std::string has overloaded the relational operators, so just use those.