Why does cout fail to output strings sometimes, but printf has no problem?











up vote
0
down vote

favorite












Problem: when printing items in a hash table, non top-level items do not print correctly.
I add items line-by-line from a text file. First, number of lines are determined, and then, the hash table is constructed, with the size found in the first loop of the file.



hash constructor



hash::hash(unsigned int tSize)
{
this->tableSize = tSize;
hashTable.resize(tableSize);
for (int i = 0; i < tableSize; i++)
{
hashTable[i] = new item;
hashTable[i]->firstLetters = "__";
hashTable[i]->str = "____";
hashTable[i]->next = NULL;
}
}


adding items, setting values, and printing them



void hash::add(std::string key)
{
int index = hafn(key);
if (hashTable[index]->str == "____")
{
hashTable[index]->str = key;
setFirstLetters(index,key[0],key[1]);
}
else
{
item *iter = hashTable[index];
item *n_ptr = new item;
n_ptr->str = key;
setFirstLetters(n_ptr, key[0], key[1]);
n_ptr->next = NULL;
while (iter->next != NULL)
{
iter = iter->next;
}
iter->next = n_ptr;
}
}

void hash::setFirstLetters(item *n_ptr, char a, char b)
{
n_ptr->firstLetters[0] = a;
n_ptr->firstLetters[1] = b;
}

void hash::setFirstLetters(int index, char a, char b)
{
hashTable[index]->firstLetters[0] = a;
hashTable[index]->firstLetters[1] = b;
}


void hash::print()
{
int num;
for (int i = 0; i < tableSize; i++)
{
num = numInIndex(i);
printer(num, i);
if (num > 1)
{
int c = 0;
for (int j = num - 1; j > 0; j--)
{
printer(c, num, i);
c++;
}
}
}
}

void hash::printer(int num, int i)
{
item *iter = hashTable[i];
cout << "-------------------------------" << endl;
cout << "index = " << i << endl;
cout << iter->str << endl;
cout << iter->firstLetters << endl;
cout << "# of items = " << num << endl;
cout << "-------------------------------" << endl;
cout << endl;
}

void hash::printer(int numIn, int num, int i)
{
item *iter = hashTable[i];
for (int j = 0; j < numIn; j++)
{
iter = iter->next;
}
cout << "-------------------------------" << endl;
cout << "index = " << i << endl;
cout << iter->str << endl;
cout << std::flush;
cout << iter->firstLetters << endl; //this does not work, though the pointer is pointing to the correct values
//printf("%sn", iter->firstLetters.c_str()); //this works, even without flushing
cout << "# of items = " << num << endl;
cout << "-------------------------------" << endl;
cout << endl;
}


struct item
{
std::string str;
std::string firstLetters;
item *next;
};


The problem is that firstLetters does not print correctly. firstLetters is set correctly. However, in third- and later level items (ie, using the same hash index), firstLetters does not print at all.



In order to be more clear, here is an output example:



-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
good

# of items = 3
-------------------------------


A correct example of output would be this:



-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
good
go
# of items = 3
-------------------------------









