Thread: program printing lines read from standard input not compile pls help weird error

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    2

    Question program printing lines read from standard input not compile pls help weird error


    Code:
    #include <string>
    #include <cstdio>
    #include <iostream>
    int main() {
        while(!feof(stdin)) {
            std::string asd;
            std::getline(std::cin, asd);
            constchar *s = asd.c_str();
            std::printf("%s", s);
        }
    }
    
    Code:
    D:\backup\C\Users\Computer\Desktop\c'>gcc -o thg thing.cppC:\Users\Computer\AppData\Local\Temp\cczP1Ilr.o:thing.cpp:(.text+0x10): undefine
    d reference to `__gxx_personality_sj0'
    C:\Users\Computer\AppData\Local\Temp\cczP1Ilr.o:thing.cpp:(.text+0x4f): undefine
    d reference to `std::basic_string<char, std::char_traits<char>, std::allocator<c
    har> >::basic_string()'
    C:\Users\Computer\AppData\Local\Temp\cczP1Ilr.o:thing.cpp:(.text+0x5e): undefine
    d reference to `std::cin'
    C:\Users\Computer\AppData\Local\Temp\cczP1Ilr.o:thing.cpp:(.text+0x6b): undefine
    d reference to `std::basic_istream<char, std::char_traits<char> >& std::getline<
    char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, st
    d::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::al
    locator<char> >&)'
    C:\Users\Computer\AppData\Local\Temp\cczP1Ilr.o:thing.cpp:(.text+0x76): undefine
    d reference to `std::string::c_str() const'
    C:\Users\Computer\AppData\Local\Temp\cczP1Ilr.o:thing.cpp:(.text+0xa1): undefine
    d reference to `std::basic_string<char, std::char_traits<char>, std::allocator<c
    har> >::~basic_string()'
    C:\Users\Computer\AppData\Local\Temp\cczP1Ilr.o:thing.cpp:(.text+0xdd): undefine
    d reference to `std::basic_string<char, std::char_traits<char>, std::allocator<c
    har> >::~basic_string()'
    C:\Users\Computer\AppData\Local\Temp\cczP1Ilr.o:thing.cpp:(.text+0x119): undefin
    ed reference to `std::ios_base::Init::~Init()'
    C:\Users\Computer\AppData\Local\Temp\cczP1Ilr.o:thing.cpp:(.text+0x13a): undefin
    ed reference to `std::ios_base::Init::Init()'
    D:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/4.9.2/../../../..
    /mingw32/bin/ld.exe: C:\Users\Computer\AppData\Local\Temp\cczP1Ilr.o: bad reloc
    address 0x0 in section `.ctors'
    D:/Program Files (x86)/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/4.9.2/../../../..
    /mingw32/bin/ld.exe: final link failed: Invalid operation
    collect2.exe: error: ld returned 1 exit status
    
    
    D:\backup\C\Users\Computer\Desktop\c'>
    Last edited by Bokor; 05-03-2018 at 04:03 PM. Reason: ??

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You need to use g++ to compile c++ programs.
    And you really shouldn't include iostream and cstdio at the same time.
    Just use iostream, so
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string line;
        while (getline(cin, line)) {
            cout << line << '\n';
        }
    }
    Also, note that you should test the input function itself to control the loop, not use eof().
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    May 2018
    Posts
    2
    thank you, it worked when I compiled it with g++, for the code and for the tips

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read multiple lines of input
    By veera in forum C Programming
    Replies: 4
    Last Post: 06-28-2015, 11:52 PM
  2. using scanf() to read a character from standard input
    By brassbin in forum C Programming
    Replies: 2
    Last Post: 11-01-2013, 04:12 PM
  3. Why isn't the first line at the standard input read?
    By BenBusby in forum C Programming
    Replies: 3
    Last Post: 03-21-2013, 04:20 PM
  4. Replies: 3
    Last Post: 04-27-2011, 09:46 PM
  5. read from standard input a list of filenames?
    By Roger in forum C Programming
    Replies: 1
    Last Post: 10-27-2009, 06:13 PM

Tags for this Thread