see here http://www.cprogramming.com/tutorial/lesson10.html
it stops reading after it reaches a space. Why is this and is there any easy way around it.
see here http://www.cprogramming.com/tutorial/lesson10.html
it stops reading after it reaches a space. Why is this and is there any easy way around it.
Input from istreams is delimited by a space, this is why you can say something like:
cin>> i >> j >> k;
And use input with spaces:
10 20 30
Of course this becomes a problem when you want to read a string of data that includes spaces. In this case you want to have input delimited by a newline character, which is what the getline method does:
cin.getline ( array, SIZE );
or with C++ strings:
getline ( cin, someString );
Note that wherever I used cin, you could replace it with a user defined istream:
ifstream ifs ( "someFile.txt" );
getline ( ifs, someString );
-Prelude
My best code is written with the delete key.
Use std::getline(cin, string).
Kuphryn
okay
If I include the line
Using std::getline(cin, string);
i get these error messages
error C2653: 'std' : is not a class or namespace name
error C2146: syntax error : missing ';' before identifier 'getline'
: error C2501: 'Using' : missing storage-class or type specifiers
: fatal error C1004: unexpected end of file found
if i don't i get this
error C2065: 'getline' : undeclared identifier
here is the line getting the error message:
getline ( Levels, String );
Put this:
using namespace std;
at the very top of your code, just below your includes, if it is not already there (which it ought to be anyway)
then, when you want to fill a string called String from a istream called Stream just use this:
getline(Stream, String);
If this doesn't work, post the smallest complete segment of code you can isolate that produces an error. Be sure to use code tags.
>Using std::getline(cin, string);
C++ is case sensitive, it should be
using std::getline;
Also note that getline is defined in <string>, not <iostream> and the previous statement would cause an error if you don't include string. The getline from iostream is bound to cin and is used differently, so the using statement would be
using std::cin;
And the call to getline would be
cin.getline ( cstring, size, delimiter );
>at the very top of your code, just below your includes, if it is not
>already there (which it ought to be anyway)
Actually, according to good style it shouldn't be there. using namespace std; opens the entire std namespace to the program, thus making everything in it global, which is a bad thing. The preferred method is to either use the std:: prefix for anything in std or open only the parts of std that you will be using in the function that you will use them. If something is to be used in every area of the program then you can make it open globally, such as cout.
#include <iostream>
using std::cout;
This is a valid use of the using statement in the global scope. Anything more should require a great deal of thought and understanding of the consequences.
-Prelude
My best code is written with the delete key.
okay now I'm getting these errors
(6) : error C2653: 'std' : is not a class or namespace name
(6) : error C2873: 'getline' : symbol cannot be used in a using-declaration
(73) : error C2065: 'getline' : undeclared identifier
here is the first six lines of my code perhaps allegro is interfering
[code]
#include <allegro.h>
#include <stdio.h>
#include <fstream.h>
#include <string.h>
using std::getline;
[code]
and here is the only real relevent part
ifstream Levels("Levels.txt");
getline ( Levels, String ); //line 73
>#include <string.h>
string.h doesn't contain the C++ string class, use:
#include <string>
using std::getline;
.
.
.
getline ( Levels, String );
-Prelude
My best code is written with the delete key.
okay thanks here the error message I'm getting now
: error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : expects 3 arguments - 2 provided
and
: error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'clas
Aren't are also suposed to input in a third argument which will stop getline
Show your code please, it's easier to debug with an error message AND code.
-Prelude
My best code is written with the delete key.
I didn't really change anything other then String.h to string and
Here's the corrected tutorial with char arrays holding the strings:
And here is the corrected tutorial with C++ strings:Code:#include <fstream> #include <iostream> /* Edits: The .h extension is not the current standard */ int main() { char astr[10]; /* Edits: Identifiers starting with str are reserved */ //Used later std::ofstream a_file("example.txt"); //Creates an instance of ofstream, and opens example.txt a_file<<"This text will now be inside of example.txt"; //Outputs to example.txt through a_file a_file.close(); //Closes up the file std::ifstream b_file("example.txt"); //Opens for reading the file b_file.getline ( astr, 10 ); //Reads one string from the file std::cout<<astr; //Should output 'this' b_file.close(); //Do not forget this! } /* Big Edits: Namespaces are used to conform with the current standard, no changes were made to handle errors, one should be wary of that when working with files */
I hope that clears everything up for you.Code:#include <fstream> #include <iostream> #include <string> /* Edits: The .h extension is not the current standard */ int main() { std::string astr; /* Edits: Identifiers starting with str are reserved */ //Used later std::ofstream a_file("example.txt"); //Creates an instance of ofstream, and opens example.txt a_file<<"This text will now be inside of example.txt"; //Outputs to example.txt through a_file a_file.close(); //Closes up the file std::ifstream b_file("example.txt"); //Opens for reading the file std::getline ( b_file, astr ); //Reads one string from the file std::cout<<astr; //Should output 'this' b_file.close(); //Do not forget this! } /* Big Edits: Namespaces are used to conform with the current standard, no changes were made to handle errors, one should be wary of that when working with files */
-Prelude
My best code is written with the delete key.