why?

this is something simple, ijust dont know why happen this?
Im still learning and try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #include <iostream>
using namespace std;

#define PI 3.1416
int main ()
{
    int x;
    cout << "Escribe un numero:";
    cin >> x;
    cout << "\n";
    if (x >0)
        cout << x <<" es positivo";
    else if (x <0)
        cout << x << " es negativo";
    else cout << x << " es 0";
    cout << '\n';
    int a;
    a=0;
    while (a!=x)
        {cout << a << ',';
    a++;}

}


write a negative in the input and you will understand my question
closed account (EwCjE3v7)
Well if i put in -5 for example, then when we get to our while, a will start of with0 then 1,2,3,4,5,6,7,8,9,10..... But never -5.

Here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 #include <iostream>
using namespace std;

#define PI 3.1416
int main ()
{
    int x;
    cout << "Escribe un numero:";
    cin >> x;
    cout << "\n";
    int a = 0;
    if (x >0)
    {
        cout << x <<" es positivo" << '\n';
        while (a!=x)
        {
          cout << a << ',';
          --a;  // here
         }
    }
    else if (x <0)
    {
        cout << x << " es negativo";
        while (a!=x)
        {
          cout << a << ',';
          ++a;  // here
         }
     }
    else cout << x << " es o";

}
Last edited on
For one, when you enter a negative number, this part of your code will never end.
1
2
3
while (a!=x)
        {cout << a << ',';
    a++;}

since you keep increasing a, and not decreasing it. A better way, would be
1
2
3
4
5
6
7
8
if(a >=x)
while (a!=x)
        {cout << a << ',';
    a++;}
else
while (a!=x)
        {cout << a << ',';
    a--;}
Last edited on
(It will actually end eventually. Once the numbers get too big and it overflows they will loop around and become negative. I would not recommend waiting for this to happen.)
Thanks for the answers :D
And also thanks for the alternative options. Thanks
> It will actually end eventually.
> Once the numbers get too big and it overflows they will loop around and become negative.

When an arithmetic operation on a signed integer results in an overflow, the behaviour is undefined.

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

int main()
{
    int a  = std::numeric_limits<int>::max() ;
    const int b = a ;
    std::cout << "a == " << a << "  b == " << b << '\n' ; 
    
    std::cout << "just before engendering undefined behaviour\n" << std::flush ;
    ++a ; // *** engenders undefined behaviour ***
    std::cout << "after ++a, " ;
    
    if( a == b ) std::cout << "a is equal to b\n" ;
    else if( a > b ) std::cout << "a is greater than b\n" ;
    else std::cout << "a is less than b\n" ;
}

clang++ without -ftrapv
a == 2147483647  b == 2147483647
just before engendering undefined behaviour
after ++a, a is greater than b

g++ without -ftrapv
a == 2147483647  b == 2147483647
just before engendering undefined behaviour
after ++a, a is less than b

clang++ with -ftrapv
a == 2147483647  b == 2147483647
just before engendering undefined behaviour
bash: line 10: 13572 Illegal instruction     (core dumped) ./a.out
undefined behaviour generated a trap

http://coliru.stacked-crooked.com/a/939e16fcd0c2a56a
Topic archived. No new replies allowed.