Loop issue

Hello everybody, I've just started my adventure with C++. I have a problem with this bit of code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main()
{
    int x;
    int y = 0;
    int z;

    cout<<"Number: ";
    cin>> x;
    cin.ignore();

    for (int x; y < 11; z=x*y++) {
        cout<< x << " X " << y << " = " << z <<"\n";
        }
    cin.get();

}


The main problem lies within these lines:
1
2
3
for (int x; y < 11; z=x*y++) {
        cout<< x << " X " << y << " = " << z <<"\n";
        }

What I want to recieve is a small chart that displays, for example:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
and so on till 3 x 10, but for any number I put in, say we'll name it "n" (so n * (numbers from 1-10) = proper result), but my compiler says the following:

|14|warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]|

and the results that I get in the running program are, well, quite unusual... I get something like that:
- I input number when it says "Number: "
- I press ENTER
- Program displays: VEEERY high number (not the one inputed) X (numbers from 1-10, as it should) = EVEN HIGHER number.

Can someone tell me what am I doing wrong? Like, any issue with the build of the loop, or the variables? Thanks a lot for any help.

Last edited on
You are creating a new variable named x on line 13. This variable is not the same variable as the variable that you created on line 5. It gives you stange results because you never give the variable a value so the value is undefined (it could be anything).
since x will not be changing, then perhaps:

1
2
3
4
for(int y = 0; y < 10; z = x * ++y) // want to increment y first, unless you want to include x * 0
{
  cout << x << " x " << y << " = " << z << endl;
}


makes more sense?
Thanks for your reply. It kinda both fixed and not. I've noticed my mistake (removed the int before x in the loop) so now it displays properly, but there is another problem. The results are "moved"one line too low (the result are like this:
1 x 0 = some weird number
1 x 1 = 0
1 x 2 = 1
1 x 3 = 2
1 x 4 = 3
and so on.
I think I get the idea where the error is, as the program takes the predetermined y = 0 variable and, while taking it into the loop, automaticaly adds 1 to is (as loop states z=x*y++).
So is there any way to fix that troublesome variable?
@Texan40 your lines actually fixed the "moved results" issue, yet the result for x * 0 are still messed up. I highly apprecieate your concern though.
Weird thing happened. I set the y variable (at the beggining) to -1 and ran the program. Code::Blocks still displays x * 0 as a mess, but the cpp.sh shows it normally (as 0). Which one sholud I believe?
Yeah, I was off on the pre-increment info. I think the issue is that the 3rd parameter in the for statement is evaluated after the loop runs. So if you evaluate z in there, it will always be off the first run.

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

using namespace std;

int main()
{
    int x;
    cout << "Number: ";
    cin >> x;
    cin.ignore(80,'\n');

    for (int y = 1; y < 10; ++y)
    {
      int z = x * y;
      cout<< x << " X " << y << " = " << z <<"\n";
    }

    cin.get();
    return EXIT_SUCCESS;
}


on second thought this looks more like your original code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int x, y = 1;
    cout << "Number: ";
    cin >> x;
    cin.ignore(80,'\n');
    int z = y * x; // correct first iteration
    for (; y < 11; z = x * ++y)
    {
      cout<< x << " X " << y << " = " << z <<"\n";
    }

    cin.get();
    return EXIT_SUCCESS;
}
Last edited on
Works like a charm, buddy. Thanks!
Topic archived. No new replies allowed.