share|improve this question
























  • I have been using the debugger continually. On the line that firstLetters is printed, that variable is correct inside struct item
    – Adam
    Nov 12 at 23:42










  • You seem to be expecting us to debug your uncompilable code.
    – Neil Butterworth
    Nov 12 at 23:44










  • I am not. The code does compile. I haven't given every single piece of code, only the relevant ones. I just need help on situations that cout will not print, even though the output stream has been flushed.
    – Adam
    Nov 12 at 23:45






  • 2




    Bug in there somewhere that isn't jumping out at me. Could we trouble you for a Minimal, Complete, and Verifiable example? This is important because if you don't know what the bug is, how can you know what is and what isn't relevant? The real beauty of the MCVE is usually you get in part way and the reduced code to bug ratio reveals the bug to you.
    – user4581301
    Nov 12 at 23:46






  • 1




    @Adam You are initializing firstLetters = "__" only for the initial top-level item objects that the hash constructor creates, but you are NOT initializing firstLetters to anything in the secondary item objects that hash::add() creates, so n_ptr->firstLetters is blank when setFirstLetters(n_ptr, ...) is called, so setFirstLetters() tries to index into invalid memory. If you want firstLetters to alows be initialized as "__", you should add a constructor to the item` struct for that.
    – Remy Lebeau
    Nov 13 at 0:14

















up vote
0
down vote

favorite












Problem: when printing items in a hash table, non top-level items do not print correctly.
I add items line-by-line from a text file. First, number of lines are determined, and then, the hash table is constructed, with the size found in the first loop of the file.



hash constructor



hash::hash(unsigned int tSize)
{
this->tableSize = tSize;
hashTable.resize(tableSize);
for (int i = 0; i < tableSize; i++)
{
hashTable[i] = new item;
hashTable[i]->firstLetters = "__";
hashTable[i]->str = "____";
hashTable[i]->next = NULL;
}
}


adding items, setting values, and printing them



void hash::add(std::string key)
{
int index = hafn(key);
if (hashTable[index]->str == "____")
{
hashTable[index]->str = key;
setFirstLetters(index,key[0],key[1]);
}
else
{
item *iter = hashTable[index];
item *n_ptr = new item;
n_ptr->str = key;
setFirstLetters(n_ptr, key[0], key[1]);
n_ptr->next = NULL;
while (iter->next != NULL)
{
iter = iter->next;
}
iter->next = n_ptr;
}
}

void hash::setFirstLetters(item *n_ptr, char a, char b)
{
n_ptr->firstLetters[0] = a;
n_ptr->firstLetters[1] = b;
}

void hash::setFirstLetters(int index, char a, char b)
{
hashTable[index]->firstLetters[0] = a;
hashTable[index]->firstLetters[1] = b;
}


void hash::print()
{
int num;
for (int i = 0; i < tableSize; i++)
{
num = numInIndex(i);
printer(num, i);
if (num > 1)
{
int c = 0;
for (int j = num - 1; j > 0; j--)
{
printer(c, num, i);
c++;
}
}
}
}

void hash::printer(int num, int i)
{
item *iter = hashTable[i];
cout << "-------------------------------" << endl;
cout << "index = " << i << endl;
cout << iter->str << endl;
cout << iter->firstLetters << endl;
cout << "# of items = " << num << endl;
cout << "-------------------------------" << endl;
cout << endl;
}

void hash::printer(int numIn, int num, int i)
{
item *iter = hashTable[i];
for (int j = 0; j < numIn; j++)
{
iter = iter->next;
}
cout << "-------------------------------" << endl;
cout << "index = " << i << endl;
cout << iter->str << endl;
cout << std::flush;
cout << iter->firstLetters << endl; //this does not work, though the pointer is pointing to the correct values
//printf("%sn", iter->firstLetters.c_str()); //this works, even without flushing
cout << "# of items = " << num << endl;
cout << "-------------------------------" << endl;
cout << endl;
}


struct item
{
std::string str;
std::string firstLetters;
item *next;
};


The problem is that firstLetters does not print correctly. firstLetters is set correctly. However, in third- and later level items (ie, using the same hash index), firstLetters does not print at all.



In order to be more clear, here is an output example:



-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
good

# of items = 3
-------------------------------


A correct example of output would be this:



-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
good
go
# of items = 3
-------------------------------









share|improve this question
























  • I have been using the debugger continually. On the line that firstLetters is printed, that variable is correct inside struct item
    – Adam
    Nov 12 at 23:42










  • You seem to be expecting us to debug your uncompilable code.
    – Neil Butterworth
    Nov 12 at 23:44










  • I am not. The code does compile. I haven't given every single piece of code, only the relevant ones. I just need help on situations that cout will not print, even though the output stream has been flushed.
    – Adam
    Nov 12 at 23:45






  • 2




    Bug in there somewhere that isn't jumping out at me. Could we trouble you for a Minimal, Complete, and Verifiable example? This is important because if you don't know what the bug is, how can you know what is and what isn't relevant? The real beauty of the MCVE is usually you get in part way and the reduced code to bug ratio reveals the bug to you.
    – user4581301
    Nov 12 at 23:46






  • 1




    @Adam You are initializing firstLetters = "__" only for the initial top-level item objects that the hash constructor creates, but you are NOT initializing firstLetters to anything in the secondary item objects that hash::add() creates, so n_ptr->firstLetters is blank when setFirstLetters(n_ptr, ...) is called, so setFirstLetters() tries to index into invalid memory. If you want firstLetters to alows be initialized as "__", you should add a constructor to the item` struct for that.
    – Remy Lebeau
    Nov 13 at 0:14















