• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

error C276

Hello I am trying to make a program for my class and I am trying to make a sentinal value for one of my while loops but for the second condition that looks for the character "q". But I am getting an error:

error C2676: binary '!=' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator

Here is the code if it helps:

// This demonstrates a reasonable way to process a file until end-of-file
// Author: Colin Goble
// Inclass demo 4/28/2005

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main ()
{
      string fName, lName;
      char modality, grade;
      string line;
      ifstream inFile;
      string fileName;
      

      
      cout << "Please enter the input file name: ";
      cin >> fileName;

      inFile.open(fileName.c_str());
      while ( ! inFile.is_open() || fileName != 'q') { // !!!!THIS IS THE LINE IN QUESTION!!!!
            
            cout << "File not found" << endl;
            inFile.clear();
            cout << "Please try again: ";
            cin >> fileName;
            inFile.open(fileName.c_str());
      }
      while ( inFile >> fName >> lName >> modality >> grade ) {
            cout << fName << " " << lName << " " << modality << " " << grade << endl;
      }
      inFile.close();
      return 0;
}

Thank you for your Help!!!

0
Calv1n
Asked:
Calv1n
1 Solution
 
TheC2Commented:
I am unfamiliar with this string class, and I could not find any documentation about it in the MSDN, but from what I saw in the headers, it has a function called "compare" which sounds like what you are looking for.
You might need to assign 'q' to another instance of a string class and then pass it to the compare function.

Also, you can always declare fileName as char*, allocate some memory useing malloc or calloc, and then use the standard strcmp function to compare it to 'q'.

Hope this helps...
0
 
rcarlanCommented:
'q' is a character.
"q" is a string.

You can compare fileName with "q" or you can compare fileName[0] with 'q'.

Radu
0
 
makerpCommented:

// either you are comparing the whole of the filename, in which case you want to use != !q!

 while ( ! inFile.is_open() || fileName != "q")

// or you are comparing the first character, in which case

 while ( ! inFile.is_open() || fileName.at(0) != 'q')

the above assumes the filename is greater than 0 characters - if this is not true using at will throw an out of range exception, therefore i would do something like

 while ( ! inFile.is_open() || (fileName.length() > 0 && fileName.at(0) != 'q'))  
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now