Garbage values in array display c++

Viewed 105

When I tried to execute this code am getting the garbage values in the output.

int empid;
int unit500[] = {3,5,6,7,8,9};
int unit1000[] = {4, 6, 7, 8, 9, 10};
int unit924[] = {10, 9, 8, 5, 4, 3};
cout << "Enter emp id: ";
cin >> empid;
if(empid == 2)
{
    cout << empid << endl;
}
else if(empid == 500)
{
    for(int i=0; unit500[i] != '\0'; i++)
    {
        cout << unit500[i];
    }
}
else if(empid == 1000)
{
    for(int j=0; unit1000[j] != '\0'; j++)
    {
        cout << unit1000[j];
    }
}
else if(empid == 924)
{
    for(int k=0; unit924[k] != '\0'; k++)
    {
        cout << unit924[k];
    }
}

Output:

Enter emp id: 500 35678950042012642293652942012642293584229365241989632147332096

3 Answers

Why are you looking for a char inside an array of int ? (Well, this is not an error, I just wanted to point out a consistency mistake here)

Actually, the character '\0' is promoted to the integer 0. Since none of your arrays contain any 0, you'll get an infinite loop for each of your for loops.

But it is probably not the behaviour you was expecting. It seems that you want to display the whole array.
Note that a array is not a C String so don't expect it to be null-terminated !
In this case, you just have to iterate until the size of the array is reached.

For example:

//...
int unit500[] = {3,5,6,7,8,9};

//...

// Get the size of unit500
const unsigned int unit500_size = sizeof(unit500) / sizeof(unit500[0]);

// Or (since C++17)
//const unsigned int unit500_size = std::size(unit500);

// Iterate until the last element of unit500
for(unsigned int i = 0; i < unit500_size; ++i)
{
    std::cout << unit500[i] << ", ";
}

//...

Of course, using std::array would be a better option. You could get the size in a significantly more readable way by simply calling the size() method.

Your condition to end the loop is not adequate. Rather use a counter, loop until the counter is equal to the size of the array

for(int i=0; i<6/*size of array*/; i++){
     ...
}

You can fix it like this:

#include <iostream>
#include <vector>
int main()
{
    int empid;
    std::vector<int> unit500 = { 3,5,6,7,8,9 };
    std::vector<int> unit1000 = { 4, 6, 7, 8, 9, 10 };
    std::vector<int> unit924 = { 10, 9, 8, 5, 4, 3 };
    std::cout << "Enter emp id: ";
    std::cin >> empid;
    if (empid == 2)
    {
        std::cout << empid << std::endl;
    }
    else if (empid == 500)
    {
        for (int i = 0; i != unit500.size(); i++)
        {
            std::cout << unit500[i];
        }
    }
    else if (empid == 1000)
    {
        for (int j = 0; j != unit1000.size(); j++)
        {
            std::cout << unit1000[j];
        }
    }
    else if (empid == 924)
    {
        for (int k = 0; k != unit924.size(); k++)
        {
            std::cout << unit924[k];
        }
    }
}

Garbage value because there is no '\0' character in integer arrays. By the way it's easier to work with vector rather than with arrays.

Related