up vote
0
down vote

favorite









up vote
0
down vote

favorite











Problem: when printing items in a hash table, non top-level items do not print correctly.
I add items line-by-line from a text file. First, number of lines are determined, and then, the hash table is constructed, with the size found in the first loop of the file.



hash constructor



hash::hash(unsigned int tSize)
{
this->tableSize = tSize;
hashTable.resize(tableSize);
for (int i = 0; i < tableSize; i++)
{
hashTable[i] = new item;
hashTable[i]->firstLetters = "__";
hashTable[i]->str = "____";
hashTable[i]->next = NULL;
}
}


adding items, setting values, and printing them



void hash::add(std::string key)
{
int index = hafn(key);
if (hashTable[index]->str == "____")
{
hashTable[index]->str = key;
setFirstLetters(index,key[0],key[1]);
}
else
{
item *iter = hashTable[index];
item *n_ptr = new item;
n_ptr->str = key;
setFirstLetters(n_ptr, key[0], key[1]);
n_ptr->next = NULL;
while (iter->next != NULL)
{
iter = iter->next;
}
iter->next = n_ptr;
}
}

void hash::setFirstLetters(item *n_ptr, char a, char b)
{
n_ptr->firstLetters[0] = a;
n_ptr->firstLetters[1] = b;
}

void hash::setFirstLetters(int index, char a, char b)
{
hashTable[index]->firstLetters[0] = a;
hashTable[index]->firstLetters[1] = b;
}


void hash::print()
{
int num;
for (int i = 0; i < tableSize; i++)
{
num = numInIndex(i);
printer(num, i);
if (num > 1)
{
int c = 0;
for (int j = num - 1; j > 0; j--)
{
printer(c, num, i);
c++;
}
}
}
}

void hash::printer(int num, int i)
{
item *iter = hashTable[i];
cout << "-------------------------------" << endl;
cout << "index = " << i << endl;
cout << iter->str << endl;
cout << iter->firstLetters << endl;
cout << "# of items = " << num << endl;
cout << "-------------------------------" << endl;
cout << endl;
}

void hash::printer(int numIn, int num, int i)
{
item *iter = hashTable[i];
for (int j = 0; j < numIn; j++)
{
iter = iter->next;
}
cout << "-------------------------------" << endl;
cout << "index = " << i << endl;
cout << iter->str << endl;
cout << std::flush;
cout << iter->firstLetters << endl; //this does not work, though the pointer is pointing to the correct values
//printf("%sn", iter->firstLetters.c_str()); //this works, even without flushing
cout << "# of items = " << num << endl;
cout << "-------------------------------" << endl;
cout << endl;
}


struct item
{
std::string str;
std::string firstLetters;
item *next;
};


The problem is that firstLetters does not print correctly. firstLetters is set correctly. However, in third- and later level items (ie, using the same hash index), firstLetters does not print at all.



In order to be more clear, here is an output example:



-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
good

# of items = 3
-------------------------------


A correct example of output would be this:



-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
good
go
# of items = 3
-------------------------------









share|improve this question















Problem: when printing items in a hash table, non top-level items do not print correctly.
I add items line-by-line from a text file. First, number of lines are determined, and then, the hash table is constructed, with the size found in the first loop of the file.



hash constructor



