Sign in to follow this  
sas1992

Win32

Recommended Posts

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this