Jump to content
  • Advertisement
Sign in to follow this  
sas1992

Win32

This topic is 3509 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Does Standard C++ count?

// string_getline_sample.cpp
// compile with: /EHsc
// Illustrates how to use the getline function to read a
// line of text from the keyboard.
//
// Functions:
//
// getline Returns a string from the input stream.
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)
#include <string>
#include <iostream>

using namespace std ;

int main()
{
string s1;
cout << "Enter a sentence (use <space> as the delimiter): ";
getline(cin,s1, ' ');
cout << "You entered: " << s1 << endl;;
}

Share this post


Link to post
Share on other sites
Still not getting it.
Following error comes:-
Compiling...
DX1.cpp
C:\DX1\DX1.cpp(11) : error C2146: syntax error : missing ';' before identifier 'a'
C:\DX1\DX1.cpp(11) : error C2501: 'String' : missing storage-class or type specifiers
C:\DX1\DX1.cpp(11) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

DX1.exe - 3 error(s), 0 warning(s)
I included the following:-
#include <string.h>

string a="hello";

Share this post


Link to post
Share on other sites
Quote:
Original post by sas1992
I included the following:-
#include <string.h>

string a="hello";

Look closely at EasilyConfused example:

* It's <string>, not <string.h>
* It's std::string, not string

Both are because string is part of STL, the standard template library. STL headers (the files you #include) have no .h ending and the types in STL are declared in the std namespace.

Share this post


Link to post
Share on other sites
Quote:
Original post by sas1992
I included the following:-
#include <string.h>

string a="hello";


Look at the EasilyConfused post. You didnt specify the namespace (std) and you included string.h which is the header for C strings, you must include string which is the header for C++ std::strings. You must choose one of the following:

1.

#include <string>
using namespace std;

int main()
{
string a = "hello";
}



2.

#include <string>

int main()
{
std::string a = "hello";
}


Share this post


Link to post
Share on other sites
Quote:
Original post by sas1992
C:\DX1\DX1.cpp(11) : error C2501: 'String' : missing storage-class or type specifiers


If you directly copy and pasted the error, than from the looks of it you are using 'String' with a capital 'S' and not 'string' with a lower-case 's'. C++ is case-sensitive. And of course, like others said, make sure you are using namespace 'std' and make sure to include the header file like '#include "string"' (no '.h').

Cheers
-Scott

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!