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.?

0

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

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 Yeah, I figured out that OP wanted to use cin.getline instead, which works with char*'s. Edited.
no I want the string in an array and when i am trying cin.getline() it says error: cin was not declared in scope..
@RahulBhojwani Yes, because you must use std::cin.getline(...). See this for a runnable example.
@Dukeling Thanks.. What is this std:: can you provide some link where I can get stuff about it??
|
-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;
}

6 Comments

Never use gets, use fgets or gets_s, and gets_s is standard C now (since C11)
-1: Sorry, but recommending gets is wrong in every circumstance.
as per the question, it seems Rahul is a school going and he hardly use new C++11 thats why gets recommended.
@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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.