0

This is the code:

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>

int main()
{
      char str1[100];
      char str2[100];
      getline(str1,100,'\n');
      getline(str2,100,'\n');
      return 0;
}

I want to read string and store it in array so I in am using this method but it shows the following error on compiling

   [Error]138: error: `getline' was not declared in this scope

What is the method of reading string in array.?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Rahul Bhojwani
  • 87
  • 1
  • 11

2 Answers2

5

getline is a member of cin (which belongs to the std namespace). You need to say:

std::cin.getline(...)

Test.

Though I would personally recommend this:

#include <string>
#include <iostream>

int main()
{
    std::string str1;
    std::string str2;
    std::getline(std::cin, str1);
    std::getline(std::cin, str2);
    return 0;
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • 1
    I think Mr OP wants a smelly `char str1[100]` array. So maybe you should tell him to `strcpy` or persuade him against *C-Style* strings. – phoeagon Jul 05 '13 at 13:05
  • @phoeagon Yeah, I figured out that OP wanted to use `cin.getline` instead, which works with `char*`'s. Edited. – Bernhard Barker Jul 05 '13 at 13:18
  • no I want the string in an array and when i am trying cin.getline() it says error: cin was not declared in scope.. – Rahul Bhojwani Jul 05 '13 at 13:19
  • @RahulBhojwani Yes, because you must use `std::cin.getline(...)`. See [this](https://ideone.com/6ZbzIt) for a runnable example. – Bernhard Barker Jul 05 '13 at 13:25
  • @Dukeling Thanks.. What is this std:: can you provide some link where I can get stuff about it?? – Rahul Bhojwani Jul 05 '13 at 13:29
  • @RahulBhojwani Namespace `std` contains all the classes, objects and functions of [the C++ Standard Library](http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library). [Reference](http://en.cppreference.com/w/cpp). – Bernhard Barker Jul 05 '13 at 13:36
-3

Please use gets() or (for Microsoft compiler gets_s() (with buffer length check ))

#include <iostream>
#include <string>

int main()
{
  char str1[100];
  char str2[100];

  gets(str1);
  gets(str2);

  return 0;
}
mkag
  • 106
  • 12
  • Never use `gets`, use `fgets` or `gets_s`, and `gets_s` is standard C now (since C11) – Yu Hao Jul 05 '13 at 13:29
  • -1: Sorry, but recommending `gets` is wrong in every circumstance. – milleniumbug Jul 05 '13 at 14:03
  • as per the question, it seems Rahul is a school going and he hardly use new C++11 thats why gets recommended. – mkag Jul 05 '13 at 17:21
  • @mkag W.H.A.T.? You realise that this question has hardly anything to do with C++11? And Dukeling's answer also has *nothing* to do with C++11? `gets` is always wrong. This is because it just writes in memory until in happens to meet the newline character in standard input. You can't restrict it, so your app will always suffer from buffer overflow. Buffer overflow accounts for 40% of security bugs in the software. – milleniumbug Jul 06 '13 at 00:53
  • @mkag (continuation) `gets` was so bad it was *removed altogether* from C, where it originated from. There wasn't, there isn't and there won't be any reason to use `gets`, as at the same time as `gets` existed, `fgets` was the safe solution for reading single line to memory in C. And in C++ you can banish all these memory bugs out of existence *just by using the correct function and correct idioms*. – milleniumbug Jul 06 '13 at 00:53
  • @mkag (continuation) You fail at providing the correct, failsafe answer, therefore you fail, and therefore you got downvoted by me. What is more, it seems that you know enough on the subject that you recommend `gets_s`, saying that it is supported in Microsoft compiler and it has buffer length check, yet completely fail to account that the safe alternatives were (and they are) supported in every C (`fgets`) and C++ (`std::getline`) compiler. – milleniumbug Jul 06 '13 at 00:54