Thread: Please help me with input errors.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    12

    Unhappy Please help me with input errors.

    Hey guys,

    I'm new in here and from Austria.

    Tomorrow I've to present a programmed game and the input of a textfile doesn't work.

    I get the errors "ios" has not been declared and
    "in" was not declared in this scope.

    The error has to be in the line where the fstream commands are.

    Code:
    int table_x=20, table_y=20;
        char cstring[256];
        /*highscore____________________________open highscore.dat_______________________________*/
        std::fstream datfile;
        datfile.open("highscore.dat", ios::in);
        /*highscore____________________________highscores_______________________________*/
        while(mouse_x<=70||mouse_x>=255||mouse_y<=340||mouse_y>=410||!mouse_b&1)
        {
            draw_sprite(screen, menu_background, background_x, background_y);
            draw_sprite(screen, button_quit, button_x, button_quit_y);
            /*highscore________________________highscores___________________________________*/
            while (!datfile.eof())
            {
                datfile.getline(cstring, sizeof(cstring));
    
                textprintf_ex(screen,font,table_x,table_y,makecol(255,255,255), makecol(0, 0, 0),"%s", cstring);
                table_y=table_y+10;
            }
    Thank you so much.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Like your use of std::fstream, you probably need to prefix your use of ios::in with std:: as well.

    Also, this:
    Code:
    ...
    while (!datfile.eof())
    {
        datfile.getline(cstring, sizeof(cstring));
        ...
    ...is wrong (see here). The example in the link is C, not C++ but the lesson being imparted there is still applicable. You need to test the return result of the read operation and not test EOF when controlling your loops. This is true in almost all cases, rarely do people ever use an EOF test correctly in their code when loops are involved. Try something like this instead:
    Code:
    ...
    while (datfile.getline(cstring, sizeof(cstring))
    {
        ...
    Better yet you should ditch the C-style character arrays and use a std::string container instead.
    Last edited by hk_mp5kpdw; 06-13-2012 at 07:43 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    Hmm, dunno really what you mean but if I use
    Code:
    std::ios::in
    I get loads of errors.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What errors? Looking the use of fstream's open member function it looks like the parameter should actually be more along the lines of std::ios_base::in. Post your errors.

    [edit]What compiler/IDE are you using?[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    I'm using Codeblocks with Allegro.

    When I'm writing std::ios::in or std::ios_base::in I get ........loads of errors xD

    Code:
    obj\Debug\main.o||In function `ZN11stlpmtx_std16_Filebuf_Tmp_BufIcED1Ev':|
    ::basic_ios()]+0xd)||undefined reference to `_imp___ZN11stlpmtx_std8ios_baseC2Ev'|
    ::~basic_ios()]+0x16)||undefined reference to `_imp___ZN11stlpmtx_std8ios_baseD2Ev'|
    ::basic_filebuf()]+0x5a)||undefined reference to `_imp___ZN11stlpmtx_std13_Filebuf_baseC1Ev'|
    obj\Debug\main.o||In function `Z4menuv':|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|66|undefined reference to `_imp___ZN11stlpmtx_std6localeC1Ev'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|71|undefined reference to `_imp___ZN11stlpmtx_std6localeD1Ev'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|74|undefined reference to `_imp___ZN11stlpmtx_std6localeD1Ev'|
    obj\Debug\main.o||In function `ZN11stlpmtx_std16_Filebuf_Tmp_BufIcED1Ev':|
    )]+0x5e)||undefined reference to `_imp___ZN11stlpmtx_std6localeC1Ev'|
    obj\Debug\main.o||In function `WinMain':|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|41|undefined reference to `_imp___ZN11stlpmtx_std6localeD1Ev'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|41|undefined reference to `_imp___ZN11stlpmtx_std6localeD1Ev'|
    obj\Debug\main.o||In function `Z4menuv':|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|62|undefined reference to `_imp___ZN11stlpmtx_std6localeD1Ev'|
    obj\Debug\main.o||In function `ZN11stlpmtx_std16_Filebuf_Tmp_BufIcED1Ev':|
     int)]+0x1e)||undefined reference to `_imp___ZN11stlpmtx_std13_Filebuf_base7_M_openEPKci'|
    ::basic_streambuf()]+0x55)||undefined reference to `_imp___ZN11stlpmtx_std6localeC1Ev'|
    ::~basic_streambuf()]+0x19)||undefined reference to `_imp___ZN11stlpmtx_std6localeD1Ev'|
    obj\Debug\main.o||In function `WinMain':|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|41|undefined reference to `_imp___ZN11stlpmtx_std13_Filebuf_base8_M_closeEv'|
    obj\Debug\main.o||In function `ZN11stlpmtx_std16_Filebuf_Tmp_BufIcED1Ev':|
    )]+0x4d)||undefined reference to `_imp___ZN11stlpmtx_std8ios_base5imbueERKNS_6localeE'|
    obj\Debug\main.o||In function `WinMain':|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|41|undefined reference to `_imp___ZN11stlpmtx_std6localeD1Ev'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|41|undefined reference to `_imp___ZN11stlpmtx_std5ctypeIcE2idE'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|41|undefined reference to `_imp___ZNK11stlpmtx_std6locale12_M_get_facetERKNS0_2idE'|
    obj\Debug\main.o||In function `Z4menuv':|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|61|undefined reference to `_imp___ZN11stlpmtx_std8numpunctIcE2idE'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|62|undefined reference to `_imp___ZNK11stlpmtx_std6locale12_M_get_facetERKNS0_2idE'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|69|undefined reference to `_imp___ZN11stlpmtx_std8ios_base5imbueERKNS_6localeE'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|74|undefined reference to `_imp___ZN11stlpmtx_std6localeaSERKS0_'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|78|undefined reference to `_imp___ZN11stlpmtx_std6localeD1Ev'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|79|undefined reference to `_imp___ZN11stlpmtx_std6localeD1Ev'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|83|undefined reference to `_imp___ZN11stlpmtx_std6localeD1Ev'|
    obj\Debug\main.o||In function `ZN11stlpmtx_std16_Filebuf_Tmp_BufIcED1Ev':|
    C:\Program Files (x86)\CodeBlocks\MinGW\include\stl\_fstream.h:(.text$_ZN11stlpmtx_std8ios_base23_M_check_exception_maskEv[stlpmtx_std::ios_base::_M_check_exception_mask()]+0x1d)||undefined reference to `_imp___ZN11stlpmtx_std8ios_base16_M_throw_failureEv'|
     unsigned int)]+0x2a)||undefined reference to `_imp___ZN11stlpmtx_std12__node_alloc13_M_deallocateEPvj'|
     int)]+0x1e)||undefined reference to `_imp___ZN11stlpmtx_std13_Filebuf_base8_M_writeEPci'|
    ::_M_exit_input_mode()]+0x34)||undefined reference to `_imp___ZN11stlpmtx_std13_Filebuf_base8_M_unmapEPvx'|
    )]+0x66)||undefined reference to `_imp___ZN11stlpmtx_std6localeC1ERKS0_'|
    )]+0x7d)||undefined reference to `_imp___ZN11stlpmtx_std6localeaSERKS0_'|
    obj\Debug\main.o||In function `WinMain':|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|41|undefined reference to `_imp___ZN11stlpmtx_std6localeD1Ev'|
    obj\Debug\main.o||In function `ZN11stlpmtx_std16_Filebuf_Tmp_BufIcED1Ev':|
    )]+0x7)||undefined reference to `_imp___ZN11stlpmtx_std7codecvtIcciE2idE'|
    )]+0x16)||undefined reference to `_imp___ZNK11stlpmtx_std6locale12_M_get_facetERKNS0_2idE'|
    )]+0x7)||undefined reference to `_imp___ZN11stlpmtx_std7codecvtIcciE2idE'|
    )]+0x16)||undefined reference to `_imp___ZNK11stlpmtx_std6locale12_M_use_facetERKNS0_2idE'|
    )]+0x2a)||undefined reference to `_imp___ZN11stlpmtx_std12__node_alloc11_M_allocateERj'|
    ::_M_throw_length_error() const]+0xe)||undefined reference to `_imp___ZN11stlpmtx_std24__stl_throw_length_errorEPKc'|
    ::~basic_ios()]+0x16)||undefined reference to `_imp___ZN11stlpmtx_std8ios_baseD2Ev'|
    ::~basic_ios()]+0x16)||undefined reference to `_imp___ZN11stlpmtx_std8ios_baseD2Ev'|
    ::~basic_streambuf()]+0x19)||undefined reference to `_imp___ZN11stlpmtx_std6localeD1Ev'|
    ::~basic_streambuf()]+0x19)||undefined reference to `_imp___ZN11stlpmtx_std6localeD1Ev'|
    C:\Program Files (x86)\CodeBlocks\MinGW\include\stl\_fstream.h:(.text$_ZN11stlpmtx_std13_Filebuf_base11__page_sizeEv[stlpmtx_std::_Filebuf_base::__page_size()]+0x4)||undefined reference to `_imp___ZN11stlpmtx_std13_Filebuf_base12_M_page_sizeE'|
    obj\Debug\main.o||In function `Z4menuv':|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|66|undefined reference to `_imp___ZN11stlpmtx_std13_Filebuf_base7_M_seekExi'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|76|undefined reference to `_imp___ZN11stlpmtx_std13_Filebuf_base7_M_seekExi'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|81|undefined reference to `_imp___ZN11stlpmtx_std13_Filebuf_base7_M_seekExi'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|85|undefined reference to `_imp___ZN11stlpmtx_std13_Filebuf_base7_M_seekExi'|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|109|undefined reference to `_imp___ZN11stlpmtx_std13_Filebuf_base7_M_seekExi'|
    obj\Debug\main.o:C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|115|more undefined references to `_imp___ZN11stlpmtx_std13_Filebuf_base7_M_seekExi' follow|
    obj\Debug\main.o||In function `WinMain':|
    C:\Users\Andreas\Documents\Schule\AINP\2011_12\Projekt C++\C++\snake\snake\main.cpp|41|undefined reference to `_imp___ZN11stlpmtx_std13_Filebuf_base12_M_file_sizeEv'|
    ||More errors follow but not being shown.|
    ||Edit the max errors limit in compiler options...|
    ||=== Build finished: 50 errors, 0 warnings ===|

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, none of those are compilation errors at least. "Undefined reference" errors are linker errors and they are showing up now because in fixing the previous error you mentioned with your original post, the compiler was able to complete its job successfully and the build of your executable finally had a chance to proceed to the next step... linking. These errors are usually because you have not added some library file in your project's linker settings or you have additional source files that have not been compiled and/or linked in with the rest of your project's code. Can you show your compiler/linker settings being used with this project?

    What type of project are you making? It does not look like a basic console program since I see a reference to a WinMain function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    I'm making an Allegro based game, the code I posted was only from a function.
    The other parts are running well.

    I've set the liballeg.a, libaldat.a, liballd.a, liballd_s.a, liballeg_s.a, liballp.a and liballp_s.a in the compiller settings.
    I remember I had set some other settings a year ago before I had to re-set-up my windows...
    And I'm using the minGW

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    did you invoke the compiler via the gcc command or with g++? if you used gcc, try g++ instead.

    show us your compiler command.

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    I took the GNU gcc Compiler

    [edit] in the Compiler settings
    Last edited by Andy Eder; 06-13-2012 at 09:19 AM.

  10. #10
    Registered User
    Join Date
    Jun 2012
    Posts
    12
    Does now one know a solution?

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    C++ code needs to use g++, not gcc as mentioned by Elkvis. As for other Allegro issues I cannot comment as I've never used it. Not sure what specific libraries it might require you to link in with the rest of the project... other than the ones you seem to be using already.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-24-2009, 01:28 AM
  2. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  3. Help loading a vector with input from input stream
    By Bnchs in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2006, 03:53 PM
  4. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 08:43 PM
  5. Handling input errors, general question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-08-2001, 06:21 PM