How do I store an input as 20 characters long, with spaces included.
For example when storing an address '31 High Street',
only stores the '31'??
Can someone please help me??
How do I store an input as 20 characters long, with spaces included.
For example when storing an address '31 High Street',
only stores the '31'??
Can someone please help me??
Cheers V
But could someone please explain what each part of the command does??
I understand that the '20' is the maxium number of characers.
But is the 'string' the variable type to be entered or is it the name of the variable.
And also I how I program some code that operates when the maximum charaters is more than 20, ie. using the '{' and '}'??
Thanks alot JamMan..
cin.getline's the function you're using
string's the name of the variable it's being read into.
-Govtcheez
[email protected]
Cheers Gotvcheese, thats what I thought it was. But does this only works on strings??
Can anyone help me with the following:
"And also I how I program some code that operates when the maximum charaters is more than 20, ie. using the '{' and '}'??"
Thanks once again JamMan..
there are several versions of getline(). The usual version has prototype
getline(char StringWhereInputWillBeStored, int NumberOfCharToBeStored, char terminatingChar);
The terminating character defaults to newline char so you frequently see only two parameters being written for.
If the input is longer than the max indicated char then the remaining char remain in the ifstream and can cause havoc if not appropriately dealt with. I am not sure what you mean regarding use of the curly braces.
By the curly bracket I mean:
Thanks alotCode:if //length of a string is equal to or more than 20, need to know the code to put here?? { //then do this }
Well you could do this:
char dummy[3000]; //use a big enough array to hold just about any input;
char input[21];//only really interested in first 20 char however, the last element is for the null char.
cin.getline(dummy, 2999, '\n');
if(strlen(dummy) < 21)
strcpy(input, dummy);
else
{
for(int i = 0; i < 20; i++)
{
input[i] = dummy[i];
}
input[20] = '\0';
}
//now do whatever you want with dummy as the first 20 char of dummy have been copied into input.
Cheers whoever you are..
You know there are some very good programers, that never register and stay as 'unregistered', why dont they register??
Thanks anyway..
JamMan..
they can't be bothered!
programmers are lazy thats why we reuse code and why OOP was invented.
True..
.. and a great thanks to everyone who helped me with this problem.