Thread: Printf's paranormal activity

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    2

    Printf's paranormal activity

    Hey you all!

    I'm a beginner in C programming and I've a little big problem in a project with the printf() function (I mean, I'm almost sure about that). The function just has to print some fields of an array of emails stored in a databse.

    I'll try to explain it. The problem is in these lines:

    Code:
    //LIST ALL EXISTING EMAILS
        for (int i = 0; i < db->email_count; ++i) {
    
            printf("\n");
            printf("%d.", i);
            printf(" %s  ", db->emails[i].from);
            printf("  %s", db->emails[i].subject);
            printf("%s",db->emails[i].date); //PROBLEM
    
    
        }
        printf(NEW_LINE NEW_LINE);
    
    //SHOW THE CONTENT OF THE SELECTED EMAIL
    
        int select_num = 0;
    
    
        printf(INSTRUCTION1);//asks the user a number
        scanf("%d", select_num);
    
    //ERROR
     
        printf("\nID: %s", db->emails[select_num].id);
        printf("\n\nDate: %s", db->emails[select_num].date);
        printf("\nFrom: %s", db->emails[select_num].from);
        printf("\nTo: %s", db->emails[select_num].to);
        printf("\nSubject:%s", db->emails[select_num].subject);
        printf("\n\nBody: %s", db->emails[select_num].body);
    These lines appear just like that, in the same function in my program. But the printfs obey only until the scanf(). After take the number from the user (a number smaller than the email_count), the program finishes with a: "RUN FAILED (exit value 1, total time: 7s)". That's the "ERROR". You will notice that there is no difference between the first section of printfs and the second one. The question is: WHY?

    I have to say one more thing: the date, in the first section of printfs, it just not appears despite the printf("PROBLEM"). It has the format "Tue, 07 Feb 2017 21:32:46 +0100 (CET)" and I'd swear that's it's correctly saved in the string "db->emails[i].date", because of that:

    I want to make clear that:
    - Firstly, the format (%s or %d) is correct in all cases.
    - The content I want to print is loaded in every address (when I print these fields(date,body,subject) into the function which fills them, the content appears without any problem). In addition, note that in this case, the first section of printfs wouldn't work.
    - There is no special case with the db->emails.date. It follows exactly the same patron as the other strings when I fill it, and I can also print it in the function that put the date in the address.
    - If I don't print the date in both sections, the "ERROR" persists.


    PD: I have a strange problem with the printf and the '\n'. Sometimes, and it depens on the place where I put the '\n' in the printf(), the strings that I want to print appear cut.

    Excuse my poor English, and thank you VERY much.This problem is infuriating!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,424
    Well the first thing I see is that you're not passing a pointer into the scanf() call, you're just sending a plain int.

    By the way a properly configured modern compiler should be able to detect and warn you about this problem.

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    2
    Quote Originally Posted by jimblumberg View Post
    Well the first thing I see is that you're not passing a pointer into the scanf() call, you're just sending a plain int.

    By the way a properly configured modern compiler should be able to detect and warn you about this problem.
    FACEPALM. Unforgivable!

    THANK YOU, KIND SIR.

    And about the \n question... ?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Quote Originally Posted by Belacqua View Post
    I have a strange problem with the printf and the '\n'. Sometimes, and it depens on the place where I put the '\n' in the printf(), the strings that I want to print appear cut.
    Cut, how? Can you give some examples?
    Devoted my life to programming...

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    From memory stdout does not have to flush its output until a new line character is found
    You can get around it by using
    Code:
    fflush(stdout);
    ... after your printf

    fflush(stdout)
    Fact - Beethoven wrote his first symphony in C

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,067
    Quote Originally Posted by Belacqua
    FACEPALM. Unforgivable!
    Well yes, it is rather unforgivable since you can often discover such mistakes (and they can happen even to experienced C programmers!) by compiling at a high warning level and paying attention to the warnings
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looping Activity
    By exoruel in forum C++ Programming
    Replies: 5
    Last Post: 08-11-2014, 05:27 AM
  2. Mouse activity
    By rogster001 in forum C Programming
    Replies: 2
    Last Post: 09-03-2009, 05:52 AM
  3. Get HDD Activity
    By leandroaz in forum C++ Programming
    Replies: 13
    Last Post: 10-10-2008, 12:28 PM
  4. logging key activity
    By threahdead in forum C Programming
    Replies: 1
    Last Post: 10-03-2002, 01:05 AM

Tags for this Thread