Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. how to use std::getline(std::cin, s);
Forum Updated to NodeBB v4.3 + New Features

how to use std::getline(std::cin, s);

Scheduled Pinned Locked Moved Unsolved C++ Gurus
3 Posts 3 Posters 2.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on 19 Jul 2020, 22:40 last edited by Natural_Bugger
    #1

    I'm trying to build some console application using "example" code.

    i have been looking online and it looks correct.
    http://www.cplusplus.com/reference/string/string/getline/

    but it does not work or compile in Qt creator:

    QString s;
    std::getline(std::cin, s);
    

    error:

    main.cpp:34: error: no matching function for call to ‘getline(std::istream&, QString&)’
       34 |     std::getline(std::cin, s);
          |                             ^
    

    how to solve?

    regards.

    J 1 Reply Last reply 20 Jul 2020, 04:39
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 20 Jul 2020, 04:38 last edited by
      #2

      @Natural_Bugger said in how to use std::getline(std::cin, s);:

      how to solve?

      By reading the documentation of std::getline(): 'Defined in header <string>'

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • N Natural_Bugger
        19 Jul 2020, 22:40

        I'm trying to build some console application using "example" code.

        i have been looking online and it looks correct.
        http://www.cplusplus.com/reference/string/string/getline/

        but it does not work or compile in Qt creator:

        QString s;
        std::getline(std::cin, s);
        

        error:

        main.cpp:34: error: no matching function for call to ‘getline(std::istream&, QString&)’
           34 |     std::getline(std::cin, s);
              |                             ^
        

        how to solve?

        regards.

        J Offline
        J Offline
        JKSH
        Moderators
        wrote on 20 Jul 2020, 04:39 last edited by
        #3

        std::getline() knows about std::string but it doesn't know about QString. So, store your input in a std::string first and then call QString::fromStdString().

        See https://doc.qt.io/qt-5/qstring.html#fromStdString

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        4

        1/3

        19 Jul 2020, 22:40

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved