Thread: Strange test results..

  1. #1
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59

    Strange test results..

    Hi, I'm writing a program that reads data out of a text file and into an array of "student" structures.

    The array of structures is then sorted according to "grade". If two students have the same grade, they are sorted alphabetically.

    The program works, but strangely, when I add another line of information to the text file, the program hangs at the end, as if it's waiting for more input. I don't understand why it would work for one text file, but not the other if they're both ended the same way.

    If someone could take a look, it would really help me out. I guess I could pass this program in like this, but I'm just really curious as to why it wouldn't work for both of my text files.

    This program is set up to be used in a Linux environment like so:
    Code:
    ./ex11 < data
    or, for the second data file (the one that causes an error in the program)
    Code:
    ./ex11 < data2
    Here's a url to download the application:
    http://kouellette16.home.comcast.net/ex11.tar
    This is my first time using the tar utility, lol. I hope I did it correctly.

    Thanks again for any help.

  2. #2
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    In ex11.c
    Code:
    char c;
    It should be int c since EOF is probably -1 and therefore outside the range of what char can hold.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Aia View Post
    In ex11.c
    Code:
    char c;
    It should be int c since EOF is probably -1 and therefore outside the range of what char can hold.
    Really?

    Code:
    #include <stdio.h>
    int main (void) {
    	
    	signed char a ; 
    	a = -1 ; 
    	printf("a is %i\n", a ) ; 
    	return 0 ; 
    }
    Works for me. So, my point being, a char can represent a -1.

    Todd (I didn't look at the OP's code - just replying to the comment)

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    He needs the int.
    Last edited by MacGyver; 12-08-2007 at 05:11 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > Works for me. So, my point being, a char can represent a -1.

    _signed_ char is guaranteed to be able to hold any value from -127 to +127, but whether just plain char is signed or unsigned depends on the platform.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If char is signed, then a char with value 128 will be -1, which will result in EOF. The issue becomes apparent when you have high ASCII values in that range.

  7. #7
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Solved

    The problem was in my cmp function. Instead of returning the difference between the two values, i was just returning a -1, 0, or 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange decimal rounding
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 06-12-2008, 01:05 AM
  2. Strange concat bugg
    By Zarniwoop in forum C Programming
    Replies: 7
    Last Post: 05-01-2008, 09:43 PM
  3. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  4. Contest Results - Snake Numbers
    By pianorain in forum Contests Board
    Replies: 4
    Last Post: 06-22-2006, 09:14 AM
  5. strange numbers, while loops questionable?
    By exluddite in forum C++ Programming
    Replies: 8
    Last Post: 05-06-2004, 11:11 AM