-1

Here is an explanation (as in the tutorial) of what the example code was supposed to do:

As a rather contrived example, how would we write a for loop that outputs every number from 1 to 100, but stops if the user enters the letter q? We could easily do it with a while loop, and use what we learnt above to make it quite small, but lets do it in a for loop

When I run it, I get an error "No match for 'operator=='...". Now I researched on the net and on SO itself, but all just seem to have advanced answers involving concepts which I have not yet learned.

I have provided the code and also some of the links where I researched. On one of the sites it says that "Yes, basically in C you can't use == to compare strings". On another site they spoke about overloading operators, which I also then researched, but that just confused me even more. On a similar problem here on SO one guy suggested using double quotes instead of single quotes, but I knew that would not work in my case since I used a char and not a const char or string. And I ran it and my suspicions were confirmed. It gave an error: "Invalid conversion from const char to char"

So basically my question is this:

What is wrong with this code used in the tutorial, and what are the alternatives to prevent getting an error?

#include <iostream>

int main()
{
    int i = 0;
    char input = ' ' ;
    for (i=1; i<=100; ++i)
    {
        if ((std::cin >> input) == 'a')
        { 
            break;
        }

    }

    return 0;
}
Community
  • 1
  • 1
Ashley Pieterse
  • 85
  • 1
  • 1
  • 10

1 Answers1

2
std::cin >> input;

returns a reference to std::cin which, unsurprisingly, cannot be compared to a char. You probably want

if (std::cin >> input && input == 'a')

or even better,

if (!(std::cin >> input) || input == 'a')

which will also break if reading input fails.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • Alright, thanks, Baum. I understand now why the error happened in the first place. But it really surprises me that a tutorial would make such an elementary mistake. And I also tried both alternatives you suggested and they worked perfectly. Just one question, though. In the second method you gave, what do you mean with "which will also break if reading "input" fails" ? – Ashley Pieterse Feb 05 '15 at 12:52
  • 1
    @AshleyPieterse `std::cin >> input` will fail if e.g. the user inputs something other than a `char` followed by whitespace (might happen) or if the OS for some odd reason fails to get the user input from the terminal to your program (pretty unlikely, but could technically happen). If your tutorial gets things that easy wrong, you might consider getting a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Baum mit Augen Feb 05 '15 at 12:55
  • Ahhh I get it now. Thanks for explaining it all in such an easy to understand manner, Baum. And yes, I will look for better tuts online now and also for a good book from the link you provided. Thanks again. :) – Ashley Pieterse Feb 05 '15 at 13:06