hash::hash(unsigned int tSize)
{
this->tableSize = tSize;
hashTable.resize(tableSize);
for (int i = 0; i < tableSize; i++)
{
hashTable[i] = new item;
hashTable[i]->firstLetters = "__";
hashTable[i]->str = "____";
hashTable[i]->next = NULL;
}
}


adding items, setting values, and printing them



void hash::add(std::string key)
{
int index = hafn(key);
if (hashTable[index]->str == "____")
{
hashTable[index]->str = key;
setFirstLetters(index,key[0],key[1]);
}
else
{
item *iter = hashTable[index];
item *n_ptr = new item;
n_ptr->str = key;
setFirstLetters(n_ptr, key[0], key[1]);
n_ptr->next = NULL;
while (iter->next != NULL)
{
iter = iter->next;
}
iter->next = n_ptr;
}
}

void hash::setFirstLetters(item *n_ptr, char a, char b)
{
n_ptr->firstLetters[0] = a;
n_ptr->firstLetters[1] = b;
}

void hash::setFirstLetters(int index, char a, char b)
{
hashTable[index]->firstLetters[0] = a;
hashTable[index]->firstLetters[1] = b;
}


void hash::print()
{
int num;
for (int i = 0; i < tableSize; i++)
{
num = numInIndex(i);
printer(num, i);
if (num > 1)
{
int c = 0;
for (int j = num - 1; j > 0; j--)
{
printer(c, num, i);
c++;
}
}
}
}

void hash::printer(int num, int i)
{
item *iter = hashTable[i];
cout << "-------------------------------" << endl;
cout << "index = " << i << endl;
cout << iter->str << endl;
cout << iter->firstLetters << endl;
cout << "# of items = " << num << endl;
cout << "-------------------------------" << endl;
cout << endl;
}

void hash::printer(int numIn, int num, int i)
{
item *iter = hashTable[i];
for (int j = 0; j < numIn; j++)
{
iter = iter->next;
}
cout << "-------------------------------" << endl;
cout << "index = " << i << endl;
cout << iter->str << endl;
cout << std::flush;
cout << iter->firstLetters << endl; //this does not work, though the pointer is pointing to the correct values
//printf("%sn", iter->firstLetters.c_str()); //this works, even without flushing
cout << "# of items = " << num << endl;
cout << "-------------------------------" << endl;
cout << endl;
}


struct item
{
std::string str;
std::string firstLetters;
item *next;
};


The problem is that firstLetters does not print correctly. firstLetters is set correctly. However, in third- and later level items (ie, using the same hash index), firstLetters does not print at all.



In order to be more clear, here is an output example:



-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
good

# of items = 3
-------------------------------


A correct example of output would be this:



-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------

-------------------------------
index = 15
good
go
# of items = 3
-------------------------------






c++ hash printf cout string-hashing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 0:28

























asked Nov 12 at 23:33









Adam

125




