For some reason my string is spitting out pure trash, and I have no idea why it's doing it.
There are no errors kicked out when debugging.

#include <iostream>
#include <string>
using namespace std;
void main(){
    string xx = "";
    string yy = "";
    for( int x = 0; x < 10001; x+= 1 ){
    xx+= x + ", ";
    }
    cout << xx;
    cin >> yy;
}

Found the issue - here is the fixed code

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
void main(){
    string xx = "";
    string yy = "";
    for( int x = 0; x < 10001; x+= 1 ){
    stringstream x1;
    x1 << x;
    xx+= x1.str() + ", ";
    }
    cout << xx;
    cin >> yy;
}

1st, it's int main() and not void main()

2nd, You were actually doing a pointer aritmetic by this line:

xx+= x + ", ";

In your case, ", " is a pointer to a sequence of characters. When you add an integer to that sequence, you are using pointer arithmetic. When you print out something, cout will go into memory at the starting location of that char pointer, and will count starting from 0 the characters. Whenever you add an integer to that pointer, cout will no longer start to show characters from that memory zone which started from 0, but will go further, and will start to print from 0 + your number. If your number exeeds the memory zone length of your pointer, C++ will give you no errors, but will start printing the next things that are stored from that memory, which, to you, may seem garbage. Take this representation for example:

//Your pointer in memory is stored like this:
+---------------------------------
| , |   | \0| other items in memory
+---------------------------------
  0   1   2  3   4   5   6   7

When you provide a number like 10, it will go and print whatever it finds from the memory location 0 (your pointer) + 10.

As another example, take this:

#include <iostream>
using namespace std;
int main(){
    cout<<"aladdin"+3;
    return 0;
}

Your output will be: ddin, since you skipped the first 3 characters.

As for what you try to achive, this will do it, I guess:

#include <iostream>
#include <sstream>
using namespace std;
int main(){
    stringstream str;
    for (int i=0;i<10001;i++) str<<i<<", ";
    cout<<str.str();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.