• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 15:53
CEST 21:53
KST 04:53
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
uThermal's 2v2 Tour: $15,000 Main Event5Serral wins EWC 202543Tournament Spotlight: FEL Cracow 202510Power Rank - Esports World Cup 202580RSL Season 1 - Final Week9
Community News
Weekly Cups (Jul 28-Aug 3): herO doubles up6LiuLi Cup - August 2025 Tournaments5[BSL 2025] H2 - Team Wars, Weeklies & SB Ladder10EWC 2025 - Replay Pack4Google Play ASL (Season 20) Announced63
StarCraft 2
General
Rogue Talks: "Koreans could dominate again" uThermal's 2v2 Tour: $15,000 Main Event The GOAT ranking of GOAT rankings RSL Revival patreon money discussion thread Official Ladder Map Pool Update (April 28, 2025)
Tourneys
SC2's Safe House 2 - October 18 & 19 LiuLi Cup - August 2025 Tournaments $5,100+ SEL Season 2 Championship (SC: Evo) WardiTV Mondays RSL Season 2 Qualifier Links and Dates
Strategy
Custom Maps
External Content
Mutation # 485 Death from Below Mutation # 484 Magnetic Pull Mutation #239 Bad Weather Mutation # 483 Kill Bot Wars
Brood War
General
ASL Season 20 Ro24 Groups BGH Auto Balance -> http://bghmmr.eu/ StarCraft player reflex TE scores BW General Discussion Google Play ASL (Season 20) Announced
Tourneys
KCM 2025 Season 3 Small VOD Thread 2.0 [Megathread] Daily Proleagues [ASL20] Online Qualifiers Day 2
Strategy
Fighting Spirit mining rates [G] Mineral Boosting Simple Questions, Simple Answers Muta micro map competition
Other Games
General Games
Nintendo Switch Thread Stormgate/Frost Giant Megathread Total Annihilation Server - TAForever Beyond All Reason [MMORPG] Tree of Savior (Successor of Ragnarok)
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine The Games Industry And ATVI European Politico-economics QA Mega-thread
Fan Clubs
INnoVation Fan Club SKT1 Classic Fan Club!
Media & Entertainment
[\m/] Heavy Metal Thread [Manga] One Piece Movie Discussion! Anime Discussion Thread Korean Music Discussion
Sports
2024 - 2025 Football Thread Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
Gtx660 graphics card replacement Installation of Windows 10 suck at "just a moment" Computer Build, Upgrade & Buying Resource Thread
TL Community
TeamLiquid Team Shirt On Sale The Automated Ban List
Blogs
Gaming After Dark: Poor Slee…
TrAiDoS
[Girl blog} My fema…
artosisisthebest
Sharpening the Filtration…
frozenclaw
ASL S20 English Commentary…
namkraft
momentary artworks from des…
tankgirl
from making sc maps to makin…
Husyelt
Customize Sidebar...

Website Feedback

Closed Threads



Active: 736 users

[C++] Using std::string as a Function Parameter?

Blogs > Revabug
Post a Reply
1 2 Next All
Revabug
Profile Blog Joined June 2009
United Kingdom147 Posts
Last Edited: 2009-07-20 22:41:01
July 20 2009 22:33 GMT
#1
Okay so I've just started to learn C++..

After messing around with std::stings for a while I came up with the abstract idea of creating a function that returned the length of the string (not including whitespace)

Here's what I came up with:
+ Show Spoiler +

int Characters(string strString)
{
int nLength = 0;
for (int nChar = 0; nChar < strString.Length(); nChar++)
{
if (strString[nChar] != ' ')
nLength++;
}
return nLength;
}


However, of course this doesn't compile as std::string isn't a valid function parameter. So I ask, how could I get this to work? I've googled and check various tutorial sites but I haven't found anything on this.