125












  • I have been using the debugger continually. On the line that firstLetters is printed, that variable is correct inside struct item
    – Adam
    Nov 12 at 23:42










  • You seem to be expecting us to debug your uncompilable code.
    – Neil Butterworth
    Nov 12 at 23:44










  • I am not. The code does compile. I haven't given every single piece of code, only the relevant ones. I just need help on situations that cout will not print, even though the output stream has been flushed.
    – Adam
    Nov 12 at 23:45






  • 2




    Bug in there somewhere that isn't jumping out at me. Could we trouble you for a Minimal, Complete, and Verifiable example? This is important because if you don't know what the bug is, how can you know what is and what isn't relevant? The real beauty of the MCVE is usually you get in part way and the reduced code to bug ratio reveals the bug to you.
    – user4581301
    Nov 12 at 23:46






  • 1




    @Adam You are initializing firstLetters = "__" only for the initial top-level item objects that the hash constructor creates, but you are NOT initializing firstLetters to anything in the secondary item objects that hash::add() creates, so n_ptr->firstLetters is blank when setFirstLetters(n_ptr, ...) is called, so setFirstLetters() tries to index into invalid memory. If you want firstLetters to alows be initialized as "__", you should add a constructor to the item` struct for that.
    – Remy Lebeau
    Nov 13 at 0:14




















  • I have been using the debugger continually. On the line that firstLetters is printed, that variable is correct inside struct item
    – Adam
    Nov 12 at 23:42










  • You seem to be expecting us to debug your uncompilable code.
    – Neil Butterworth
    Nov 12 at 23:44










  • I am not. The code does compile. I haven't given every single piece of code, only the relevant ones. I just need help on situations that cout will not print, even though the output stream has been flushed.
    – Adam
    Nov 12 at 23:45






  • 2




    Bug in there somewhere that isn't jumping out at me. Could we trouble you for a Minimal, Complete, and Verifiable example? This is important because if you don't know what the bug is, how can you know what is and what isn't relevant? The real beauty of the MCVE is usually you get in part way and the reduced code to bug ratio reveals the bug to you.
    – user4581301
    Nov 12 at 23:46






  • 1




    @Adam You are initializing firstLetters = "__" only for the initial top-level item objects that the hash constructor creates, but you are NOT initializing firstLetters to anything in the secondary item objects that hash::add() creates, so n_ptr->firstLetters is blank when setFirstLetters(n_ptr, ...) is called, so setFirstLetters() tries to index into invalid memory. If you want firstLetters to alows be initialized as "__", you should add a constructor to the item` struct for that.
    – Remy Lebeau
    Nov 13 at 0:14


















I have been using the debugger continually. On the line that firstLetters is printed, that variable is correct inside struct item
– Adam
Nov 12 at 23:42




I have been using the debugger continually. On the line that firstLetters is printed, that variable is correct inside struct item
– Adam
Nov 12 at 23:42












You seem to be expecting us to debug your uncompilable code.
– Neil Butterworth
Nov 12 at 23:44




You seem to be expecting us to debug your uncompilable code.
– Neil Butterworth
Nov 12 at 23:44












I am not. The code does compile. I haven't given every single piece of code, only the relevant ones. I just need help on situations that cout will not print, even though the output stream has been flushed.
– Adam
Nov 12 at 23:45




I am not. The code does compile. I haven't given every single piece of code, only the relevant ones. I just need help on situations that cout will not print, even though the output stream has been flushed.
– Adam
Nov 12 at 23:45




2




2




Bug in there somewhere that isn't jumping out at me. Could we trouble you for a Minimal, Complete, and Verifiable example? This is important because if you don't know what the bug is, how can you know what is and what isn't relevant? The real beauty of the MCVE is usually you get in part way and the reduced code to bug ratio reveals the bug to you.
– user4581301
Nov 12 at 23:46




Bug in there somewhere that isn't jumping out at me. Could we trouble you for a Minimal, Complete, and Verifiable example? This is important because if you don't know what the bug is, how can you know what is and what isn't relevant? The real beauty of the MCVE is usually you get in part way and the reduced code to bug ratio reveals the bug to you.
– user4581301
Nov 12 at 23:46




1




1




@Adam You are initializing firstLetters = "__" only for the initial top-level item objects that the hash constructor creates, but you are NOT initializing firstLetters to anything in the secondary item objects that hash::add() creates, so n_ptr->firstLetters is blank when setFirstLetters(n_ptr, ...) is called, so setFirstLetters() tries to index into invalid memory. If you want firstLetters to alows be initialized as "__", you should add a constructor to the item` struct for that.
– Remy Lebeau
Nov 13 at 0:14






@Adam You are initializing firstLetters = "__" only for the initial top-level item objects that the hash constructor creates, but you are NOT initializing firstLetters to anything in the secondary item objects that hash::add() creates, so n_ptr->firstLetters is blank when setFirstLetters(n_ptr, ...) is called, so setFirstLetters() tries to index into invalid memory. If you want firstLetters to alows be initialized as "__", you should add a constructor to the item` struct for that.
– Remy Lebeau
Nov 13 at 0:14



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271641%2fwhy-does-cout-fail-to-output-strings-sometimes-but-printf-has-no-problem%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271641%2fwhy-does-cout-fail-to-output-strings-sometimes-but-printf-has-no-problem%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?