This code does not work in Bloodshed-Dev C++ 4.9.9.2. Why?

This is a discussion on This code does not work in Bloodshed-Dev C++ 4.9.9.2. Why? within the C++ Programming forums, part of the General Programming Boards category; could you please explain to me, why this code, does not work in Bloodshed-Dev C++ 4.9.9.2. The following errors occurred ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    30

    This code does not work in Bloodshed-Dev C++ 4.9.9.2. Why?

    could you please explain to me, why this code, does not work in Bloodshed-Dev C++ 4.9.9.2. The following errors occurred when i compile this code:

    first error: "myfile" undeclared (first use this function)
    second error: "getline" undeclared (first use this function)
    third error: "line" undeclared (first use this function)

    It seems that the compiler does not recognize the variable type string. Correct if i am wrong.

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    
    int main ()
    {
      string line;
      ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile, line);
          cout << line << endl;
        }
        myfile.close();
      }
    
      else cout << "Unable to open file"; 
    
      return 0;
    }
    Thanks...
    Last edited by neo_phyte; 09-27-2006 at 01:04 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
      string line;
      ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile, line);
          cout << line << endl;
        }
        myfile.close();
      }
    
      else cout << "Unable to open file"; 
    
      return 0;
    }
    ...or even better would be...
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main ()
    {
      std::string line;
      std::ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          std::getline (myfile, line);
          cout << line << endl;
        }
        myfile.close();
      }
    
      else cout << "Unable to open file"; 
    
      return 0;
    }
    Google namespaces.
    Sent from my iPadŽ

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    Use the correct standard headers.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main()
    {
        std::string line;
        std::ifstream myfile("example.txt");
        if (myfile.is_open())
        {
            while (! myfile.eof() )
            {
                std::getline(myfile, line);
                std::cout << line << std::endl;
            }
            myfile.close();
        }
    
        else std::cout << "Unable to open file"; 
    
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Ummm... well it appears you've edited your post to use the wrong headers as laserlight noted... if that's the code that you used, then your problem is that <string.h> and <string> are not the same library. <string.h> corresponds to <cstring> which is your strcpy, strcat, strlen functions and such.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    30
    okay.. thanks for a great help, i understand what SlyMaelstrom explaining that <string.h> corresponds to strcpy, strlen, strcmp, strcat, and other string functions. what does <string> corresponds with? hope you englighten me with this two terms... does also <iostream> and <iostream.h> are different libraries?

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Quote Originally Posted by neo_phyte
    okay.. thanks for a great help, i understand what SlyMaelstrom explaining that <string.h> corresponds to strcpy, strlen, strcmp, strcat, and other string functions. what does <string> corresponds with? hope you englighten me with this two terms... does also <iostream> and <iostream.h> are different libraries?
    <string> cooresponds to the string class that you're trying to use in your code. <iostream> and <iostream.h>, if you have the latter, are essentially the same thing. The only difference is <iostream> is under the std namespace and <iostream.h> isn't. I'm pretty sure <iostream.h> isn't actually standard, it was just created for some beta version of the C++ standard.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    30
    now i understand, till next problem...

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    There are bigger difference between <fstream.h> and <fstream> besides the namespace, so just changing the headers and adding the namespace doesn't always make the code work.

    >> it was just created for some beta version of the C++ standard.
    It was actually "standard" for many years before there was a standard, which is why it is still so prevalent today.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Quote Originally Posted by Daved
    It was actually "standard" for many years before there was a standard
    I can see why the first "standard" is in quotes... anyway, as far as the current standard is concerned, iostream.h is apparently only mentioned in one of the appendixes as being provided as a coutesy for all the existing code that includes it. It is, however, not standard.

    There actually are small differences between the two libraries that you may or may not encounter when using it. They are listed here.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    15
    I use Dev and it doesnt like using header files with '.h'. But is there any speed or memory differences between "using namespace std;" and writing it out the whole time.

  11. #11
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    It is fine with .h headers... it is not ok with you using old "standard" headers (such as iostream.h fstream.h).

    The point of namespaces is to keep things in one nice tidy area so that there are no naming conflicts (once you get into multiple libraries at the same time, you may find this out) therefore it is considered good by most people on this board to use std:: (namespace and scope resolver) rather than a using statement. If you want to know more on this, read up on namespaces. There is no speed differences other than in typing (That I know of) but in the long run most of the time spent programming is not the actual writing part, it is debuging and designing.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    > while (! myfile.eof() )
    See the FAQ on why using eof() to control a loop is bad.

    Back on topic.
    Small student exercises typically only use the std namespace, so "using namespace std;" is a quick way of saving some typing. It's about on a par with using global variables I suppose. You can just about get away with it in a single source file, but it won't take long before it becomes a real PITA.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    I prefer to do this:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    Rather than

    Code:
    using namespace std;
    It looks neater, and you delcare the namespaces you will use in the file, rather than declare the entire unit using the second option.

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    It doesn't look neater when you have
    Code:
    using std::cout;
    using std::cin;
    using std::endl;
    using std::setw;
    using std::string;
    using std::getline;
    using std::vector;
    using std::ofstream;
    using std::ifstream;
    /* ... and so on and so forth ... */
    I just use the std:: qualifier. Some people think it looks a mess, but really, those people just don't know how to format code properly.
    Sent from my iPadŽ

  15. #15
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Yes, I agree there, but you break it up by delcaring each namespace under the requaired header, like this for example:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    #include <string>
    
    using std::string;
    using std::getline;
    
    #include <iomanip>
    
    using std::setw;
    using std::setprecision;
    I know it does not look much of a difference, but I prefer it this way. It does also eliminate the need to type std:: before every cout statement or vector for example. I am not going to argue about this, as in broad terms, it really is up to the programmer how he/she presents their code and formating. But that is the style I choose to use, and I would not ever want to use namespace std unless it was really necassary

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does Dev C++ 4 even work in Vista?
    By asmileguo in forum C++ Programming
    Replies: 14
    Last Post: 02-07-2008, 09:14 AM
  2. Simple C++ code doesn't work
    By alex_dude_122 in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2006, 01:53 PM
  3. i cant get my code to work! (hawknl)
    By Guido in forum Networking/Device Communication
    Replies: 3
    Last Post: 05-23-2004, 02:20 AM
  4. Wait on Bloodshed Dev C++
    By BigSter in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2001, 03:46 AM
  5. Tutorial about Bloodshed Dev!
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2001, 07:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21