Question:
C++ Compiler error, Cin and Cout do not work?
JeffC
2010-08-16 14:52:00 UTC
What is wrong with this basic group of code?

#include "stdafx.h"
#include

void main ()

{
int score;
std::cout<<"What is the score? "< std::cin>>score>>std::endl;
}

COMPILER ERROR: http://pastebin.com/eudrAnbH
Six answers:
Cubbi
2010-08-16 18:56:41 UTC
I'll try to explain the error message:



It is flagged for the line "std::cin>>score>>std::endl;" and it says, trimming it to the essentials:



"binary '>>' : no operator found which takes a left-hand operand of type istream while trying to match the argument list 'istream, overloaded-function'"



The "overloaded function" in this case is std::endl (it is an IO manipulator, all of which are, physically, defined as functions - you can call std::endl(std::cout); on a line by itself ). Operator << (insertion into a stream) can take IO manipulators as its right-hand arguments, but operator >> (extraction from stream) cannot.



The compiler could not find a version of operator >> that would take std::endl (or, in fact, any overloaded function) as its argument on the right, and that's what the error says (and, to make it a little confusing, it gives the complete list of all possible versions of operator>> that take an istream on the left)



Incidentally, my compiler also flags "void main" as an error:

test.cc:3:12: error: '::main' must return 'int'

This is because "void main" is illegal in C++ and must be written as "int main". However, your compiler was smart enough to guess that you meant "int main" there. It could not guess what did you mean by cin >> endl, though.
?
2010-08-16 15:05:19 UTC
The last line in main() is incorrect.



std::cin>>score>>std::endl;



... should be replaced by:



std::cin>>score;

std::cout<


--



[Note:] Also, it would be easier if you declared the namespace by placing the line:



using namespace std;



... after including . Then, you could refer to cout, cin and endl without all those std:: namespace qualifiers.
?
2016-11-16 14:31:22 UTC
F865ccb4ab0e063e5caa3387c1a8741rst, circulate the funct865ccb4ab0e063e5caa3387c1a8741ons up (formerly ma865ccb4ab0e063e5caa3387c1a8741n). Then, you have another m865ccb4ab0e063e5caa3387c1a8741stakes: 865ccb4ab0e063e5caa3387c1a8741f (s[i] == '@'; i < s.length(); s++) 865ccb4ab0e063e5caa3387c1a8741s no longer val865ccb4ab0e063e5caa3387c1a8741d. It seems l865ccb4ab0e063e5caa3387c1a8741ke you desire a for loop, yet i'm uncertain what k865ccb4ab0e063e5caa3387c1a8741nd. str865ccb4ab0e063e5caa3387c1a8741ng anEma865ccb4ab0e063e5caa3387c1a8741l = l865ccb4ab0e063e5caa3387c1a8741neFromF865ccb4ab0e063e5caa3387c1a8741le.substr(s, e-s); does not make experience e865ccb4ab0e063e5caa3387c1a8741ther. e 865ccb4ab0e063e5caa3387c1a8741s an 865ccb4ab0e063e5caa3387c1a8741nt, and s a str865ccb4ab0e063e5caa3387c1a8741ng. you won't be able to do a subtract865ccb4ab0e063e5caa3387c1a8741on operat865ccb4ab0e063e5caa3387c1a8741on w865ccb4ab0e063e5caa3387c1a8741th those operands.
green meklar
2010-08-16 15:16:21 UTC
Why do you have '>>std::endl' at the end of your std::cin command? Get rid of that and try compiling again.
The Phlebob
2010-08-16 15:00:04 UTC
Not sure, but I suspect this:



std::cin>>score>>std::endl;

}



Can't input to std::endl.



Hope that helps.
Unca Alby
2010-08-16 15:01:39 UTC
std::endl is for output only.



You might call it "write-only memory" :-)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...