473,325 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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("Testing 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?
Apr 21 '07 #1
7 10899
JosAH
11,448 Expert 8TB
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("Testing 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?
Expand|Select|Wrap|Line Numbers
  1. while(category != null);
Note the trailing semi-colon.

kind regards,

Jos
Apr 21 '07 #2
Does your real while loop looks like this by any chance?
Expand|Select|Wrap|Line Numbers
  1. while(category != null);
Note the trailing semi-colon.

kind regards,

Jos
Nope, no semi-colon after the while loop.
Apr 21 '07 #3
JosAH
11,448 Expert 8TB
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
Apr 21 '07 #4
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).

Expand|Select|Wrap|Line Numbers
  1. void reportSales(GJCType *menu)
  2. {
  3.    float smallItemSales;
  4.    float medItemSales;
  5.    float largeItemSales;
  6.    float totalItemSales;
  7.    float smallCatSales;
  8.    float medCatSales;
  9.    float largeCatSales;
  10.    float totalCatSales;
  11.  
  12.    CategoryTypePtr category = menu->headCategory;
  13.    ItemTypePtr item;
  14.  
  15.    printf("Sales Report\n------------\n");
  16.    printf("Testing Code - pre loop entry");
  17.  
  18.    /*Loop through each category*/
  19.    while(category != NULL)
  20.    {
  21.       printf("Testing Code - within loop");
  22.  
  23.       /*reset all category variables*/
  24.       item = category->headItem;
  25.       smallCatSales = 0;
  26.       medCatSales = 0;
  27.       largeCatSales = 0;
  28.       totalCatSales = 0;
Apr 21 '07 #5
JosAH
11,448 Expert 8TB
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.
Apr 21 '07 #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.
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!
Apr 21 '07 #7
JosAH
11,448 Expert 8TB
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
Apr 21 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: anuradha.k.r | last post by:
hi, i am writing a socket program in python,both client side and server side.I've written the client side which is working perfectly fine(checked it against server program written in C).but as for...
35
by: Henry | last post by:
I was doing this program for an exercise in a book. The point was to create a program that would take a numerical grade from a user and convert it to a letter grade (yeah really easy). I tried to...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
19
by: linzhenhua1205 | last post by:
I want to parse a string like C program parse the command line into argc & argv. I hope don't use the array the allocate a fix memory first, and don't use the memory allocate function like malloc....
5
by: Kuku | last post by:
My Program is crashing at strupr(e2.n); int main() { struct emp {
8
by: Vivek Menon | last post by:
Hi, I am using a C program to write/read from a serial port. The writing part is working perfectly fine. However, I am not able to read the values correctly and display them. To debug this issue I...
3
by: cs | last post by:
Hi, I'm new to C and would appreciate any feedback on the following program, asplit, which splits a file into 2 new files, putting a certain number of lines in the first file, and all the rest...
11
by: icarus | last post by:
Hi, this is a simple temperature converter (Fahrenheit to Celsius to Kelvin). Using gcc 4.0. The user is supposed to enter q or any other non-character to exit the program. Problem with this...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.