Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie question on strings

Reply
Thread Tools

Newbie question on strings

 
 
Deniz Dogan
Guest
Posts: n/a
 
      05-25-2007
Hello.

I am rather new to C/C++ and the only programming/scripting languages
that I know (well) is Java, Ruby, Python and similar high-level
languages. Just so you know where I'm coming from.


I have a really simple problem. I have this piece of code:

// Array of "numerical" characters.
char numbers [] = {'0','1','2','3','4','5','6','7','8','9'};

// Checks whether a char is a numerical character (0-9).
bool isnumber(char x) {
for (int i = 0; i < sizeof(numbers); ++i) {
if (x == numbers[i]) {
return true;
}
}
return false;
}

int main() {
string mystring;
getline(cin, mystring);

string x = "", y = "";
for (int i = 0; i < sizeof(mystring); ++i) {
if (isnumber(mystring[i]))
x << mystring[i];
else
y << mystring[i]
}
return 0;
}

As you can see, I'd like to have one string x containing all of the
numerical characters from the input, and all the non-numerical ones in a
string y.

The compilation error that I get is the following:

addressbook.cpp: In function `int main()':
addressbook.cpp:40: error: no match for 'operator<<' in 'x <<
(&mystr)->std::basic_string<_CharT, _Traits, _Alloc>:perator[] [with
_CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>](((unsigned int)i))'
addressbook.cpp:47: error: no match for 'operator<<' in 'y <<
(&mystr)->std::basic_string<_CharT, _Traits, _Alloc>:perator[] [with
_CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>](((unsigned int)i))'


My guess is that this means that I am disallowed to use the << operator,
for some reason.

Any help is highly appreciated and I apologize for not being able to
solve this problem myself, as it probably is a very simple error.

-- Deniz Dogan
 
Reply With Quote
 
 
 
 
Deniz Dogan
Guest
Posts: n/a
 
      05-25-2007
Deniz Dogan wrote:
> Hello.
>
> I am rather new to C/C++ and the only programming/scripting languages
> that I know (well) is Java, Ruby, Python and similar high-level
> languages. Just so you know where I'm coming from.
>
>
> I have a really simple problem. I have this piece of code:
>
> // Array of "numerical" characters.
> char numbers [] = {'0','1','2','3','4','5','6','7','8','9'};
>
> // Checks whether a char is a numerical character (0-9).
> bool isnumber(char x) {
> for (int i = 0; i < sizeof(numbers); ++i) {
> if (x == numbers[i]) {
> return true;
> }
> }
> return false;
> }
>
> int main() {
> string mystring;
> getline(cin, mystring);
>
> string x = "", y = "";
> for (int i = 0; i < sizeof(mystring); ++i) {
> if (isnumber(mystring[i]))
> x << mystring[i];
> else
> y << mystring[i]
> }
> return 0;
> }
>
> As you can see, I'd like to have one string x containing all of the
> numerical characters from the input, and all the non-numerical ones in a
> string y.
>
> The compilation error that I get is the following:
>
> addressbook.cpp: In function `int main()':
> addressbook.cpp:40: error: no match for 'operator<<' in 'x <<
> (&mystr)->std::basic_string<_CharT, _Traits, _Alloc>:perator[] [with
> _CharT = char, _Traits = std::char_traits<char>, _Alloc =
> std::allocator<char>](((unsigned int)i))'
> addressbook.cpp:47: error: no match for 'operator<<' in 'y <<
> (&mystr)->std::basic_string<_CharT, _Traits, _Alloc>:perator[] [with
> _CharT = char, _Traits = std::char_traits<char>, _Alloc =
> std::allocator<char>](((unsigned int)i))'
>
>
> My guess is that this means that I am disallowed to use the << operator,
> for some reason.
>
> Any help is highly appreciated and I apologize for not being able to
> solve this problem myself, as it probably is a very simple error.
>
> -- Deniz Dogan


