Advertisement

Comparing strings

Started by May 25, 2002 11:51 PM
5 comments, last by Feblex 22 years, 7 months ago
I seem to be having yet another problem with my code. My problem revolves around the fact that I can''t seem to be able to compare a string with an array entry, like so:
  
char choice;


int main()
 {

 vector<string> questions;
 ifstream ifs( "questions.txt" );
 if( !ifs )
 {
  cerr << "Could not open question file" << endl;
  exit( 1 );
 }
 while( !ifs.eof() )
 {
 string line;
  getline( ifs, line ); // read a line

  questions.push_back( line );
  }

 vector<string> answers;
 ifstream ifa( "answers.txt" );
 if( !ifa )
 {
  cerr << "Could not open answer file" << endl;
  exit( 1 );
 }
 while( !ifa.eof() )
 {
 string line;
  getline( ifa, line ); // read a line

  answers.push_back( line );
  }




 cout << questions[1];
 cout << "\n";
 cout << "\n";
 cin  >> choice;
 if(stricmp("choice",answers[1])==0)
 {
 cout << "\n";
 cout << "Correct!";
 }
 cout << "\n";
 system("PAUSE");
 return 0;
}
  
Upon compiling, I get this error: cannot convert `answers.vector,__default_alloc_template >,allocator,__default_alloc_template > > >::operator [](1)'' from type `basic_string,__default_alloc_template >'' to type `const char *'' Basically, I load the lines of a file into an array so that each line is an entry in the array. I then compare a selected array entry to a user''s input. Any help on this odd error message would be greatly appreciated!
string and char* are not the same type. To compare a string to a string you use ==, to compare a char* to a char* you use strcmp. To compare a string to a char* you generally use ==, the char* is promoted to string (or did they actually make a mixed type operator? don''t remember). You could also use c_str(), a member function of string that returns a char*, but that isn''t as pretty. So replace:

if(stricmp("choice",answers[1])==0)

with

if(choice==answers[1])
Advertisement
Hmm... I get a new error now:


  no match for `char & == basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > &'  


Any explanation for this wild problem?

Edit: had to put the error into source tags because of a confusion by the messageboard over the symbols in the error message.


[edited by - mrbugg on May 26, 2002 1:26:48 AM]
if(answers[1] == "choice") works for me...
quote: Original post by mrbugg
Edit: had to put the error into source tags because of a confusion by the messageboard over the symbols in the error message.

That''s because greater than and less than are HTML mark up tag begin/end characters.

First off, "choice" should NOT be in quotes. Secondly, you could just make choice a string too. Then you shouldn''t have any problem with using if(choice == answers[1]).

One question: what does your answers file look like?

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
I''ve tried not using "" marks. I get the same error message.

My answer file looks like this:

a
b
b
c
d
a
etc.
Advertisement
Ok, that''s what I thought. I''m about to make your program WAY simpler.

Ok, first off keep choice as a char.

Now since all of the answers are just one character, you don''t need to store them in a vector of strings. All you need is an array of chars (or a vector of chars if you want to have a variable number of answers). When you compare the choice, you can just do:

if(choice == answers[whatever])

When you read in the answers, you can use, assuming you use a vector of chars:

char a;
ifa >> a;
answers.push_back(a);
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/

This topic is closed to new replies.

Advertisement