0

I'm printing a statement from classes using getter functions. The top 1.5 lines of my cout statement are not printing. I have tried flushing the stream, and I also copied and pasted the lines that are not printing outside the if statement, and it prints! I can't figure out what is going on. Here is the function:

// display all books out on loan
void displayBorrowed(vector<LibraryBook>& book)
{

    cout << "Books currently checked out: " << endl << endl;

    for(unsigned int i = 0; i < book.size(); i++)
    {
        //cout << "ID: " << book[i].getId_() << "  Title: " 
             //<< book[i].getTitle_() << endl << endl;

        if(book[i].getIsLoaned_() == true)
        { 
            std::cout.flush();
            cout << "ID: " << book[i].getId_() << "  Title: "
                 << book[i].getTitle_() << "  Author: " 
                 << book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_()
                 << "  Year Published: " <<  book[i].getYearPubl_() << endl
                 << "Due Date: " << book[i].getDueMonth_() << "/" 
                 << book[i].getDueDay_() << "/" << book[i].getDueYear_()
                 << " Date Borrowed:  " << book[i].getBorrwdMonth_() << "/"
                 << book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_()
                 << endl << "Checked out by: " << book[i].getBorrwFirst_()
                 << " " <<  book[i].getBorrwLast_() << endl << endl;
        }
    }
}

It displays this:

Books currently checked out:

  Author: Brendan Behan  Year Published: 1958
Due Date: 8/2/2017 Date Borrowed:  7/21/2017
Checked out by: Cassie Peterson

If the lines in the if statement are copied out of the if statement it displays normally:

ID: 78620 Title: Zhuan Falun

