When the array is output, some trash is output too

  Kiến thức lập trình

I want to create a program that will output two arrays sequentially. You cannot remove the num variable, since this code fragment is only a small part of a larger program. The problem here is related to declaring arrays.

#include <iostream>
using namespace std;
int main()
{
    int Xnum[2]{ 0,0 };
    int Ynum[2]{ 0,0 };
    int num = 4;
    for (int i = 0; i < num; i++)
    {
        std::cout << "Xnum: " << Xnum[i] << "n";
        std::cout << "Ynum: " << Ynum[i] << "n";
    }

}

Now I’m learning c++ from some material and there is an example that, oddly enough, works.

#include <iostream>
  
int main()
{
    const int max {6};
    int numbers[max];
  
    int i{};
    std::cout << "Enter numbers" << std::endl;
    while(i < max)
    {
        std::cin >> numbers[i];    // вводим число
    }

    for (i =0; i < max; ++i)
    {
        std::cout << numbers[i] << "t";
    }
    std::cout << std::endl;
}

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT