Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 15 Dec 2011 04:45 PM |
In C++, I don't know whether the itoa() function messes with the number when I do it, but somehow when I execute the main() function, it's giving me weird numbers, instead of 'i'. This is the pastebin ID to my code:
/izbWwHUG
I think it's something to do with the code at line 18:
"strcat(str1, itoa(i2, num1, 10))"
..but I can't pinpoint what's wrong with it.. Any solutions to the weird output I'm getting, and how to get the correct output. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 15 Dec 2011 04:47 PM |
Oh yeah, if you want to know the output, this is it:
"The variable 'i' is: 1 The variable 'i' is: 1(weird symbol)1" |
|
|
| Report Abuse |
|
|
NVI
|
  |
| Joined: 11 Jan 2009 |
| Total Posts: 4744 |
|
|
| 15 Dec 2011 04:50 PM |
#include < sstream > //remove spaces #include < string >
//...
int num = 1414523; std::stringstream ss; std::string str; ss << num; ss >> str;
This is a much easier method. It's probably not as fast as itoa, but you ARE using C++... |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 15 Dec 2011 04:53 PM |
pls explain code line by line. >_>
what does the "::" do, does it call it like a function. whaaa?? |
|
|
| Report Abuse |
|
|
NVI
|
  |
| Joined: 11 Jan 2009 |
| Total Posts: 4744 |
|
|
| 15 Dec 2011 04:55 PM |
| :: is to access a namespace. "std" is a namespace. You might wanna brush up on C++'s core syntax before going any further... just saying. |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 15 Dec 2011 05:03 PM |
char str1[] = "The variable 'i' is: ";
That only allocated enough space for the string that you're initializing it to. There's no room in the array for you to concatenate new things onto it after doing that.
What you want is something like this: char str1[256]; strcpy(str1, "The Variable 'i' is: ")
That would leave a bunch of extra space in the array for all the numbers that you're going to concatenate to it.
(By why not just use C++' std::string class instead if you're using C++? It would be infinitely simpler) |
|
|
| Report Abuse |
|
|
NVI
|
  |
| Joined: 11 Jan 2009 |
| Total Posts: 4744 |
|
|
| 15 Dec 2011 05:05 PM |
| Ooooorrrrr, use C++'s string class and don't concern yourself with low-level memory management. :D |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 15 Dec 2011 05:10 PM |
| However, if you can't do the low level string stuff then you'll probably have more trouble later on in cases which are orders of magnitude harder to debug, so you might as well learn it now. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 15 Dec 2011 05:15 PM |
@stravant
I actually thought about that.. but this seems to work..
char str1[] = "The variable 'i' is: "; int i(5); //later..
strcat(str1, itoa(i, str1, 10))
Why does that work and not my first set of code? |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 15 Dec 2011 05:19 PM |
Chance. It really doesn't work, and when you concatenate those thing you're trashing random possibly important memory by writing strings to it.
It just so happens that the slight difference in how the compiler compiled it makes it work now, when it broke something important before. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 15 Dec 2011 05:23 PM |
| Okay, thanks for your help. Let me pick around with your code. =) |
|
|
| Report Abuse |
|
|
|
| 15 Dec 2011 05:23 PM |
| @Stravant, really? I remember making feeling proud of myself because I made a dynamic array (before I learned that that was impossible), which I did through some ridiculous way. That must have been what was happening. |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 15 Dec 2011 05:25 PM |
| Yup, that was definitely what was happening. Usually when you write over the end of an array it doesn't break anything important... however, there's still that 5% of the time that it _will_ break something important, and then it will be hard to figure out what's going wrong. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 15 Dec 2011 05:33 PM |
Now I do a little tweeking, and this is the result:
/CGa3fdTe
..now my code won't ever end. >_> |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 15 Dec 2011 05:56 PM |
| Anyone know what's going on? |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 15 Dec 2011 05:57 PM |
| What do you mean it won't ever end? Works fine on my computer. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 15 Dec 2011 05:58 PM |
O.O
it just wont end, like an infinite loop. |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
| |
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 15 Dec 2011 06:07 PM |
It's going like this:
"The variable 'i' is 1" "The variable 'i' is 2" .. "The variable 'i' is 10" "The variable 'i' is 1"
and then repeapeapeapeapeats. |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 15 Dec 2011 06:08 PM |
| Well idk what's wrong with your compiler. Maybe because itoa is non-standard it works differently on some platforms. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 15 Dec 2011 06:19 PM |
Okay. =/
So it should run normally though, right? The code and all is correct? |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 15 Dec 2011 06:22 PM |
Meh, I retyped the code and all, and it worked perfectly.
=D! |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 15 Dec 2011 06:38 PM |
Here's a conforming copy of the code: /ayRbEMaL
It uses sprintf instead of itoa, because I don't have itoa in my compiler currently, and it's not a standard function anyways. Here's quick look at what the issue was anyways, if you were to look at the stack memory for your thing it looks like this:
-------------- str1[0] str1[1] ... str1[99] num1[0] num2[1] i -------------
Now, everything went fine up until the code in your loop get up to 10. In memory "10" is represented as: {'1', '0', NULL}. That's three characters... num1 is only 2 characters long. No when you did the itoa, the final "NULL" in the string ended up getting written into the next place in the stack... the "i" variable for you loop! Effectively setting it back to 0. So your code would just spin back and forth from 0 up to 10 and then back again, acting as an infinite loop.
Before it worked fine, likely because the padding of your code was such that "i" happened to end up far enough away from "num2" on the stack that the last NULL in the string had space to trash instead of messing up the "i" variable. |
|
|
| Report Abuse |
|
|
Flurite
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
|
| 15 Dec 2011 07:14 PM |
Hey, your right. When I was rewriting my code, I gave num1 the space to have 5 characters, so it worked. Then I changed it back to 2, and wa-la, it became infinite.
Holy cheese dudez, ur liek the judicial branch of a compiler cus u interperet rlly well. |
|
|
| Report Abuse |
|
|