Aug 8, 2015 at 5:17pm Aug 8, 2015 at 5:17pm UTC
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 Aug 8, 2015 at 5:20pm Aug 8, 2015 at 5:20pm UTC
Aug 8, 2015 at 6:03pm Aug 8, 2015 at 6:03pm UTC
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?
Aug 9, 2015 at 12:22am Aug 9, 2015 at 12:22am UTC
¿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 Aug 9, 2015 at 12:52am Aug 9, 2015 at 12:52am UTC
Aug 9, 2015 at 4:11am Aug 9, 2015 at 4:11am UTC
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 Aug 9, 2015 at 4:11am Aug 9, 2015 at 4:11am UTC
Aug 9, 2015 at 5:52pm Aug 9, 2015 at 5:52pm UTC
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
stopflagflag2
but 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 Aug 9, 2015 at 6:27pm Aug 9, 2015 at 6:27pm UTC
Aug 9, 2015 at 11:13pm Aug 9, 2015 at 11:13pm UTC
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 Aug 9, 2015 at 11:14pm Aug 9, 2015 at 11:14pm UTC
Aug 9, 2015 at 11:22pm Aug 9, 2015 at 11:22pm UTC
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 Aug 10, 2015 at 6:44pm Aug 10, 2015 at 6:44pm UTC