| View previous topic :: View next topic |
| Author |
Message |
AlbanainRetard Master Cheater
Reputation: 0
Joined: 02 Nov 2008 Posts: 494 Location: Canada eh?
|
Posted: Sun Mar 22, 2009 11:20 pm Post subject: C++ Char-by-Char in String |
|
|
Any idea why this wouldn't work?
| Code: | void Arithmetic(string number)
{
string temp = "";
for(int i = 0; i < number.length(); i++)
{
if(number[i] == '+')
{
WriteLn(temp);
temp = "";
Arithmetic(number.substr(i+1,number.length()-(i+1));
}
temp += number[i];
}
} |
This doesn't work I can't figure it out o.0 (which is odd)
Any ideas? In theory it loops through all characters and if its a + then it calls a new one, yet it messes up.
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Mar 23, 2009 12:44 am Post subject: |
|
|
| F5 would like a word with you
|
|
| Back to top |
|
 |
mStorm Expert Cheater
Reputation: 0
Joined: 21 Feb 2009 Posts: 107
|
Posted: Mon Mar 23, 2009 12:04 pm Post subject: |
|
|
| I don't exactly see what you are trying to accomplish, there is probably an easier way than recursive function calling...
|
|
| Back to top |
|
 |
kitterz Grandmaster Cheater Supreme
Reputation: 0
Joined: 24 Dec 2007 Posts: 1268
|
Posted: Mon Mar 23, 2009 1:47 pm Post subject: |
|
|
try number.length() - 1 instead of number.length()
_________________
|
|
| Back to top |
|
 |
mStorm Expert Cheater
Reputation: 0
Joined: 21 Feb 2009 Posts: 107
|
Posted: Mon Mar 23, 2009 3:15 pm Post subject: |
|
|
This might work for you:
| Code: |
void Arithmetic(string number)
{
for (int i=1; i <=(int)number.length();i++)
{
if (number.substr(i, 1)=="+")
{
cout << number.substr(0, i) << endl;
Arithmetic(number.substr(i+1, number.length() - (i+1)));
return;
}
}
cout << number << endl;
}
|
|
|
| Back to top |
|
 |
AlbanainRetard Master Cheater
Reputation: 0
Joined: 02 Nov 2008 Posts: 494 Location: Canada eh?
|
Posted: Mon Mar 23, 2009 4:33 pm Post subject: |
|
|
@Kitterz Length-1 Wouldn't work It just means I am going less.
@mStorm, thanks I guess next time I should use substr through all the code
@slovach Debugging really does not help.
_________________
|
|
| Back to top |
|
 |
BanMe Master Cheater
Reputation: 0
Joined: 29 Nov 2005 Posts: 375 Location: Farmington NH, USA
|
Posted: Mon Mar 23, 2009 5:51 pm Post subject: |
|
|
@albanian.. debugging DOES help..next time use fucking stoi() to do this shit..and listen to kitterz.. o0
regards BanMe
_________________
don't +rep me..i do not wish to have "status" or "recognition" from you or anyone.. thank you. |
|
| Back to top |
|
 |
kitterz Grandmaster Cheater Supreme
Reputation: 0
Joined: 24 Dec 2007 Posts: 1268
|
Posted: Mon Mar 23, 2009 6:42 pm Post subject: |
|
|
try Arithmetic(number.substr(i+1,number.length()-(i+2));
im not too sure about it. You make the code very confusing.
I suggest that you do it another way.
_________________
|
|
| Back to top |
|
 |
AlbanainRetard Master Cheater
Reputation: 0
Joined: 02 Nov 2008 Posts: 494 Location: Canada eh?
|
Posted: Mon Mar 23, 2009 6:57 pm Post subject: |
|
|
| kitterz wrote: | try Arithmetic(number.substr(i+1,number.length()-(i+2));
im not too sure about it. You make the code very confusing.
I suggest that you do it another way. |
Well I got it working. Anywho I would need to make it go from another direction on list or opperations due to it saying: 1+2 = Add 1, 2.
_________________
|
|
| Back to top |
|
 |
kitterz Grandmaster Cheater Supreme
Reputation: 0
Joined: 24 Dec 2007 Posts: 1268
|
Posted: Mon Mar 23, 2009 7:22 pm Post subject: |
|
|
| AlbanainRetard wrote: | | kitterz wrote: | try Arithmetic(number.substr(i+1,number.length()-(i+2));
im not too sure about it. You make the code very confusing.
I suggest that you do it another way. |
Well I got it working. Anywho I would need to make it go from another direction on list or opperations due to it saying: 1+2 = Add 1, 2. |
why don't you try using strtok()
_________________
|
|
| Back to top |
|
 |
|