Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.
/* write a prog to take the name of the user and output it with a thank you stamement*/

#include<iostream>
using namespace std;
int main()
{
 char ch[20];
 cout<<"enter your name pls"<<endl;
  cin >> ch>>endl;
  cout<<"thank you::"<<ch<<endl;
 return 0;
}

Output is:

ff@ff-desktop:~/CCCP/ch1$ g++ 2.cpp -o 2
2.cpp: In function ‘int main()’:
2.cpp:9: error: no match for ‘operator>>’ in ‘std::operator>> [with _CharT = char, _Traits = std::char_traits<char>](((std::basic_istream<char, std::char_traits<char> >&)(& std::cin)), ((char&)(& ch))) >> std::endl’
/usr/include/c++/4.4/istream:119: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:123: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:130: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:166: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:170: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:173: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:177: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:180: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:184: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:188: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:193: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:197: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:202: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:206: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:210: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:214: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:238: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/istream:741: note: std::basic_istream<_CharT2, _Traits2>& std::operator>>(std::basic_istream<_CharT2, _Traits2>&, _CharT2*) [with _CharT2 = char, _Traits2 = std::char_traits<char>, _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/bits/istream.tcc:853: note: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) [with _CharT = char, _Traits = std::char_traits<char>]
ff@ff-desktop:~/CCCP/ch1$

Can anyone please tell me what's wrong?

ACTIONPARSNIP(ANDREW-WOODHEAD666) suggested this:

Try changing: #include <iostream>

To: #include "iostream"

Or try: #include <iostream.h>

or: #include "iostream.h"

And after doing these changes, I got the following.

Here is the output for the same program above, with "iostream.h" instead of <iostream.h>:

2.cpp:2:21: error: iostream.h: No such file or directory
2.cpp: In function ‘int main()’:
2.cpp:8: error: ‘cout’ was not declared in this scope
2.cpp:8: error: ‘endl’ was not declared in this scope
2.cpp:9: error: ‘cin’ was not declared in this scope

Can anyone please help in this and suggest me whats wrong?

share|improve this question
    
It is an error in your program ,but it is offtopic here , this may ontopic in stackoverflow.com –  Tachyons Jul 27 '12 at 23:10
    
I'm not sure this is off-topic, because compilers / STL implementations on some other platforms might actually accept an overload that allows calling operator>> with an std::istream on the left and an std::ostream& on the right. But I do strongly agree that this would be better on Stack Overflow, as it's not fundamentally about a platform-specific issue. –  Eliah Kagan Jul 27 '12 at 23:13
add comment

closed as off topic by James Henstridge, Anwar Shah, hbdgaf, Minato Namikaze, izx Aug 18 '12 at 6:58

Questions on Ask Ubuntu are expected to relate to Ubuntu within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

#include <iostream> was correct. (Actually, you had #include<iostream>, without a space, but that is correct too.) Including "iostream.h" or <iostream.h> instead are actually both wrong, though some compilers and standard libraries will accept them.

This problem is because the standard library doesn't provide an overload of operator>> that can take an std::istream on the left and an std::ostream& on the right. To put it more plainly, this is where your error is happening:

cin >> ch>>endl;
  • Please note that this is equivalent to (cin >> ch) >> endl, and it's the >> outside the parentheses where the error is happening, though changing the parentheses to try to pass ch and endl as arguments to >> would also be wrong.
  • endl is an ostream& and cin is an istream. >> cannot operate with an istream on the left and an ostream& on the right, as the STL doesn't define any overload that can be selected for such an expression.

You can use endl with cout, but you cannot use it like this with cin.

How to fix this depends on what you intended to do.

  • If your goal is to print a newline after taking input, add a separate statement (cout << endl) after this, or add endl to the subsequent statement with cout that you already have.

  • If your goal is to stop taking input when a newline is encountered, that will already happen using << with cin, so just remove the endl part altogether:

    cin >> ch;
    
  • If your goal is to keep taking input when any whitespace except a newline is encountered (that is, you want to make sure to read exactly one line of input into a char array orstd::string), you will have to use a more complex expression, or perhaps not even use operator>> with cin at all.

    Since it's not clear that is what you want, and solutions for doing that are not platform specific (whereas what overloads are available in an STL implementation is at least theoretically platform-specific), details about how to do that are beyond the scope of this answer. But if that's what you want to do, I recommend asking on Stack Overflow.

share|improve this answer
add comment

You should use:#include <iostream> (don't forget about space) and using namespace std;

Compile it by following this syntax: g++ -o 2 2.cpp

If this doesn't work then: find /usr/include -name iostream -type f -print and add it to your include path

share|improve this answer
    
You don't need a space (though personally I think it looks better with one). The original error was not due to a wrong #include directive. <iostream> was being properly included. The modified source code did introduce a wrong #include directive, but just fixing that won't fix the original error (which is due to an attempt to call an overload of operator>> that <iostream> does not provide). –  Eliah Kagan Jul 27 '12 at 23:24
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.