I just realized that sizeof might not be the thing I'm looking for here.
Not sure though.
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      05-25-2007
On 25 Maj, 14:42, Deniz Dogan <(E-Mail Removed)> wrote:
> Hello.
>
> I am rather new to C/C++ and the only programming/scripting languages
> that I know (well) is Java, Ruby, Python and similar high-level
> languages. Just so you know where I'm coming from.
>
> I have a really simple problem. I have this piece of code:
>
> // Array of "numerical" characters.
> char numbers [] = {'0','1','2','3','4','5','6','7','8','9'};
>
> // Checks whether a char is a numerical character (0-9).
> bool isnumber(char x) {
> for (int i = 0; i < sizeof(numbers); ++i) {
> if (x == numbers[i]) {
> return true;
> }
> }
> return false;
> }


You should use the isdigit() function found in <ctype> instead.


> int main() {
> string mystring;
> getline(cin, mystring);
>
> string x = "", y = "";


You don't have to initialize them just 'string x, y;' will do.

> for (int i = 0; i < sizeof(mystring); ++i) {
> if (isnumber(mystring[i]))
> x << mystring[i];


To add a character to a string use the += operator.

> else
> y << mystring[i]
> }
> return 0;
>
> }


--
Erik Wikström

 
Reply With Quote
 
Deniz Dogan
Guest
Posts: n/a
 
      05-25-2007
Erik Wikström wrote:
> On 25 Maj, 14:42, Deniz Dogan <(E-Mail Removed)> wrote:
>> Hello.
>>
>> I am rather new to C/C++ and the only programming/scripting languages
>> that I know (well) is Java, Ruby, Python and similar high-level
>> languages. Just so you know where I'm coming from.
>>
>> I have a really simple problem. I have this piece of code:
>>
>> // Array of "numerical" characters.
>> char numbers [] = {'0','1','2','3','4','5','6','7','8','9'};
>>
>> // Checks whether a char is a numerical character (0-9).
>> bool isnumber(char x) {
>> for (int i = 0; i < sizeof(numbers); ++i) {
>> if (x == numbers[i]) {
>> return true;
>> }
>> }
>> return false;
>> }

>
> You should use the isdigit() function found in <ctype> instead.
>
>
>> int main() {
>> string mystring;
>> getline(cin, mystring);
>>
>> string x = "", y = "";

>
> You don't have to initialize them just 'string x, y;' will do.
>
>> for (int i = 0; i < sizeof(mystring); ++i) {
>> if (isnumber(mystring[i]))
>> x << mystring[i];

>
> To add a character to a string use the += operator.
>
>> else
>> y << mystring[i]
>> }
>> return 0;
>>
>> }

>
> --
> Erik Wikström
>


Thanks a lot, Erik!

--
Deniz Dogan
 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      05-25-2007
On 25 May, 13:44, Deniz Dogan <(E-Mail Removed)> wrote:
> Deniz Dogan wrote:
> > Hello.

>
> > I am rather new to C/C++ and the only programming/scripting languages
> > that I know (well) is Java, Ruby, Python and similar high-level
> > languages. Just so you know where I'm coming from.

>
> > I have a really simple problem. I have this piece of code:

>
> > // Array of "numerical" characters.
> > char numbers [] = {'0','1','2','3','4','5','6','7','8','9'};

>
> > // Checks whether a char is a numerical character (0-9).
> > bool isnumber(char x) {
> > for (int i = 0; i < sizeof(numbers); ++i) {
> > if (x == numbers[i]) {
> > return true;
> > }
> > }
> > return false;
> > }

>
> > int main() {
> > string mystring;
> > getline(cin, mystring);

>
> > string x = "", y = "";
> > for (int i = 0; i < sizeof(mystring); ++i) {
> > if (isnumber(mystring[i]))
> > x << mystring[i];
> > else
> > y << mystring[i]
> > }
> > return 0;
> > }

