Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "ispunct()" not working on std::string

Reply
Thread Tools

"ispunct()" not working on std::string

 
 
arnuld
Guest
Posts: n/a
 
      07-18-2007
/* C++ Primer 4/e
* section 3.2 - String Standard Library

* exercise 3.10
* STATEMENT
* write a programme to strip the punctation from the string. */

#include <iostream>
#include <string>

int main()
{
std::cout << "Enter a string: ";
std::string a_word, stripped_word;
std::cin >> a_word;

for(std::string::size_type ix = 0; ix != a_word.size(); ++ix)
{
if(ispunct(a_word[ix]) == 0)
stripped_word[ix] = a_word[ix];
}

std::cout << stripped_word << std::endl;

return 0;
}

--
-- http://arnuld.blogspot.com

 
Reply With Quote
 
 
 
 
arnuld
Guest
Posts: n/a
 
      07-18-2007
On Wed, 18 Jul 2007 14:07:59 +0500, arnuld wrote:

> /* C++ Primer 4/e
> * section 3.2 - String Standard Library
>
> * exercise 3.10
> * STATEMENT
> * write a programme to strip the punctation from the string. */
>
> #include <iostream>
> #include <string>
>
> int main()
> {
> std::cout << "Enter a string: ";
> std::string a_word, stripped_word;
> std::cin >> a_word;
>
> for(std::string::size_type ix = 0; ix != a_word.size(); ++ix)
> {
> if(ispunct(a_word[ix]) == 0)
> stripped_word[ix] = a_word[ix];
> }
>
> std::cout << stripped_word << std::endl;
>
> return 0;
> }



SORRY, i forgot to post the output:

[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra ex_03-10.cpp
[arnuld@arch cpp ]% ./a.out
Enter a string: comp.lang.c++

[arnuld@arch cpp ]%


you see, it does not prinit anything at all but it compiled successfully.
where is the bug ?


--
-- http://arnuld.blogspot.com

 
Reply With Quote
 
 
 
 
Obnoxious User
Guest
Posts: n/a
 
      07-18-2007
On Wed, 18 Jul 2007 14:07:59 +0500, arnuld wrote:

> /* C++ Primer 4/e
> * section 3.2 - String Standard Library
>
> * exercise 3.10
> * STATEMENT
> * write a programme to strip the punctation from the string. */
>
> #include <iostream>
> #include <string>
>
> int main()
> {
> std::cout << "Enter a string: ";
> std::string a_word, stripped_word;
> std::cin >> a_word;
>
> for(std::string::size_type ix = 0; ix != a_word.size(); ++ix)
> {
> if(ispunct(a_word[ix]) == 0)
> stripped_word[ix] = a_word[ix];


A std::string is not an open array, you can't just assign
to random non-existing positions.

stripped_word += a_word[ix];
or
stripped_word.append(a_word[ix]);

> }
>
> std::cout << stripped_word << std::endl;
>
> return 0;
> }
>


--
Obnoxious User
 
Reply With Quote
 
anon
Guest
Posts: n/a
 
      07-18-2007
arnuld wrote:
> /* C++ Primer 4/e
> * section 3.2 - String Standard Library
>
> * exercise 3.10
> * STATEMENT
> * write a programme to strip the punctation from the string. */
>
> #include <iostream>
> #include <string>
>
> int main()
> {
> std::cout << "Enter a string: ";
> std::string a_word, stripped_word;
> std::cin >> a_word;
>
> for(std::string::size_type ix = 0; ix != a_word.size(); ++ix)
> {
> if(ispunct(a_word[ix]) == 0)
> stripped_word[ix] = a_word[ix];
> }
>
> std::cout << stripped_word << std::endl;
>
> return 0;
> }
>


Here is the fixed (hack) version:


#include <iostream>
#include <string>

int main()
{
std::cout << "Enter a string: ";
std::string a_word, stripped_word;
std::cin >> a_word;

for(std::string::size_type ix = 0; ix != a_word.size(); ++ix)
{
if(ispunct(a_word.at(ix)) == 0)
stripped_word.append(1,a_word.at(ix));
}

std::cout << stripped_word << std::endl;

return 0;
}
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-18-2007
arnuld wrote:
> /* C++ Primer 4/e
> * section 3.2 - String Standard Library
>
> * exercise 3.10
> * STATEMENT
> * write a programme to strip the punctation from the string. */
>
> #include <iostream>
> #include <string>
>
> int main()
> {
> std::cout << "Enter a string: ";
> std::string a_word, stripped_word;
> std::cin >> a_word;
>
> for(std::string::size_type ix = 0; ix != a_word.size(); ++ix)
> {
> if(ispunct(a_word[ix]) == 0)
> stripped_word[ix] = a_word[ix];
> }
>
> std::cout << stripped_word << std::endl;
>
> return 0;
> }
>

As well as the other suggestions, you could use:

for( std::string::const_iterator ix = a_word.begin();
ix != a_word.end(); ++ix )
{
if(ispunct(*ix) == 0)
stripped_word += *ix;
}


--
Ian Collins.
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      07-18-2007
> On Wed, 18 Jul 2007 09:26:44 +0000, Obnoxious User wrote:


> A std::string is not an open array, you can't just assign to random
> non-existing positions.


ok, got it


> stripped_word += a_word[ix];


this works. we are just concatenating 2 string objects. right ?


> stripped_word.append(a_word[ix]);


[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra ex_03-10.cpp
ex_03-10.cpp: In function ‘int main()’: ex_03-10.cpp:21: error: no
matching function for call to ‘std::basic_string<char,
std::char_traits<char>, std::allocator<char> >::append(char&)’
/usr/lib/gcc/i686-pc-linux-gnu/4.2.1/../../../../include/c++/4.2.1/bits/basic_string.tcc:330:
note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>&
std::basic_string<_CharT, _Traits, _Alloc>::append(const
std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits
= std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.2.1/../../../../include/c++/4.2.1/bits/basic_string.tcc:347:
note: std::basic_string<_CharT, _Traits, _Alloc>&
std::basic_string<_CharT, _Traits, _Alloc>::append(const
std::basic_string<_CharT, _Traits, _Alloc>&, typename
_Alloc::rebind<_CharT>:ther::size_type, typename
_Alloc::rebind<_CharT>:ther::size_type) [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.2.1/../../../../include/c++/4.2.1/bits/basic_string.tcc:303:
note: std::basic_string<_CharT, _Traits, _Alloc>&
std::basic_string<_CharT, _Traits, _Alloc>::append(const _CharT*, typename
_Alloc::rebind<_CharT>:ther::size_type) [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.2.1/../../../../include/c++/4.2.1/bits/basic_string.h:824:
note: std::basic_string<_CharT, _Traits, _Alloc>&
std::basic_string<_CharT, _Traits, _Alloc>::append(const _CharT*) [with
_CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>] <near match>
/usr/lib/gcc/i686-pc-linux-gnu/4.2.1/../../../../include/c++/4.2.1/bits/basic_string.tcc:286:
note: std::basic_string<_CharT, _Traits, _Alloc>&
std::basic_string<_CharT, _Traits, _Alloc>::append(typename
_Alloc::rebind<_CharT>:ther::size_type, _CharT) [with _CharT = char,
_Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
[arnuld@arch cpp ]%
[arnuld@arch cpp ]% g++ --version
g++ (GCC) 4.2.1 20070704 (prerelease) Copyright (C) 2007 Free Software
Foundation, Inc. This is free software; see the source for copying
conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.

[arnuld@arch cpp ]%


--
-- http://arnuld.blogspot.com

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-18-2007
arnuld wrote:
> [..]
> can you tell me how this works or just point me to someplave which
> explains that:
>
> stripped_word.append(1,a_word.at(ix));
>
> i mean, what is "1" doing here ? and why (ix) rather than [ix] ? i am
> at halfway of chapter 3 so i have not come across this "append" member
> function. just covered "size" and "empty" till yet.


RTFM. 'append' is overloaded and one of them takes a number (N) and
the character and appends a string generated from N characters. There
is a constructor that does similar stuff, BTW.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      07-18-2007
> On Wed, 18 Jul 2007 12:04:02 +0200, anon wrote:

> Here is the fixed (hack) version:




> #include <iostream>
> #include <string>
>
> int main()
> {
> std::cout << "Enter a string: ";
> std::string a_word, stripped_word;
> std::cin >> a_word;
>
> for(std::string::size_type ix = 0; ix != a_word.size(); ++ix)
> {
> if(ispunct(a_word.at(ix)) == 0)
> stripped_word.append(1,a_word.at(ix));
> }
>
> std::cout << stripped_word << std::endl;
>
> return 0;



can you tell me how this works or just point me to someplave which
explains that:

stripped_word.append(1,a_word.at(ix));

i mean, what is "1" doing here ? and why (ix) rather than [ix] ? i am at
halfway of chapter 3 so i have not come across this "append" member
function. just covered "size" and "empty" till yet.




--
-- http://arnuld.blogspot.com

 
Reply With Quote
 
anon
Guest
Posts: n/a
 
      07-18-2007
Victor Bazarov wrote:
> arnuld wrote:
>> [..]
>> can you tell me how this works or just point me to someplave which
>> explains that:
>>
>> stripped_word.append(1,a_word.at(ix));
>>
>> i mean, what is "1" doing here ? and why (ix) rather than [ix] ? i am
>> at halfway of chapter 3 so i have not come across this "append" member
>> function. just covered "size" and "empty" till yet.

>
> RTFM. 'append' is overloaded and one of them takes a number (N) and
> the character and appends a string generated from N characters. There
> is a constructor that does similar stuff, BTW.


These guys got very good references:
http://www.cppreference.com/cppstring/index.html
and should answer all questions.


PS If anyone know better references, please let me know.
www.cppreference.com is missing lots of stuff
 
Reply With Quote
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      07-18-2007
On 2007-07-18 18:17, anon wrote:
> Victor Bazarov wrote:
>> arnuld wrote:
>>> [..]
>>> can you tell me how this works or just point me to someplave which
>>> explains that:
>>>
>>> stripped_word.append(1,a_word.at(ix));
>>>
>>> i mean, what is "1" doing here ? and why (ix) rather than [ix] ? i am
>>> at halfway of chapter 3 so i have not come across this "append" member
>>> function. just covered "size" and "empty" till yet.

>>
>> RTFM. 'append' is overloaded and one of them takes a number (N) and
>> the character and appends a string generated from N characters. There
>> is a constructor that does similar stuff, BTW.

>
> These guys got very good references:
> http://www.cppreference.com/cppstring/index.html
> and should answer all questions.
>
>
> PS If anyone know better references, please let me know.
> www.cppreference.com is missing lots of stuff


www.cplusplus.com is quite complete I think, but I like the simplicity
of www.cppreference.com better.

--
Erik Wikström
 
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
response.redirect is not working but server.transfer is working gaurav tyagi ASP .Net 14 01-20-2006 04:22 AM
wifi not working on new hp, or not working after live update =?Utf-8?B?RHJhZ29ueA==?= Wireless Networking 1 10-01-2005 11:17 PM
ASP.NET client-side validation working, but button click not working Alan Silver ASP .Net 1 08-02-2005 03:50 PM
Cookies working on intranet but NOT working on Internet Martin Heuckeroth ASP .Net 5 04-01-2005 01:37 AM
Regular Expression validators NOT working, Required Field validators ARE working Ratman ASP .Net 0 09-14-2004 09:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57