Could use some help

This is a discussion on Could use some help within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm quite new to C/C++ programming, and I'm having some problems when trying to create variables of type string ...

  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
    Portugal
    Posts
    7,412
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    21,457
    also, replace iostream.h and stdlib.h for <iostream> and <stdlib>. These are the standard C++ headers.
    <stdlib> should be <cstdlib>
    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
    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,296
    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,475
    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
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

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