5

I just started teaching myself C++ on the Mac, and I have run into some issues.

I have written some code that allows the user to enter a number and when they hit enter, the number will be returned to the user.

Xcode will absolutely not have it though. Every time I try to run my code, it says that there is an issue with the cin>> thisisanumber; code.

The error comes up and says

Invalid operands to binary expression. Error is on line 10.

What am I doing wrong?

#include <iostream>

using namespace std;

int main()
{
   int thisisanumber();

   cout << "Please enter a number: ";
   cin  >> thisisanumber;
   cin.ignore();
   cout << "You entered"<< thisisanumber <<"\n";
   cin.get();
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
Danny
  • 155
  • 1
  • 1
  • 6
  • The `cin.ignore()` there is normally unnecessary. You might as well just have two `cin.get()` side by side for the pause to work. `cin.sync()` doesn't have guaranteed behaviour, but it's one to look into for an always-working pause if your compiler implements it with the expected behaviour. – chris Jul 23 '12 at 21:03
  • You might be interested in [this](http://stackoverflow.com/questions/1265039/using-std-namespace), which talks about that usage of `using namespace std;`. – chris Jul 23 '12 at 21:07
  • 1
    I just want to say that for your first question, this is very good. You've separated the error line, told us what error you're getting, and provided an [SSCCE](http://sscce.org) for us to use. One note about your wording is that you mean *Every time I try to* ***compile*** *my code*... It can't be run until it's compiled first, and this is a compiler error, as opposed to a runtime error such as a segmentation fault. – chris Jul 23 '12 at 21:16

5 Answers5

10

You've fallen victim to the most vexing parse, which means thisisanumber is being treated as a function. Take out the parentheses and you should be fine:

int thisisanumber;

Also consider making it a bit more readable, such as thisIsANumber. If you ever need to know it, thisIsANumber uses the camel-case naming convention.

chris
  • 60,560
  • 13
  • 143
  • 205
3

Declare your variable without brackets, like

int thisisanumber;

With brackets, it is interpreted as a function, and a function can't be passed as a parameter to the >> operator.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • 2
    Good on you for not saying `operator<<` too. It actually can work with that, which might not be expected: http://ideone.com/TJ3rJ – chris Jul 23 '12 at 21:05
2

Your problem is the so called most vexing parse. Basically everything, which could be parsed as a function declaration will be parsed as such. Therefore the compiler will interpret int thisisanumber(); as a declaration of a function thisisanumber taking zero arguments and returning an int. If you consider this behaviour the problems with cin>>thisisanumber; should be somewhat selfevident.

If you remove the parantheses, changing the variable declaration to int thisisanumber;, your program should behave like you'd expect it to with thisisanumber being a variable of type int.

You might however reconsider your naming conventions, thisisanumber isn't exactly readable. I would suggest going with this_is_a_number, thisIsANumber or ThisIsANumber.

Grizzly
  • 19,595
  • 4
  • 60
  • 78
1
int thisIsANumber;

try making it variable declaration because what you wrote has been interpreted as function.

0

delete () after thisisanumber, because () after thisisanumber means that it's a function, when it's not.