-1

I am attempting to use an overloaded function to get a string.

void get(char prompt[], int size,const std::string b = "")
{
    std::cout << prompt << ": ";  
    std::cin.get(b, size);  
    std::cin.ignore(10, '\n');  
}

Now I did just change the last argument from a character array to a string at the advice of another poster on this site, so I'm a little lost. I'm getting error message at the '.' in between cin and get. I've also tried cin.getline (I have no idea if there's a difference or what it is)

Error message : cannot convert parameter 1 from 'const std::string' to 'char *'

The error has a lot of other stuff, but I think that's the important bit.

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
ShengLong916
  • 159
  • 2
  • 2
  • 6
  • 1
    Your recent series of questions has made it very clear that you really just need to sit down and read [a good book on C++](http://stackoverflow.com/q/388242/636019)... – ildjarn May 02 '12 at 23:27
  • I agree. The problem with my current C++ class is that my instructor wrote the book and it is very confusing :( – ShengLong916 May 03 '12 at 00:07
  • That sounds just.. awful. You have my sympathies. ;-/ – ildjarn May 03 '12 at 00:09

3 Answers3

3

I'm indirectly answering your question by suggesting an alternative method. Here's my implementation, along with an example of how to use it.

#include <string>
#include <iostream>
#include <ostream>

std::string get(std::string const& prompt)
{
  std::string ret;
  std::cout << prompt << ": ";
  getline(std::cin, ret);
  return ret;
}

int main()
{
  std::cout << get("Please enter your answer") << std::endl;

  // or
  std::string prompt("Enter your answer");
  std::string response = get(prompt);
  std::cout << response << std::endl;
}
ildjarn
  • 62,044
  • 9
  • 127
  • 211
James Custer
  • 857
  • 6
  • 12
  • mostly because I would have no idea how to use that in the rest of my program :) – ShengLong916 May 02 '12 at 23:28
  • 2
    @ShengLong916 : Now's the time to learn -- this _is_ the correct/idiomatic approach. – ildjarn May 02 '12 at 23:30
  • @ShengLong916 I've included a compilable example. – James Custer May 02 '12 at 23:31
  • Okay I tried it this way. At the spot where the information should be showing the information that I am getting, it is getting an unhandled exception in the middle of the program. I've never seen this before. What is it? – ShengLong916 May 02 '12 at 23:43
  • 1
    @ShengLong916 : You tell us -- catch the exception and see what type it is and what line of code caused it. – ildjarn May 02 '12 at 23:44
  • @ildjarn Why do you include `ostream`? – mgiuffrida May 02 '12 at 23:46
  • 2
    @eli : Because that's where `std::endl` lives, and including `iostream` is not guaranteed to include `ostream` in C++98/03 (it is in C++11). – ildjarn May 02 '12 at 23:47
  • seeing as how this is a school project and my teacher has us doing it a certain way I'm going to use the other method. Plus since I have another overloaded get function, this one is conflicting with it :s – ShengLong916 May 02 '12 at 23:58
3

When working with strings, you need to use the free function getline from the string header, not the member function getline from iostream. So it would be std::getline(std::cin, b);.

That being said getline won't accept a const string as its argument for the simple reason that the whole point of calling getline is to write to the string. Also note that unless you make b a (non-const) reference, any changes you perform on b inside your get method will not be visible outside of the method since strings are copied if you pass them by value.

The difference between istream::get(char*, streamsize) and istream::getline(char*, streamsize) is that the latter discards the newline character (as does the getline method for strings) while the former does not.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • You would also want to pass in the string by reference, otherwise the string will just be copied and the call to your function will have no effect on the string. Or just return the string instead of passing it as an argument. – mgiuffrida May 02 '12 at 23:24
  • Let's say I try void get(char prompt[], int size, std::string b = ""), std::getline(cin, b); says that there are too few arguments.... – ShengLong916 May 02 '12 at 23:25
  • @ShengLong916 I don't see why it would say that. Are you sure it's complaining about the getline call and not something else? – sepp2k May 02 '12 at 23:29
  • It's giving me two errors on that line : at the . in between cin and getline - no instance of overloaded funtion "std::basic_istream....." matches the argument list, at the end bracket after (cin, b - too few arguments in function call. I am including string and string.h – ShengLong916 May 02 '12 at 23:34
  • also, instead of cin as the argument, should I be using std::cin since I havent included namespace std? – ShengLong916 May 02 '12 at 23:36
  • @ShengLong916 "at the . in between cin and getline" I thought you said the line you had was `std::getline(cin, b)`. That line doesn't have `.` between `cin` and `getline`. And yes, that should be `std::cin`. My bad. – sepp2k May 02 '12 at 23:43
  • OH... ok. so I fixed that but now the information isn't passing. It's just taking the string that I initialized in the constructor. UGH. I don't know how I'll ever get this to work. – ShengLong916 May 03 '12 at 00:00
  • @ShengLong916 Not quite sure what you just said there (What information isn't passing where? What string that you initialized in what constructor?), but maybe you missed the point about changed to `b` not being visible on the outside unless you make it a reference? – sepp2k May 03 '12 at 00:03
  • You are right. I forgot about that. thanks. Unfortunately I can't make heads or tails of pointers or references. Thanks anyways. I'm giving up and turning in what I have. – ShengLong916 May 03 '12 at 00:13
0

http://www.cplusplus.com/reference/iostream/istream/get/

http://www.cplusplus.com/reference/iostream/istream/ignore/

your call to get() doesn't match any of the existing istream methods. it just may end up being recursive if it ever works?

#include <string>

namespace std {
    //I am attempting to use an overloaded function to get a string.
    class Ciostream : public iostream {
        public:
        void get(char prompt[], int size,const std::string b = "")
        {
            cout << prompt << ": ";
            cin.get(b, size);
            cin.ignore(10, '\n');
        }


    };
}
//1.cpp:11:28: error: no matching function for call to 'std::basic_istream<char>::get(const string&, int&)'
Jim Michaels
  • 669
  • 5
  • 9
  • 1
    I'm not sure what the point of your answer is, but defining types inside of `namespace std` is illegal. – ildjarn May 03 '12 at 00:12