Archaic
Profile Blog Joined March 2008
United States4024 Posts
July 20 2009 22:38 GMT
#2
I'm not a C++ programmer, but do you have to close the parentheses in the if statement? Also, should the for statement be nChar < strString.Length()? I don't know what >> means, but I'm just guessing it is greater than.
Revabug
Profile Blog Joined June 2009
United Kingdom147 Posts
Last Edited: 2009-07-20 22:41:31
July 20 2009 22:40 GMT
#3
Yeah I had to type it out cause it wouldn't copy for some reason so there's some random mistakes but the actual code is fine (well, apart from it doesn't work :X)
micronesia
Profile Blog Joined July 2006
United States24683 Posts
July 20 2009 22:42 GMT
#4
Wait, couldn't you just return strString.length()? What exactly is going on lol.

When I did c++ I always used the AP Include files so I have no idea how to do string without apstring.h XD
ModeratorThere are animal crackers for people and there are people crackers for animals.
Archaic
Profile Blog Joined March 2008
United States4024 Posts
Last Edited: 2009-07-20 22:45:12
July 20 2009 22:44 GMT
#5
micronesia brings up a good point... This function is completely useless. You could just use string.Length() instead of creating a method to do the exact same thing.
EDIT: Just a random question, when using greater/less than signs, is it still shifters in C++ like in Java?
Revabug
Profile Blog Joined June 2009
United Kingdom147 Posts
Last Edited: 2009-07-20 22:45:49
July 20 2009 22:44 GMT
#6
?

I don't want to include whitespaces (as I said in the op), so strString.Length() won't work
GogoKodo
Profile Blog Joined April 2003
Canada1785 Posts
July 20 2009 22:47 GMT
#7
On July 21 2009 07:42 micronesia wrote:
Wait, couldn't you just return strString.length()? What exactly is going on lol.

When I did c++ I always used the AP Include files so I have no idea how to do string without apstring.h XD

He's trying to get the length minus the whitespace I guess.
twitter: @terrancem
VarmVaffel
Profile Blog Joined June 2008
Norway378 Posts
Last Edited: 2009-07-20 22:49:46
July 20 2009 22:48 GMT
#8
In any case, since you say that string isn't a valid parameter, my guess is that you have forgotten to include string.h, or to use the std namespace.
GogoKodo
Profile Blog Joined April 2003
Canada1785 Posts
July 20 2009 22:49 GMT
#9
Not sure about your function since it's been so long since I did c++ but you should be able to google for c++ string function arguments to find the correct way.

On another note, currently you are only getting rid of the ' ' (spaces) in your count, and not any whitespace such as a tab. Might want to look into that as well.
twitter: @terrancem
Revabug
Profile Blog Joined June 2009
United Kingdom147 Posts
July 20 2009 22:49 GMT
#10
(not including whitespace)

I don't want to include whitespaces

He's trying to get the length minus the whitespace I guess.


Adeny
Profile Blog Joined January 2009
Norway1233 Posts
Last Edited: 2009-07-20 22:55:25
July 20 2009 22:50 GMT
#11
Still, the actual length of the string and the string doesn't change, so the returned value from strString.length() will be constant throughout the entire loop no matter how many whitespaces. Just return strString.length() to a nSomething and use nChar < nSomething in the loop. I can't see why this wouldn't work?

To clarify, say your string is "HI MOM".

You get the length of the string which is 6 characters.
You loop through, count the H, nLength ++1;
Loop through, count the I, nLength ++1;
Loop through, won't count the whitespace.
Loop through, counts the M, nLength ++1;
Loop through, counts the O, nLength ++1;
Loop through, counts the M, nLength ++1;

At the end of this, you will have the length of the actual string which will still be 6 characters, and the nLength variable which you individually use to count the characters except for the whitespace which will be 5.
b3h47pte
Profile Blog Joined May 2007
United States1317 Posts
July 20 2009 22:50 GMT
#12
What IDE are you using? I loaded your code up into Visual Studios 2008 and it works fine.
http://pastebin.com/m1fe7c353

Results in

[image loading]


are you making sure you include iostream and using namespace std?
GogoKodo
Profile Blog Joined April 2003
Canada1785 Posts
July 20 2009 22:51 GMT
#13
Ok after a quick google I found some code using this syntax

void function (std::string &str)
{
}
twitter: @terrancem
Archaic
Profile Blog Joined March 2008
United States4024 Posts
July 20 2009 22:52 GMT
#14
Oh, I see what you mean now! If you want to ignore tabs, you may consider using the C++ equivalent of the Java:
strString[nChar] != ' ' && !(strString[nChar]+"").equals("\t");
The second part is converting the character to a string, to use .equals() function (in Java, Strings don't use ==), and comparing it to the string of a tab (\t).
But other than that, I can't really see an obvious problem. You may consider issues not within the code, but within your compiler.
GogoKodo
Profile Blog Joined April 2003
Canada1785 Posts
July 20 2009 22:53 GMT
#15
Make sure you're compiling the correct up to date version of the code and using the updated executable. Silly thing to point out but this is what cause several pages of confused people in konadora's blog asking for programming help.
twitter: @terrancem
Revabug
Profile Blog Joined June 2009
United Kingdom147 Posts
Last Edited: 2009-07-20 22:59:40
July 20 2009 22:59 GMT
#16
I didn't realise I needed to add using namespace std; before the function, I thought I could just add it in int main() where the function would get called because that's when it would be run first..

Thanks
b3h47pte
Profile Blog Joined May 2007
United States1317 Posts
July 20 2009 23:21 GMT
#17
I'm glad I (we) could help! Good luck learning C++! It's a rewarding experience.
2Pacalypse-
Profile Joined October 2006
Croatia9505 Posts
July 20 2009 23:49 GMT
#18
I'm a C programmer, and I'm just wondering did C++ abandon pointers completely or can you still use them?

Moderator"We're a community of geniuses because we've found how to extract 95% of the feeling of doing something amazing without actually doing anything." - Chill
b3h47pte
Profile Blog Joined May 2007
United States1317 Posts
July 20 2009 23:54 GMT
#19
yea you can use pointers in C++.
2Pacalypse-
Profile Joined October 2006
Croatia9505 Posts
July 21 2009 00:08 GMT
#20
Oh ok.
And sorry for taking over the thread, but what's with this string data type?
What's the difference between string type and char[]?

Even though I prefer C over all other programs, I'm gonna have to do some C++ for uni this year so just getting prepared I guess...
Moderator"We're a community of geniuses because we've found how to extract 95% of the feeling of doing something amazing without actually doing anything." - Chill
1 2 Next All
Please log in or register to reply.
Live Events Refresh
uThermal 2v2 Circuit
15:00
Group Stage Day 1
WardiTV995
uThermal899
IndyStarCraft 250
TKL 235
SteadfastSC202
LamboSC2174
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
uThermal 899
IndyStarCraft 250
TKL 235
SteadfastSC 202
LamboSC2 174
ZombieGrub91
BRAT_OK 81
goblin 41
StarCraft: Brood War
Britney 19001
Calm 3070
ggaemo 515
Jaedong 386
Larva 294
actioN 187
TY 38
Aegong 33
Mong 18
yabsab 13
[ Show more ]
IntoTheRainbow 8
NaDa 7
ivOry 3
Stormgate
B2W.Neo210
JuggernautJason109
RushiSC32
UpATreeSC4
Counter-Strike
fl0m1983
Heroes of the Storm
Liquid`Hasu466
Khaldor155
Other Games
gofns11572
Grubby2978
Beastyqt389
KnowMe167
Fuzer 109
oskar106
Livibee78
Trikslyr57
EmSc Tv 14
Organizations
Other Games
EmSc Tv 14
StarCraft 2
EmSc2Tv 14
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 20 non-featured ]
StarCraft 2
• kabyraGe 333
• davetesta23
• tFFMrPink 13
• sooper7s
• AfreecaTV YouTube
• Migwel
• intothetv
• LaughNgamezSOOP
• Kozan
• IndyKCrew
StarCraft: Brood War
• 80smullet 27
• FirePhoenix10
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota21096
• WagamamaTV775
League of Legends
• TFBlade1099
Other Games
• imaqtpie1454
• Shiphtur282
Upcoming Events
RSL Revival
6h 7m
RSL Revival
14h 7m
SC Evo League
16h 7m
uThermal 2v2 Circuit
19h 7m
CSO Cup
20h 7m
Sparkling Tuna Cup
1d 14h
uThermal 2v2 Circuit
1d 19h
Wardi Open
2 days
RotterdaM Event
2 days
Replay Cast
3 days
[ Show More ]
RSL Revival
3 days
The PondCast
5 days
Replay Cast
6 days
LiuLi Cup
6 days
Liquipedia Results

Completed

ASL Season 20: Qualifier #2
FEL Cracow 2025
CC Div. A S7

Ongoing

Copa Latinoamericana 4
Jiahua Invitational
BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Qualifiers
uThermal 2v2 Main Event
HCC Europe
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025

Upcoming

ASL Season 20
CSLPRO Chat StarLAN 3
BSL Season 21
BSL 21 Team A
RSL Revival: Season 2
Maestros of the Game
SEL Season 2 Championship
WardiTV Summer 2025
Thunderpick World Champ.
MESA Nomadic Masters Fall
CS Asia Championships 2025
Roobet Cup 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.