-1

This probably has a specific answer, however I'm trying to test the getline() function by making it stop at a "." for instance just so I can assure myself that i'm using it correctly. Looking at online syntax the way it's supposed to be used is as follows

Syntax 1: istream& getline (istream& is, string& str, char delim);

is : It is an object of istream class and tells the function about the stream from where to read the input from.

str : It is a string object, the input is stored in this object after being >read from the stream.

delim : It is the delimitation character which tells the function to stop >reading further input after reaching this character.

However when I tried to test this small library function in my code, it throws an exception stating that can be seen below:

Why does it state that it's overloaded? Or am I missing something very small under my radar?

Community
  • 1
  • 1
Ulivax
  • 9
  • 1
  • 8
  • 5
    Last parameter should be a character, not a string. For example `'.'`, and not `"."`. And don't use getline for parsing strings - read the whole string, and then write your own parser to act on the string. –  Aug 19 '18 at 17:40
  • Ahh thank you very much, wasn't aware that there's a differentiation between the " and ' so I'll be sure to ensure i'm using the correct type for application in the future! – Ulivax Aug 19 '18 at 17:42
  • 1
    I recommend you take a time out with [a reputable text](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and and brush up on the basic syntax. Like it or not, it's really important to get it right. Some little mistakes will result in hard-to-spot logic errors. For example, `^` is the XOR operator, not the exponent operator. The compiler will happily compile `x^2` and the program will give you the wrong answer. – user4581301 Aug 19 '18 at 17:50
  • Please don't post links to picture (or integrate pictures into questions): instead include the actual error messages as text, probably appropriate formatted using markdown (e.g., a compiler error probably wants to be on a separate line, indedented by 4 space characters to make it look like code). – Dietmar Kühl Aug 19 '18 at 17:50

1 Answers1

-1

std::getline has two overloads, one with 2 arguments (of type istream &is and string &str) and the one you listed. Your mistake is that the third argument should by of type char , not of type std::string. When writing the code, double quotes (") makes a char* string, while single quotes (') makes a char.

Also I'd recommend typing out the namespace std::. It's only 5 characters (even if all the typing adds up) and it's generally considered better practice.

Aiden Woodruff
  • 341
  • 3
  • 9
  • Another quick question, why would you want to add the namespace std:: beforehand as good practice, if you include it under header deceleration? I didn't show it in the screenshot but i had used "using namespace std;" would that suffice, or would it be better to remove that namespace and type it manually? – Ulivax Aug 19 '18 at 19:47
  • Most of the time you are probably safe with `using namespace std;`, but many style guides recommend typing it out fully qualified. It's mostly about not polluting the global namespace. Imagine if you included some code that wasn't namespace'd and it defined it's own getline. – Aiden Woodruff Aug 19 '18 at 19:57