Results 1 to 6 of 6

Thread: What's wrong with this code??

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Unhappy What's wrong with this code??

    I've been struggling to figure out what's wrong with this. Can somebody help? Using VC++ I have this code :
    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <string.h>
    
    string sMyStr;
    
    int main()
    {
    	sMyStr = "Hello";
    	return 0;
    }
    I get these errors :

    C:\Program Files\Microsoft Visual Studio\MyProjects\jwDOS3\jwDOS3.cpp(5) : error C2146: syntax error : missing ';' before identifier 'sMyStr'

    C:\Program Files\Microsoft Visual Studio\MyProjects\jwDOS3\jwDOS3.cpp(5) : error C2501: 'string' : missing storage-class or type specifiers

    C:\Program Files\Microsoft Visual Studio\MyProjects\jwDOS3\jwDOS3.cpp(5) : fatal error C1004: unexpected end of file found
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  2. #2
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Try this:

    Code:
    #include <stdio.h>  //Why do you need this for what you are doing?
    #include <iostream.h>
    #include <string>
    
    std::string sMyStr;
    
    int main()
    {
    	sMyStr = "Hello";
    	return 0;
    }
    Alcohol & calculus don't mix.
    Never drink & derive.

  3. #3
    Megatron
    Guest
    You could use char*, which is a string.
    Code:
    #include <iostream.h>
    
    char* sMyStr;
    
    int main()
    {
    	
    	sMyStr = "Hello";
    	return 0;
    }

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    It's usually better to use the standard headers. If you use <iostream.h> then a lot of the stuff that's in the standard namespace usually is accessible without taking any notice of namespaces, like cout for instance. I'm not sure whether that's because the M$ header puts you in the standard namespace (instead of the global namespace that is) or because it moves some things from the standard namespace into the global namespace, but... well anyway it makes things unclear.

    So, use the standard headers and explicitly resolve the scope:

    Code:
    #include <cstdio>  //I don't know why you need this either
    #include <iostream>
    #include <string>
    
    std::string sMyStr;
    
    int main()
    {
    	sMyStr = "Hello";
    	return 0;
    }
    or, alternatively, use the standard namespace instead of the global namespace, so that the scope becomes implicit:
    Code:
    #include <cstdio>  //I don't know why you need this either
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string sMyStr;
    
    int main()
    {
    	sMyStr = "Hello";
    	return 0;
    }
    namespaces can make things confusing, but they're pretty fly when you need them. They save all kinds of hassle in big projects and reusable code.
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Why would they make their own header if the standard one is basically the same thing? Like you said, it would only serve to confuse people.
    Alcohol & calculus don't mix.
    Never drink & derive.

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Yes, it would be unlike Microsoft to make something non-standard wouldn't it?
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width