r/programminghelp • • Jul 04 '15

Help with a basic C++ program not working?

Keep in mind I'm using Code::blocks. This program runs without errors, but it just outputs a blank line when I try to run it. I sent my friend the code and he pasted it in to his Code::blocks, and it worked fine. The only difference between us is that I'm using windows and he's using Linux. All my other programs work.

//A program that outputs all characters with their ASCII codes.

#include <iostream>

#include <string>

using namespace std;

int main()

{

    int i;

    while(i<=127){

        cout<<char(i)<<" = "<<i<<endl;

        i++;

    }

}
0 Upvotes

4 comments sorted by

2

u/Marvin4242 Jul 04 '15

Forgot to initialize i to 0, it starts off with a random value. Try i=0; before the loop.

1

u/MildCutlery Jul 04 '15

will do

EDIT: It worked! I don't know why using Linux would change that, but it worked nonetheless. Thanks marvin!

1

u/Marvin4242 Jul 05 '15

It worked because when you do not initialize a local variable the content is completely dependent on where and how the code is loaded and what value happens to be there. Its not based on OS, rather compiler, OS, where its loaded, what was running in that area of RAM before. So being undefined it was probably a value bigger then 127 on one machine and the loop never ran, and may have been 0 in another area and it worked.

1

u/MildCutlery Jul 06 '15

Got it. Thanks once again!