Thread: RE: [Dev-C++] Spaces in strings?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <pr...@le...> - 2003-04-01 05:56:10
|
Thanks Dany. But doesn't this limit my code in some manner? I've been
brought to believe that strings are better than chars since they
automatically adjust to the size of the input.
Regards,
Pranav Negandhi
This email is a natural product. The slight variations in spelling and
grammar enhance its individual character and beauty and are in no
way considered to be flaws or defects.
<snip>
The best thing is to use a char array. Try this one:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char cName[80];
cout << "Enter some words\n";
cin.getline(cName,80);
cout << "The word is " << cName << endl;
system("pause");
}
<snip>
|
|
From: <pr...@le...> - 2003-04-01 10:48:20
|
Thanks Richard.
But I don't understand how the input stream being used by getline() (i.e.
cin) reads in spaces and tabs. Any explanations on that?
<code snippet>
std::getline(std::cin, name);
</code snippet>
Regards,
Pranav Negandhi
This email is a natural product. The slight variations in spelling and
grammar enhance its individual character and beauty and are in no
way considered to be flaws or defects.
<snip>
yeah, you've gotten a few emails telling you to uses the getline function
rather tham cin.
basically cin will read the input string up to the first white space, this
is either a space(" ")or a tab(" ") or a enter( "/n" )
getline however will read everything including spaces and tabs up to the
first 'enter' or "endl"
<snip>
|
|
From: philip m. <mor...@ho...> - 2003-04-01 14:14:07
|
const int LEN = 80;
char str[LEN];
cout << "Enter string: " << flush;
cin.getline(str, LEN); // use default '\n' delimeter
cout << str << endl;
In the above example getline will just keep reading in characters
(whether they are tabs, spaces or anything else) until:
1) LEN - 1 characters have been read
2) it encounters the delimeter specified by the 3rd argument (which
defaults to the newline character)
3) it reaches the end of the file
whichever happens first - see
http://www.objsw.com/docs/iostream_19.html#SEC19
Philip Mortimer
----- Original Message -----
From: <pr...@le...>
To: <tel...@ya...>
Cc: <dev...@li...>
Sent: Tuesday, April 01, 2003 11:57 AM
Subject: Re: [Dev-C++] Spaces in strings?
>
> Thanks Richard.
> But I don't understand how the input stream being used by getline() (i.e.
> cin) reads in spaces and tabs. Any explanations on that?
>
> <code snippet>
> std::getline(std::cin, name);
> </code snippet>
>
>
> Regards,
> Pranav Negandhi
>
> This email is a natural product. The slight variations in spelling and
> grammar enhance its individual character and beauty and are in no
> way considered to be flaws or defects.
>
>
>
> <snip>
>
> yeah, you've gotten a few emails telling you to uses the getline function
> rather tham cin.
> basically cin will read the input string up to the first white space, this
> is either a space(" ")or a tab(" ") or a enter( "/n" )
> getline however will read everything including spaces and tabs up to the
> first 'enter' or "endl"
> <snip>
>
>
>
>
>
>
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: ValueWeb:
> Dedicated Hosting for just $79/mo with 500 GB of bandwidth!
> No other company gives more support or power for your dedicated server
> http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Ioannis V. <iv...@em...> - 2003-04-02 01:24:34
|
My way of reading a sequence with whitespaces without size limit until a
'\n' character is found:
#include <iostream>
#include <string>
int main()
{
using namespace std;
string s;
=20
while(cin)
{
char c;
cin.get(c);
=09
if(c=3D=3D'\n')
break;
s+=3Dc;
}
}
I know it is low level and I assume that there must be a better way =
(which I
do not know so far).
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|
|
From: philipmortimer <ph...@co...> - 2003-04-01 14:51:37
|
const int LEN = 80;
char str[LEN];
cout << "Enter string: " << flush;
cin.getline(str, LEN); // use default '\n' delimeter
cout << str << endl;
In the above example getline will just keep reading in characters
(whether they are tabs, spaces or anything else) until:
1) LEN - 1 characters have been read
2) it encounters the delimeter specified by the 3rd argument (which
defaults to the newline character)
3) it reaches the end of the file
whichever happens first (typically encountering a newline) - see
http://www.objsw.com/docs/iostream_19.html#SEC19
Philip Mortimer
----- Original Message -----
From: <pr...@le...>
To: <tel...@ya...>
Cc: <dev...@li...>
Sent: Tuesday, April 01, 2003 11:57 AM
Subject: Re: [Dev-C++] Spaces in strings?
>
> Thanks Richard.
> But I don't understand how the input stream being used by getline() (i.e.
> cin) reads in spaces and tabs. Any explanations on that?
>
> <code snippet>
> std::getline(std::cin, name);
> </code snippet>
>
>
> Regards,
> Pranav Negandhi
>
> This email is a natural product. The slight variations in spelling and
> grammar enhance its individual character and beauty and are in no
> way considered to be flaws or defects.
>
>
>
> <snip>
>
> yeah, you've gotten a few emails telling you to uses the getline function
> rather tham cin.
> basically cin will read the input string up to the first white space, this
> is either a space(" ")or a tab(" ") or a enter( "/n" )
> getline however will read everything including spaces and tabs up to the
> first 'enter' or "endl"
> <snip>
>
>
>
>
>
>
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: ValueWeb:
> Dedicated Hosting for just $79/mo with 500 GB of bandwidth!
> No other company gives more support or power for your dedicated server
> http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Dany P. W. <da...@qu...> - 2003-04-01 07:12:07
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of pr...@le... > Sent: Tuesday, 1 April 2003 6:06 p.m. > To: Dany P. Wu > Cc: dev...@li... > Subject: RE: [Dev-C++] Spaces in strings? > > > > Thanks Dany. But doesn't this limit my code in some manner? > I've been brought to believe that strings are better than > chars since they automatically adjust to the size of the input. Ok everyone, I think we both need help to answer this. I am also fairly new to C++ and that has always been the way I handle strings. So far I have never had the need for very long strings. What sort of length are we talking about here? Any other suggestions anyone? Cheers, Dany. -- This mail is a natural product. The slight variations in spelling and grammar enhance its individual character and beauty and in no way are to be considered flaws or defects. |