program hangs after printf() with literal string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Avaenuha
    New Member
    • Apr 2007
    • 19

    program hangs after printf() with literal string

    Hi,

    It appears my program can't get past a particular printf() statement.

    Code excerpt:
    [...]
    printf("Sales Report\n--------------");
    printf("Testing code - pre loop entry");

    while(category != null)
    {
    printf("Testing code - in-loop");
    [...]

    (Ignore the category thing, that's just the start of a linkedlist search) The program is written so that at the end of a function, it'll return to main (which will reprint a menu for next selection). However, when I run this option, all i get is:

    Sales Report
    ------------------

    and then it hangs - no segfault, it just sits there, like there's an infinite loop. But given it doesn't even print the 'pre loop test', I can't see how it could BE in an infinite loop - it hasn't even gotten to the loop yet. It's stuck between printf() statements.

    Furthermore, if I replace the "printf("Testin g code - pre loop entry"); with just a 'return;', the funtion returns to the menu after printing the first printf() statement. So it can access a 'return;' statement fine, but it can't read anything else after that first printf().

    I have this written pretty much identically in another function, where it has to print out stock quantities rather than sales, and that works perfectly. I can't find any indication in my reference manuals as to what's causing this, and GDB simply tells me there's a segfault in " ?? () ". I'm really at a loss as to diagnose this - help?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Avaenuha
    Hi,

    It appears my program can't get past a particular printf() statement.

    Code excerpt:
    [...]
    printf("Sales Report\n--------------");
    printf("Testing code - pre loop entry");

    while(category != null)
    {
    printf("Testing code - in-loop");
    [...]

    (Ignore the category thing, that's just the start of a linkedlist search) The program is written so that at the end of a function, it'll return to main (which will reprint a menu for next selection). However, when I run this option, all i get is:

    Sales Report
    ------------------

    and then it hangs - no segfault, it just sits there, like there's an infinite loop. But given it doesn't even print the 'pre loop test', I can't see how it could BE in an infinite loop - it hasn't even gotten to the loop yet. It's stuck between printf() statements.

    Furthermore, if I replace the "printf("Testin g code - pre loop entry"); with just a 'return;', the funtion returns to the menu after printing the first printf() statement. So it can access a 'return;' statement fine, but it can't read anything else after that first printf().

    I have this written pretty much identically in another function, where it has to print out stock quantities rather than sales, and that works perfectly. I can't find any indication in my reference manuals as to what's causing this, and GDB simply tells me there's a segfault in " ?? () ". I'm really at a loss as to diagnose this - help?
    Does your real while loop looks like this by any chance?
    Code:
    while(category != null);
    Note the trailing semi-colon.

    kind regards,

    Jos

    Comment

    • Avaenuha
      New Member
      • Apr 2007
      • 19

      #3
      Originally posted by JosAH
      Does your real while loop looks like this by any chance?
      Code:
      while(category != null);
      Note the trailing semi-colon.

      kind regards,

      Jos
      Nope, no semi-colon after the while loop.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Avaenuha
        Nope, no semi-colon after the while loop.
        Can you copy and paste (the relevant parts) of your code here? Don't type
        it in by hand because subconsciously you might correct certain typos etc.

        kind regards,

        Jos

        Comment

        • Avaenuha
          New Member
          • Apr 2007
          • 19

          #5
          Code from start of the function to several lines after the start of the while loop. Yes, I realise the while loop isn't closed; neither is the function, but it's a long loop, and also part of an assignment, so I'd rather not post the whole while loop unless necessary. (My uni has strict plaguarism rules).

          Code:
          void reportSales(GJCType *menu)
          {
             float smallItemSales;
             float medItemSales;
             float largeItemSales;
             float totalItemSales;
             float smallCatSales;
             float medCatSales;
             float largeCatSales;
             float totalCatSales;
          
             CategoryTypePtr category = menu->headCategory;
             ItemTypePtr item;
          
             printf("Sales Report\n------------\n");
             printf("Testing Code - pre loop entry");
             
             /*Loop through each category*/
             while(category != NULL)
             {
                printf("Testing Code - within loop");
                
                /*reset all category variables*/
                item = category->headItem;
                smallCatSales = 0;
                medCatSales = 0;
                largeCatSales = 0;
                totalCatSales = 0;

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            What happens if you terminate each printed string with '\n'? stdout is line
            buffered and it will not print out a line until it has to print a new line character
            or when its buffer is full. printf() is the poor man's debugger and it's your friend ;-)

            kind regards,

            ps. I can't find the cause of the bug given that code snippet.

            Comment

            • Avaenuha
              New Member
              • Apr 2007
              • 19

              #7
              Originally posted by JosAH
              What happens if you terminate each printed string with '\n'? stdout is line
              buffered and it will not print out a line until it has to print a new line character
              or when its buffer is full. printf() is the poor man's debugger and it's your friend ;-)

              kind regards,

              ps. I can't find the cause of the bug given that code snippet.
              Ah-hah! That is indeed why the testing code won't print. Completely forgot about the newline thing with stdout. (Aaaand the problem exists within the while loop, which is indeed infinite. Oh joy.)

              Problem solved - thanks for your help!

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by Avaenuha
                Ah-hah! That is indeed why the testing code won't print. Completely forgot about the newline thing with stdout. (Aaaand the problem exists within the while loop, which is indeed infinite. Oh joy.)
                Have fun solving the bug ;-)

                Problem solved - thanks for your help!
                You're welcome of course.

                kind regards,

                Jos

                Comment

                Working...