why do i have to add a new variable in this code

  c++

I was trying to write a code that takes two numbers as a input and change the numbers into words in certain rules.

#include <iostream>


int main() {

    string nums[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};

    int x,y;
    cin >> x;
    cin >> y;

    for (int x = x; x <= y; x++){
            if (x <= 9){
                cout << nums[x] << "n";
            }
            else if (x % 2 == 0){
                cout << "even" << "n";
            }
            else {
                cout << "odd" <<"n";
            }

    }
return 0;

}

this is the code I wrote at first. but whatever input I put in, the loop starts from x=0.

#include <iostream>


int main() {

    string nums[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};

    int x,y;
    cin >> x;
    cin >> y;

    for (int i = x; i <= y; i++){
            if (i <= 9){
                cout << nums[i] << "n";
            }
            else if (i % 2 == 0){
                cout << "even" << "n";
            }
            else {
                cout << "odd" <<"n";
            }

    }
return 0;

}

This is the second code I wrote and it worked as I wanted to. I found out that if I add a variable, it works as what I wanted too. I found the solution but I don’t know why I have to add a variable and why the first one always starts from x=0. sorry for my bad English
(both are using namespace std)

Source: Windows Questions C++

LEAVE A COMMENT