I tried changing the if statement to false to display all the books not loaned, and they all displayed the same except for the very last book (number 50 finally displayed the id # and the title. I am at a complete loss. What is going on?

It should look like this:

ID: 78620  Title:  Zhuan Falun Author: Brendan Behan  Year Published: 1958
Due Date: 8/2/2017 Date Borrowed:  7/21/2017
Checked out by: Cassie Peterson

(havent formatted display yet)

I just changed it to this where I have every single element that doesn't display in its own cout statement, and NONE of it displays!! What??! (up until author, where it started displaying before, I mean.)

    for(unsigned int i = 0; i < book.size(); i++)
    {
        if(book[i].getIsLoaned_() == true)
        { 
            std::cout.flush();
            cout << "ID: " ;
            cout << book[i].getId_();
            cout << "  Title: ";
            cout <<  book[i].getTitle_();
            cout << "  Author: " 
                 << book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_()
                 << "  Year Published: " <<  book[i].getYearPubl_() << endl
                 << "Due Date: " << book[i].getDueMonth_() << "/" 
                 << book[i].getDueDay_() << "/" << book[i].getDueYear_()
                 << " Date Borrowed:  " << book[i].getBorrwdMonth_() << "/"
                 << book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_()
                 << endl << "Checked out by: " << book[i].getBorrwFirst_()
                 << " " <<  book[i].getBorrwLast_() << endl << endl;
        }

It prints when I put an endl at the end of each element:

        if(book[i].getIsLoaned_() == true)
        { 
            std::cout.flush();
            cout << "ID: " << endl;
            cout << book[i].getId_() << endl;
            cout << "  Title: " << endl;
            cout <<  book[i].getTitle_() << endl;
            cout << "  Author: "  << endl;
            cout << book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_() << endl;
            cout << "  Year Published: " <<  book[i].getYearPubl_() << endl;
            cout << "Due Date: " << book[i].getDueMonth_() << "/"  << endl;
            cout << book[i].getDueDay_() << "/" << book[i].getDueYear_() << endl;
            cout << " Date Borrowed:  " << book[i].getBorrwdMonth_() << "/" << endl;
            cout << book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_() << endl;
            cout << endl << "Checked out by: " << book[i].getBorrwFirst_() << endl;
            cout << " " <<  book[i].getBorrwLast_() << endl << endl;
        }


Books currently checked out:

ID:
47492
  Title:
 Borstal Boy
  Author:
Brendan Behan
  Year Published: 1958
Due Date: 8/
2/2017
 Date Borrowed:  7/
21/2017

Checked out by: Cassie
 Peterson
Katie
  • 21
  • 1
  • 7
  • 2
    Have you tried separating your long cout into several smaller statements? – JGroven Apr 19 '17 at 06:06
  • What does `book[i].getId_()` do? What is the value the function returns? What does `book[i].getTitle_()` do? What is the value the function returns? This is going to be ***really hard*** for us to help you debug. You have to do some of your own debugging, preferably by stepping through the code line by line in a debugger yourself. That's not something we can possibly do. – Some programmer dude Apr 19 '17 at 06:07
  • @JGroven That most likely won't help. Each << actually is a call to a function called `operator<<`, returning the same ostream it received. So it does not make a difference if you do `cout << a << b;` or `cout << a; cout << b;` – Aconcagua Apr 19 '17 at 06:08
  • 2
    Recently a similar issue (in a different SO question) was caused by mixed up line endings and stray `\r`s. Inspect your title string and wherever you are getting it from. – T.C. Apr 19 '17 at 06:09
  • 4
    One possibility is that you have a carriage return character in your member variables. A related post: http://stackoverflow.com/questions/42589903/inconsistent-stdcout-behavior. – R Sahu Apr 19 '17 at 06:10
  • I did try splitting it up into smaller strings. For some reason the first couple lines in the cout statement in the if statement just do not want to display. I have tested all the individual getter functions. They all work. – Katie Apr 19 '17 at 06:11
  • Would the \r be something typed out explicitly, or could it be some kind of ascii code for it or something? because I typed every line and I know there is no \r anywhere. – Katie Apr 19 '17 at 06:14
  • Reordering the parts of your output can help you identify the problematic part. You can observe if it is e.g always missing the first few statements or if it always starts (or ends) after a certain statement. – wkl Apr 19 '17 at 06:14
  • @KLG52486: Character code dec 13, hex 0x0d, AKA Carriage return. Typically found in DOS/Windows style line endings – Hasturkun Apr 19 '17 at 06:16
  • 3
    Redirect the output to a file then look at that file in a hex editor to see if there is a carriage return in the output. – Michael Burr Apr 19 '17 at 06:26
  • Ok- I'm trying to figure out how to do that... I could just post the code but it's really long, which is why I didn't just do that in the first place. – Katie Apr 19 '17 at 06:30
  • There is currently an Xcode bug that might be relevant http://stackoverflow.com/questions/43158839/c-not-showing-cout-in-xcode-console-but-runs-perfectly-in-terminal – Bo Persson Apr 19 '17 at 07:41
  • Thank you, but when I output the stream to a text file instead of the terminal it clearly shows the carriage return is there. I still haven't found it. I changed the file input in case it was coming from there, and I used a hex converter to try to find what I am looking for, but I just don't know how :( – Katie Apr 19 '17 at 17:35

2 Answers2

2

My suggestion:

  1. Print each of the members in their own line.
  2. Find out which member is problematic.
  3. Dig deeper into the contents of the member to understand how it got there and fix it.

    if(book[i].getIsLoaned_() == true)
    { 
        std::cout.flush();
        std::cout << "ID: " << book[i].getId_() << std::endl
                  << "Title: " << book[i].getTitle_() << std::endl
                  << "Author First: " << book[i].getAuthorFirst_() << std::endl
                  << "Author Last:" << book[i].getAuthorLast_() << std::endl
                  << "Year Published: " <<  book[i].getYearPubl_() << std::endl
                  << "Due Date Month: " << book[i].getDueMonth_() << std::endl
                  << "Due Date Day: " << book[i].getDueDay_() << std::endl
                  << "Due Date Year: " << book[i].getDueYear_() << std::endl
                  << "Borrowed Month: " << book[i].getBorrwdMonth_() << std::endl
                  << "Borrowed Day: " << book[i].getBorrwdDay_() << std::endl
                  << "Borrowed Year: " book[i].getBorrwdYear_() << std::endl
                  << "Checked out by first: " << book[i].getBorrwFirst_() << std::endl
                  << "Checked out by last: " <<  book[i].getBorrwLast_() << std::endl
                  << std::endl;
    }
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • When I put an endl at the end of each line everything prints. Do you think that means there is no stray carriage return? – Katie Apr 19 '17 at 06:41
  • 1
    @KLG52486, quite the contrary. It means that there is a carriage return in the title. – R Sahu Apr 19 '17 at 06:49
  • If I put an endl between getTitle and " Author: " then everything displays. Any suggestions on how to figure out how that is causing the problem? – Katie Apr 19 '17 at 06:51
  • Oh, ok- so is it likely to be in the string "title" then? – Katie Apr 19 '17 at 06:52
  • Ahh I found it. I changed the title of the book, and problem solved. Except it just created a new problem. How can I use that book title? – Katie Apr 19 '17 at 06:54
  • @KLG52486, that will be hard to answer in a comment, specially since you haven't shown how the title got to be in that state to begin with. I advise creating a [mcve] that addresses only that problem. – R Sahu Apr 19 '17 at 06:57
  • I was hoping there was just a way to tell the compiler to ignore the carriage return, like ignoring an apostrophe in javascript. If it isn't an easy answer I will just choose a different book. Thank you. – Katie Apr 19 '17 at 07:00
  • 1
    You can wrap the call to `book[i].getTitle_()` in one that trims whitespace. – egrunin Apr 19 '17 at 07:04
  • How would I do that? I still haven't figured out how to get it to work. I sent the output to a file and I can see there is a carriage return after every title, not just the one specific title, so that means it must be in my code right? I can't find it :( – Katie Apr 19 '17 at 17:37
  • @KLG52486, Given a `std::string s;`, you would use `s.erase(std::find(s.begin(), s.end(), '\r'));`. – R Sahu Apr 19 '17 at 17:41
-1

/* Sometimes "half cout statement not printing problem" may occur due to use of dangling or dangerous pointers. In the code below all cout statements that present, after dangling pointer printing statement are not printing anything. If we remove the dangling pointer's cout statement then everything will be fine.

code: // dangling pointer is dangerous because it can hold undesired address. */

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a =5;
    char b= 'a';
    int *p = &a;
    char *p1 =&b;
    int *p3;  // p3 is dangling pointer

    cout<<"size of p "<<sizeof(p)<<endl; // o/p is 8, bcoz x64 bit compiler 
                                           gives 8byte and x32 gives 4 byte.
    cout<<"size of p1 "<<sizeof(p1)<<endl;

    cout<<"size of p3 "<<sizeof(p3)<<endl;
    cout<<"address of p3 is "<<&p3<<endl;

    cout<< "value at p3 is "<<*p3<<endl; //***dangling or dangerous pointer.***
   
    cout<<"hello my name is rahul ";
    cout<<a<<endl;
    cout<<&a<<endl;
    //cout<<*a<<endl;  // shows error (invalid type argument)
    cout<<p<<endl;    // shows base address
    cout<<&p<<endl;  // shows pointers address
    cout<<*p<<endl;  
    cout<<"this is an end"<<endl; 
 
  return 0;
}