0

I'm doing a USACO prompt and when I print out the variable i it keeps on outputting a memory location(i think?). Each time I run it, it outputs something different at the end. I'm not sure if there is an uninitialized value since I'm not sure what variable it could be. `

#include <fstream>
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <map>
#include <initializer_list>
#include <unordered_map>
using namespace std;

int main()
{
    ifstream fin("blocks.in");
    ofstream fout("blocks.out");

    int n = 0;
    fin >> n;
    cout << n;
    vector<int> alphab(26);
    vector<int> usedA(26);
    char foo[26] = { 'a', 'b', 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' };
    int doo[26] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    string stringA = "d";
    string stringB = "d";
    for (int i = 0; i < n; i++)
    {
        fin >> stringA >> stringB;
        stringA += stringB;
        
        sort(begin(stringA), end(stringA));
        auto last = unique(begin(stringA), end(stringA));
        stringA.erase(last, end(stringA));
        
        cout << i << "\n";
        /*
        for (int i = 0; i < stringA.length(); i++) 
        {
            for (int j = 0; j < 26; i++) 
            {
                if (stringA[i] == foo[j])
                {
                    doo[j] += 1;
                }
            }
        }
        */
    }
    cout << doo;
}

`

I made sure to give all the variables I have values prior to the loop but it didnt work :(

kate
  • 3
  • 2
  • 1
    What exactly do you expect `cout << doo` to do? – Nathan Pierson Nov 26 '22 at 03:35
  • Did you run your program in a debugger? That'll tell you the exact line that's causing your issue. – Stephen Newell Nov 26 '22 at 03:35
  • print out the array? – kate Nov 26 '22 at 03:35
  • 1
    Nope. If you want to print out an array, you have to loop over it and print out each element. What actually is happening is that `cout << doo` exhibits array-to-pointer decay and prints out the address of the first element of `doo`. – Nathan Pierson Nov 26 '22 at 03:43
  • (Addendum to the above: Arrays of `char` get treated specially, and `cout << foo` when `foo` is a `char*` or `const char*` or similar will assume that _actually_ `foo` must be pointing to a null-terminated array of characters and what's desired is to print out the string represented by this array. But in general, `cout`ing a pointer type or something that has decayed to a pointer type will print the address itself.) – Nathan Pierson Nov 26 '22 at 03:47

1 Answers1

0

When you do cout << doo, your printing the memory location of where doo starts. You need to print a specific index inside of doo to get the value.

For example, cout << doo[0] will print the first value inside your array (if it exists). cout << doo[1] will print the second element, and so on.