>
> > As you can see, I'd like to have one string x containing all of the
> > numerical characters from the input, and all the non-numerical ones in a
> > string y.

>
> > The compilation error that I get is the following:

>
> > addressbook.cpp: In function `int main()':
> > addressbook.cpp:40: error: no match for 'operator<<' in 'x <<
> > (&mystr)->std::basic_string<_CharT, _Traits, _Alloc>:perator[] [with
> > _CharT = char, _Traits = std::char_traits<char>, _Alloc =
> > std::allocator<char>](((unsigned int)i))'
> > addressbook.cpp:47: error: no match for 'operator<<' in 'y <<
> > (&mystr)->std::basic_string<_CharT, _Traits, _Alloc>:perator[] [with
> > _CharT = char, _Traits = std::char_traits<char>, _Alloc =
> > std::allocator<char>](((unsigned int)i))'

>
> > My guess is that this means that I am disallowed to use the << operator,
> > for some reason.


Correct. << in this context is the stream insertion operator. a string
is not a stream so attempting to use the stream insertion operator
makes no sense. Use += to append a char to a string.

>
> > Any help is highly appreciated and I apologize for not being able to
> > solve this problem myself, as it probably is a very simple error.

>
> > -- Deniz Dogan

>
> I just realized that sizeof might not be the thing I'm looking for here.
> Not sure though.


Also correct. sizeof evaluates the size of the object. The size of a
string object is not the same thing as the length of the data the
object is managing. Fortunately, the string class provides you with a
member function, length(), which returns what you need.

Gavin Deane

 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      05-25-2007
On Fri, 25 May 2007 14:42:44 +0200, Deniz Dogan wrote:

> Hello.
>
> I am rather new to C/C++ and the only programming/scripting languages
> that I know (well) is Java, Ruby, Python and similar high-level
> languages. Just so you know where I'm coming from.
>
>
> I have a really simple problem. I have this piece of code:
>
> // Array of "numerical" characters.
> char numbers [] = {'0','1','2','3','4','5','6','7','8','9'};
>
> // Checks whether a char is a numerical character (0-9). bool
> isnumber(char x) {
> for (int i = 0; i < sizeof(numbers); ++i) {
> if (x == numbers[i]) {
> return true;
> }
> }
> return false;
> }
>
> int main() {
> string mystring;
> getline(cin, mystring);
>
> string x = "", y = "";
> for (int i = 0; i < sizeof(mystring); ++i) {
> if (isnumber(mystring[i]))
> x << mystring[i];
> else
> y << mystring[i]
> }
> return 0;
> }
>
> As you can see, I'd like to have one string x containing all of the
> numerical characters from the input, and all the non-numerical ones in a
> string y.


You're probably looking for std::stringstream. Try this:

#include <string>
#include <iostream>
#include <sstream>

using namespace std;

int main()
{
string mystring;
getline(cin, mystring);

stringstream x, y; // does have operator <<

for (string::size_type i = 0; i < mystring.size(); ++i) {
if (isdigit(mystring[i])) // `std::isdigit' does what your `isnumber' does
x << mystring[i];
else
y << mystring[i];
}

cout << "x = " << x.str() << endl; // stringstream::str() returns the actual string
cout << "y = " << y.str() << endl;

return 0;
}

--
Lionel B
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      05-25-2007
Deniz Dogan <(E-Mail Removed)> wrote:

> // Array of "numerical" characters.
> char numbers [] = {'0','1','2','3','4','5','6','7','8','9'};
>
> // Checks whether a char is a numerical character (0-9).
> bool isnumber(char x) {
> for (int i = 0; i < sizeof(numbers); ++i) {
> if (x == numbers[i]) {
> return true;
> }
> }
> return false;
> }
>


remove all of the above. use the line below instead:

#include <cctype>

