0

I don't want to declare character array. I want to declare string. And take input and show output of that string using scanf and printf. I have tried to do it this way that's shown below but it doesn't work properly. How to do it in proper way?

#include<bits/stdc++.h>
using namespace std;

int main()
{
    string str;

    scanf("%s", str.c_str());
    printf("%s", str.c_str());

    return 0;
}

When I take input using this code, output shows like this

Asif Mohammad
  • 49
  • 1
  • 5
  • 6
    C or C++? C does not have strings - only nul terminated char arrays. So you *must* use a character array. – Weather Vane Feb 12 '18 at 12:44
  • Yes, but _why_? – Vilx- Feb 12 '18 at 12:44
  • _it doesn't work properly_. --> Can you elaborate it a bit? – H.S. Feb 12 '18 at 12:47
  • 1
    `str.c_str()` is read-only. And not allocated/empty... UB. use `std::cin` instead. `std::string` supports it natively – Jean-François Fabre Feb 12 '18 at 12:47
  • 1
    You are not supposed to mix C and C++ like that. It's basically impossible to make the `scanf` line correct because it reads an arbitrary amount of data into a buffer, so you need an arbitrarily big buffer which means you must make the buffer bigger while reading. `scanf` doesn't do that. `std::cin` does so you can do `std::cin >> str;`. You should try to use the tools that do exactly what you want instead of hacking on tools that are made for a different purpose. – nwp Feb 12 '18 at 12:50
  • Didn't you get at least a warning for the `scanf`? – Jabberwocky Feb 12 '18 at 12:51
  • `str.c_str()` returns a `const char *` for a reason. – Jean-François Fabre Feb 12 '18 at 12:52
  • @MichaelWalz no i didnt get any warning – Asif Mohammad Feb 12 '18 at 12:53
  • @H.S. I have just added a photo of my output. – Asif Mohammad Feb 12 '18 at 12:54
  • @AsifMohammad if you use gcc you can compile with the `-Wall` flag. – Jabberwocky Feb 12 '18 at 13:08
  • @AsifMohammad: check [read-c-string-with-scanf](https://stackoverflow.com/questions/20165954/read-c-string-with-scanf) – H.S. Feb 12 '18 at 14:16
  • Scanf is inherently dangerous because it doesn't limit the size of string that it will fetch. You should at least specify a max width in your %s. When I really do need to go via scanf, or absolutely must interface with other c-style file handles, or system file handles, then I end up using a std::vector, (though you can reserve(0 and data() a string in c++17), but you still need to reserve() the max string size you can accept. – Gem Taylor Feb 12 '18 at 14:21

1 Answers1

1

You are trying to write in a zone which is read only e.g. str.c_str(). What you want to do is either :

 std::cin >> str;

or if for a specific reason you have to use scanf. Since C++17 you can do :

  str.data()

Which returns a modifiable memory zone.

Edit : as @nwp said, you have to call str.resize(size) to because the size of the string is 0 before that.

cydef
  • 417
  • 3
  • 10
  • 1
    Modifiable yes, but with size 0, so not very useful. You would need to call `str.resize(some_magic_number);` first. – nwp Feb 12 '18 at 13:16
  • @nwp True, I don't see the point of using scanf here so I omitted that detail. Thanks I'll edit to match your comment – cydef Feb 12 '18 at 13:21