Hi all.

I can't get this to compile;

#include <iostream>
#include <string>
using namespace std;
 
int main ()
 
{
 
      int number;
      string response;
 
      for (number=5; response!='Y';)
     {
      cout <<number <<endl;
      cout <<"Had Enough (Y/N)?";
      cin>>response;
      }
 
cin.get ();
cin.get ();
 
return 0;
 
}

when i try to compile this message comes up:

no match for 'operator!=' in 'response!='Y''

What does this mean? What can i do to get it to work?

Any help would be greately appreciated.

Dani AI

Generated

The compiler error you saw means the code was trying to compare two incompatible types: a C++ string object and a character literal. 's idea to use a single-character variable fixes the compile-time mismatch, and 's check for both uppercase and lowercase addresses the runtime case-sensitivity. A safer, clearer pattern is to read a whole line, treat an empty line as "ask again", and then test the first character after normalizing its case.

#include <iostream>
#include <string>
#include <cctype>

int main() {
    int value = 5;
    std::string answer;
    while (true) {
        std::cout << value << '\n';
        std::cout << "Stop? (Y/N): ";
        std::getline(std::cin, answer);               // accepts empty input
        if (answer.empty()) continue;                 // repeat if user just pressed Enter
        char ch = static_cast<char>(std::toupper(static_cast<unsigned char>(answer.front())));
        if (ch == 'Y') break;                         // exit loop on Y/y
        // otherwise keep looping
    }
    return 0;
}

Notes and troubleshooting:

  • Use std::getline when you want to detect an empty line (pressing Enter). Operator>> skips whitespace and will not let you detect an empty response.
  • Cast to unsigned char before std::toupper to avoid undefined behavior for negative char values on some platforms.
  • Avoid using a variable in the loop condition before it has a known value; that causes undefined behavior. A do/while or while(true) with an explicit break (as above) keeps the logic clear.
  • If further robustness is needed (ignore leading spaces, accept words like "yes"), trim the line or inspect more of the string before deciding.

This combines the compile-time fix suggested by and the case-handling idea from , while also addressing /'s practical input issues.

Recommended Answers

All 6 Replies

try

char response = 'N';

.

Thanks

This does get it to compile.

Though whether i type N,n or Y,y it just continues to loop the question, never terminating.

try

char response = 'N';

.

Or
response !="Y"

Or
response !="Y"

When i do that it wont compile saying;

expected primary-expression before "char"

char response;

for (number=5; response!='Y' and response != 'y';)

char response;

for (number=5; response!='Y' and response != 'y';)

Cheers - got it to work!!! - though response !='y' seemed to be redunant.

Now i'm playing around to figure out how to make it repeat the question if you just type return - though i have a fealing it is far to complex for me right now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.