Hi for some reason my array prints out a bunch of 0's and other unwanted integer garbage. I can't see why because i originally assigned number to an array 1-31 using something like array[]={1, 2,3...31}

something like that, and then i use a for or while loop in the code to print the array using cout, however i get a bunch of 0's i'm confused.
please look over a bit of my code.

int temp=dayselector; //temp will equal 4 or anything
while (temp >= 0)
{
cout << "  ";
temp--;
}

 int i=0; 
while (counter > 0) //counter would be 31
{
    if (dayselector < 8)
    {
    cout << array[counter] << "  ";
    dayselector++;
    counter--;
    i++;
    }
    else
    {
    cout << "\n";
    dayselector=0;
    }
}
}

Dani AI

Generated

Most likely causes: out-of-range indexing or uninitialized memory. asked for the array declaration and asked what counter holds — those two facts usually explain the exact symptom. If the array has 31 elements the valid indexes are 0..30; using an index equal to 31 or using a loop that never visits index 0 will read past the end (undefined behaviour) or skip values, which shows up as zeros or “garbage”. Partially-initialised aggregates yield zeros for the remaining elements; automatic (stack) arrays left uninitialized contain indeterminate data.

Quick checklist to diagnose and fix

  • Print the array size (or use vector::size() / std::array). Confirm the index range you use matches 0..size-1.
  • Verify how counter and dayselector are initialized and whether either is being reused as both an offset and an index — keep offset and index in separate variables.
  • Prefer a clear loop: iterate for (i = 0; i < n; ++i) (or for (i = n-1; i >= 0; --i) for descending) rather than a confusing while-condition that can skip 0 or start at n.
  • If you pass a C-style array to a function, remember sizeof won’t give the element count; pass the size or use std::vector/std::array.

Safe example (fills days 1..31 and prints a calendar row-wise):

#include <iostream>
#include <vector>
#include <iomanip>

std::vector<int> days(31);
for (int i = 0; i < (int)days.size(); ++i) days[i] = i + 1;

int offset = 3; // weekday column 0..6
for (int i = 0; i < offset; ++i) std::cout << "   ";
for (int i = 0; i < (int)days.size(); ++i) {
    std::cout << std::setw(2) << days[i] << ' ';
    if ((offset + i + 1) % 7 == 0) std::cout << '\n';
}

Troubleshooting tips: print debug values for the index, offset and array size before accessing the array; use assertions (assert(index >= 0 && index < n)) or run with AddressSanitizer/Valgrind to catch out-of-bounds reads. These checks will reveal whether the zeros/garbage come from uninitialised elements or from undefined behaviour due to incorrect indexing.

Recommended Answers

All 2 Replies

Can you show us where you define the array itself? It might be easier if you post most/all of the source code.

And what is the variable " counter " set to ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.