> int main() {
> string mystring;
> getline(cin, mystring);
>
> string x = "", y = "";
> for (int i = 0; i < sizeof(mystring); ++i) {


for ( int i = 0; i < mystring.size(); ++i ) {

> if (isnumber(mystring[i]))

if ( isdigit( mystring[i] ) )

> x << mystring[i];

x += mystring[i];

> else
> y << mystring[i]

y += mystring[i];
> }
> return 0;
> }

 
Reply With Quote
 
oleg.belozor@gmail.com
Guest
Posts: n/a
 
      05-25-2007
I know, in this place there are many real gurus with huge experience
programming using C++ and I'm interesting, how they can implement this
functionality. My version:

#include <iostream>
#include <conio.h>
#include <sstream>

using namespace std;

int main()
{
char c;
stringstream x, y;
do
{
c = (char)getchar();
isdigit(c) ? x << c : y << c;
}
while(c != '\n');

cout <<"x = " << x.str() << endl;
cout << "y = " << y.str() << endl;

getchar();

return 0;
}

With impatience wait for yours solutions.

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      05-25-2007
On 25 May, 16:36, (E-Mail Removed) wrote:
> I know, in this place there are many real gurus with huge experience
> programming using C++ and I'm interesting, how they can implement this
> functionality. My version:
>
> #include <iostream>
> #include <conio.h>
> #include <sstream>
>
> using namespace std;
>
> int main()
> {
> char c;
> stringstream x, y;
> do
> {
> c = (char)getchar();
> isdigit(c) ? x << c : y << c;
> }
> while(c != '\n');
>
> cout <<"x = " << x.str() << endl;
> cout << "y = " << y.str() << endl;
>
> getchar();
>
> return 0;
>
> }
>
> With impatience wait for yours solutions.


The main problem with your code as a C++ solution is that conio.h and
getchar are not part of C++. To read and process an entire line, the
getline function, as used by the OP is perfectly adequate. One other
minor point, you only use your string streams for output so x and y
can be ostringstream rather than stringstream.

#include <iostream>
#include <sstream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
string line;
ostringstream x, y;
getline(cin, line);
for (string::size_type i = 0; i < line.length(); ++i)
{
char c = line[i];
isdigit(c) ? x << c : y << c;
}
cout <<"x = " << x.str() << endl;
cout << "y = " << y.str() << endl;
return 0;
}

Gavin Deane

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      05-25-2007
Gavin Deane wrote:
> On 25 May, 16:36, (E-Mail Removed) wrote:
>> I know, in this place there are many real gurus with huge experience
>> programming using C++ and I'm interesting, how they can implement this
>> functionality. My version:
>>
>> #include <iostream>
>> #include <conio.h>
>> #include <sstream>
>>
>> using namespace std;
>>
>> int main()
>> {
>> char c;
>> stringstream x, y;
>> do
>> {
>> c = (char)getchar();
>> isdigit(c) ? x << c : y << c;
>> }
>> while(c != '\n');
>>
>> cout <<"x = " << x.str() << endl;
>> cout << "y = " << y.str() << endl;
>>
>> getchar();
>>
>> return 0;
>>
>> }
>>
>> With impatience wait for yours solutions.

>
> The main problem with your code as a C++ solution is that conio.h and
> getchar are not part of C++.


Actually, getchar() *is* part of C++ (the stdio library is incorporated
by reference), but he doesn't include either <stdio.h> or <cstdio>.

> To read and process an entire line, the
> getline function, as used by the OP is perfectly adequate. One other
> minor point, you only use your string streams for output so x and y
> can be ostringstream rather than stringstream.
>
> #include <iostream>
> #include <sstream>
> #include <string>
> #include <cctype>
>
> using namespace std;
>
> int main()
> {
> string line;
> ostringstream x, y;
> getline(cin, line);
> for (string::size_type i = 0; i < line.length(); ++i)
> {
> char c = line[i];
> isdigit(c) ? x << c : y << c;
> }
> cout <<"x = " << x.str() << endl;
> cout << "y = " << y.str() << endl;
> return 0;
> }
>
> Gavin Deane
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments