2

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

2
  • how about looping from 0 to the array size & not till ur element is different from '\0"? Commented Jun 15, 2020 at 10:09
  • 1
    None of those arrays contains a null character. Commented Jun 15, 2020 at 10:11

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Agreed that std::array is preferable (at least until the languages fixes the shortcomings of 'native' arrays). However, if one 'must' use a 'native' array, std::size() handles that since C++17.
I like your answer :) I just wanted to point out that \0 is not implicitily converted. \0 IS the null character: open-std.org/JTC1/SC22/WG14/www/docs/n1124.pdf [page 61, example 12]
@underscore_d Oh right, I didn't think of it, I'll edit my answer accordingly. Thanks for the improvement :)
@crsn Thank you for the comment :) I know '\0' is the null character but we are considering here an array of int (and not char), so when the comparison occur, the char is promoted into an int (if I'm not mistaken). By the way, you linked a C documentation, not a C++ one (since they are two completely different languages you can't apply it to both languages, the rules differ a lot).
@crsn Yes, this is right, I just wanted to point out the semantic difference between '\0' as a char and 0 as an integer. Indeed the numeric values are exactly the same (0) but only the first one is called null character (hence the semantic distinction). But I will correct my answer which is indeed incorrectly stated :)
|
0

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++){
     ...
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.