getline and chars...

closed account (91vUpfjN)
The tutorial advises me to use getline unless I specifically for some rather silly reason need something else for input. At least instead of cin... I've got a good list of sources saying cin == very very bad and that's not including the Cplusplus tutorial statement which is somewhat ambiguous including the Cplusplus tutorial (which is pretty adamant that cin is very bad to use generally.

Now... I can quickly and easily

getline( cin , strings)

Though I require streams for integers

1
2
getline( cin, string_holder)
stringstream( string_holder) >> int variable


This also works for floats and bool (so far as I can tell). However it doesn't appear to be working for something of char type. Which is quite confusing.

Would someone please decode:

ambiguous overload foroperator>>’ (operand types are ‘std::stringstream {aka std::basic_stringstream<char>}’ and ‘char’)

I would really appreciate it.

Further more...
getline( cin, char variable)
Doesn't work.

1
2
3
4
Invalid arguments ' Candidates are: std::basic_istream<#0,#1> & 
 getline(std::basic_istream<#0,#1> &, std::basic_string<#0,#1,#2> &) 
 std::basic_istream<#0,#1> & getline(std::basic_istream<#0,#1> &, 
 std::basic_string<#0,#1,#2> &, #0) '
Last edited on
The tutorial advises me to use getline unless I specifically for some rather silly reason need something else for input.
operator>> respects white space characters as seperators in the stream. If you read a string, you won't get the white space characters.

At least instead of cin... I've got a good list of sources saying cin == very very bad
There's a lot going on in a stream. You'd struggle to define what it means for two streams to be equal.

As for your compile error, if you examine the full error, it's telling you that there is a number of possible matches and so the call on operator>>(char) is ambiguous. If you look at the header mentioned in the error, you'll see that there's no operator>>(char). Very odd.

I knocked up this sample:
1
2
3
4
5
6
7
8
#include <sstream>

int main()
{
        char c;
        std::istringstream iss("hello");
        iss >> c;
}


Now, that works. Why?

Let's take a look.

Compile for debug:
 
CXXFLAGS="-g"  make s2

g++ -g    s2.cc   -o s2


Check out the symbol table:
 
nm -C s2

0000000000600f30 B __bss_start
0000000000600f30 b completed.6330
0000000000600f20 D __data_start
0000000000600f20 W data_start
00000000004008d0 t deregister_tm_clones
0000000000400940 t __do_global_dtors_aux
0000000000600c98 t __do_global_dtors_aux_fini_array_entry
0000000000600f28 D __dso_handle
0000000000600ca8 d _DYNAMIC
0000000000600f30 D _edata
0000000000600f38 B _end
0000000000400af4 T _fini
0000000000400960 t frame_dummy
0000000000600c90 t __frame_dummy_init_array_entry
0000000000400c68 r __FRAME_END__
0000000000600eb0 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
                 U __gxx_personality_v0@@CXXABI_1.3
00000000004007b8 T _init
0000000000600c98 t __init_array_end
0000000000600c90 t __init_array_start
0000000000400b00 R _IO_stdin_used
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000600ca0 d __JCR_END__
0000000000600ca0 d __JCR_LIST__
                 w _Jv_RegisterClasses
0000000000400af0 T __libc_csu_fini
0000000000400a80 T __libc_csu_init
                 U __libc_start_main@@GLIBC_2.2.5
000000000040098d T main
                 w __pthread_key_create
0000000000400900 t register_tm_clones
00000000004008a0 T _start
0000000000600f30 D __TMC_END__
                 U _Unwind_Resume@@GCC_3.0
                 U std::allocator<char>::allocator()@@GLIBCXX_3.4
                 U std::allocator<char>::~allocator()@@GLIBCXX_3.4
                 U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)@@GLIBCXX_3.4
                 U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()@@GLIBCXX_3.4
                 U std::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >::basic_istringstream(std::string const&, std::_Ios_Openmode)@@GLIBCXX_3.4
                 U std::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_istringstream()@@GLIBCXX_3.4
                 U std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char&)@@GLIBCXX_3.4
0000000000400b10 r __gthread_active_p()::__gthread_active_ptr


So it's using a global operator>>(char). But it's not being used it on the temporary, hence the error. The compiler tries to apply char to the operator>> methods in istream, fails and prints all the possible candidates.
Last edited on
closed account (91vUpfjN)
Would you mind rephrase that last paragraph. I'm hoping the whole thing will make more sense that way (I doubt it).

Is there any solution to getting a user to input a character without using cin directly?
Topic archived. No new replies allowed.