programs only prints part of the results/simple tasks

This is my program -- I am experimenting with data type sizes. Unfortunately I am getting strange results, and would like your help as to what to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>

using namespace std;

int main()
{

    unsigned short x = 65535; // largest 16-bit unsigned value possible
    cout << "x was: " << x << endl;
    x = x + 1; // 65536 is out of our range -- we get overflow because x can't hold 17 bits
    cout << "x is now: " << x << endl;
    x = 0;
    cout<<"byte size "<<sizeof(int);
    	for(int i = 0; i<65538; ++i)
    {
		    	++x;
    	cout<<x<<" ";
    }
	cin>>x;

    return 0;

}


compiler output:

byte size
byte size

I would at the very least expect to see the x variable output before the byte size output -

Sometimes unpredictably it will print the x data as well.

Thanks in advance
Last edited on
Strange. Your program is correct and it works for me
http://coliru.stacked-crooked.com/a/e4c64a660b391aa1
(had to modify it slightly to avoid rejection for long output)
compiler program output:
byte size
byte size (¿twice?)


> I would at the very least expect to see the x variable output before the byte size output
you print a lot, ¿what do you do in order to see the first lines?
¿what do you do in order to see the first lines?]



1
2
cout << "x was: " << x << endl;
    x = x + 1;     cout << "x is now: " << x << endl;


Also the program continues to run or say it's running after, even after stopping the ide.

Now it turns out 2 first couple of of lines, but thats it:
1
2
x was: 65535
x is now: 0


I run the following simple loop and I get NO program output:
1
2
3
4
5
6
7
8
int x = 0;
	for(int i = 0; i<65538; ++i)
    {

		//cin>>j;
    	++x;
    	cout<<x<<" ";
    }


this works fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{

	int x = 365;
	cout<< x <<endl;
	for (int i = 0; i<120; ++i)
	{
		cout<<++ x<<endl;

	}
    return 0;

}
Last edited on
BY FAR the most interesting evidence of an inverted universe is this example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
cout<<"stop";
	short a(0);
	cout<<"flag";
	for (int i(0); i<0; ++i)
			{
			 cout<<++a<<endl;
			}
	cout<<"flag2";
	return 0;

}

as i added in the semantic flags ("stop", "flag", "flag2") the program output would NOT preint anything in between. not the loop
program output: stopflagflag2
for (int i(0); i<0; ++i) the condition `i<0' is false


> the program continues to run or say it's running after, even after stopping the ide.
¿why should stop? It just becomes an orphan.


> ¿what do you do in order to see the first lines?
what I mean is if you redirect to file, pipe to `more', scroll back or freeze the terminal.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
cout<<"stop";
	short a(0);
	cout<<"flag";
	for (short i(0); i>100; ++i)
			{
			 //cout<<++a<<endl;
			 cout<<i<<endl;
			}
	cout<<"flag2";
	return 0;

}


This will output

stopflagflag2but skip the loop entirely...

Then this works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
cout<<"stop";
	short a(0);
	cout<<"flag";
	for (short i(1); i<=100; ++i)
			{
			 //cout<<++a<<endl;
			 cout<<i<<endl;
			}
	cout<<"flag2";
	return 0;

}


I think the error lie in the true/false conditions of the for loops (middle T/F). For instance I thought
i<=100; ++i)
meant to iterate the loop to <=100. What I understand now is that it is an evaluation: if true iterate the stuff in {}, if its false--stop. If my loops were false at the outset, it wouldn't increment at all. Go figure.
Last edited on
1
2
3
4
5
6
7
8
9
for( initialization; condition; increment )
   statement

//equivalent to
initialization
while( condition ){
   statement
   increment
}
this works: an output of 1000 vars

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
	int x = 0;

	for(int i = 0; i<1000; ++i)
    {

		//cin>>j;
    	++x;
    	cout<<x<<" ";
     }

	return 0;
}


Change this to i<10000 (see below - only change made ):no output at all!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
	int x = 0;

	for(int i = 0; i<10000; ++i)
    {

		//cin>>j;
    	++x;
    	cout<<x<<" ";
     }

	return 0;
}


NO OUPUT
Last edited on
I checked my code and execution with IDEONE and it had no problem with running the above code with i<10000.

If I run the code on my IDE with i<10000 thre is zero output.

https://ideone.com/2FyFfu

Also tried a different pc IDE-Qt- ( I currently use eclipse) and it complains about the compiler:

1
2
3
4
:-1: error: C:/Qt/Tools/mingw491_32/bin/../libexec/gcc/i686-w64-mingw32/4.9.1/liblto_plugin-0.dll: error loading plugin: 
"C:\Qt\Tools\mingw491_32\bin\..\libexec\gcc
\i686-w64-mingw32\4.9.1\liblto_plugin-0.dll":


Looks like a reinstall? Or PC refresh?`

Last edited on
Topic archived. No new replies allowed.