Thread: Could use some help

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    Could use some help

    Hi,

    I'm quite new to C/C++ programming, and I'm having some problems when trying to create variables of type string.

    Here's a little example code:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    using namespace std;
    
    int main()
    {
          string myString = "This is my string."
          cout << myString
    
          system("PAUSE");
          return 0;
    }
    Now, previously I've been using Dev-C++ 5 beta as my IDE, but after numerous problems I decided to roll back to version 4, and all my previous problems have gone. I've also tried MS Visual C++ (Express), but it wasn't right for the relatively small progs I'm making at the moment.

    With version 4, the only problem I've encountered so far is that the compiler doesn't seem to want to accept "string" as a data type. Any ideas of what I can do or what the problem may be?


    I'm using Bloodshed Dev-C++ as my IDE, which comes with Mingw as the compiler.
    I'm running XP, and my compilation errors are:

    `string' undeclared (first use this function) (line 7)
    parse error before `=' (line 7)

    Any help would be greatly appreciated.

    Thanks for your time.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You need to #include <string>

    also, replace iostream.h and stdlib.h for <iostream> and <stdlib>. These are the standard C++ headers.

    EDIT: And you are missing a semi-colon after cout << myString and the string declaration.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    also, replace iostream.h and stdlib.h for <iostream> and <stdlib>. These are the standard C++ headers.
    <stdlib> should be <cstdlib>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    Oh yeah cheers for that.

    The compiler gave a new error when I removed the .h from stdlib. I can go into my include directory and rename it without the extension, but I've done that before with undesired results.

    I put the .h back in, added what you said, and it works. Thanks a lot.

    EDIT: Ah, thanks laserlight. Now when I remove the .h it works too.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by stinkipete
    The compiler gave a new error when I removed the .h from stdlib. I can go into my include directory and rename it without the extension, but I've done that before with undesired results.
    Never do that! You shouldn't ever touch those files. In fact it might be safer to make them all read-only just in case.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    Quote Originally Posted by iMalc
    Never do that! You shouldn't ever touch those files. In fact it might be safer to make them all read-only just in case.
    Thanks for the heads up. When I did that last time I backed up first. I'm not that stupid

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Strange that you had to revert back to version 4 of dev. I still have v5 and think its ok to use for small test programs. The only error that really bugs me is the bug with using the indirection operator when im using pointers. Apart from that its genrally ok. There have been many discussions on these boards about how bugged Dev can be, so Im not openning up a new debate about it now
    Double Helix STL

Popular pages Recent additions subscribe to a feed