Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: C problem

  1. #1
    Senior Member
    Join Date
    Oct 2001
    Posts
    385

    C problem

    I've been trying to get a C program to work, but I keep getting errors when I run it, though it builds fine. If you can, please take a look at it, build it, and report any errors or fixes if you can. I've tried two different compilers(DJGPP and Visual C++ Intro. Ed. from the Sams Teach Yourself C in 21 Days book, and I know the stubify.exe error), and while one part only create an error in one (even after a couple reinstalls), another creates an error in both.

    Error #1 (DJGPP only):
    [code]
    Choose one of the following:
    1 - Get character from number
    2 - Get number from character
    3 - Quit
    Your choice: 1

    Number of character: 1

    Your number is: 1
    Your character is: (some character)
    sfExiting due to signal SIGSEGV
    General Protection Fault at eip=00001771
    eax=00000001 ebx=00008.....


    Error #2 (Both):
    when I choose option 2, it runs through the whole function without getting input.


    TIA (antipoints for all who helps solve the prob)
    Preliminary operational tests were inconclusive (the dang thing blew up)

    \"Ask not what the kernel can do for you, ask what you can do for the kernel!\"

  2. #2
    Well, I changed

    void CharToNumb( void )
    {
    char x[1];
    x[0] = NULL;

    to

    void CharToNumb( void )
    {
    char x[1];
    x[0] = 0;

    and it compiled and ran OK, but the program crashes when getting a chracter from a number. I used Bloodshed Dev C++ to run it.

  3. #3
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,209
    Alright... to start off... I compiled fine (like you said). When I enter choice 1 and enter a number, I get the correct character back, so that part's right. I do, however, receive a "sfx" appearing after the output (though, as far as I can see, that was intentional by you for program flow testing).

    Next, if I try to convert a character to an integer (by pressing choice 2) I don't get any chance to input a value. Instead of using an array of characters for the value of x in CharToNumb, I would recommend just using a character (char x) and using scanf to get the input. That should solve that problem.

    I don't exactly know why you did a lot of the things you did (this should have been a very simple program, but your code seems to be a lot more complex then it needs to be). Anyway, play around with my suggestion and see what you can do with it. Also, if you want some advice about fixing up the program to clean up some of the code, let me know and I can help.

    AJ

    EDIT: Actually, if you just change the gets(x); to scanf("%s", &x); the program works perfectly. That resolves the Error #2 you have listed above. As for Error #1, I'm not getting that error at all. I'll keep looking at it though... Note: I compiled and ran this using xlc and gcc on an IBM AIX workstation. I'll test it in some other compilers as well.

  4. #4
    Senior Member
    Join Date
    Oct 2001
    Posts
    385
    avden, I originally used char x; and scanf("%d", x); then I tried scanf("%c", x);, same prob.
    I'm still learning, so I will be pretty inefficient for awhile, though I would like to see how you would fix up the code. You're right, the 'sfx' was to track where the error for option 1 happens.
    Preliminary operational tests were inconclusive (the dang thing blew up)

    \"Ask not what the kernel can do for you, ask what you can do for the kernel!\"

  5. #5
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,209
    Since you're using x as an array, you have to use %s as the scanf option because you need to read in a string. That should solve the problem with the second error. I haven't been able to reproduce your first error, however. I tried with various compilers on my AIX machines as well as Microsoft VS.NET. It may be simply that you're using a number that is too low. Try using 65 as the number entered (you should get an A back). It possible that your compiler built it so that you can't enter some of the reserved ASCII values (I know I used to have problems like that with an old Borland compiler... anything below 32 caused it to crash).

    AJ

  6. #6
    Senior Member
    Join Date
    Oct 2001
    Posts
    385
    hmm...I'm about to shoot this author for that one...claimed scanf was only for numeric values. The problem with the first error is that it exits between f and x, outside of the printing function.
    Preliminary operational tests were inconclusive (the dang thing blew up)

    \"Ask not what the kernel can do for you, ask what you can do for the kernel!\"

  7. #7
    Senior Member
    Join Date
    Aug 2001
    Posts
    356
    ok well this doesnt go to your questions, but i cleaned up your "NumToChar" function(btw, you should name it numToChar and reserve names starting with caps for classes in C++).

    this should give you an idea of the kind of code reduction you can do:
    Code:
    void NumbToChar( void )
    {
    	short         x;
    //	char          y = -127; /* Set y and z to minimum values */
    //	unsigned char z =    0;
    
    	printf("\nNumber of Character: ");
    	scanf("%d", &x);
    
    
    	if (x < -127 || x > 255)
    		printf("Acceptable range is -127 to 255");
    /*	else if (x < 0)
        {
    		for( ; y < x; y++);
    		printf("\nYour number is:    %d", x);
    		printf("\nYour character is: %c", x);
        }*/
    	else
    	{
    //		for( ; z < x; z++);
    		printf("\nYour number is:    %d", x);
    		printf("\nYour character is: %c", x);
    	}
    	printf("\ns");
    }
    -8-

    There are 10 types of people in this world: those who understand binary, and those who dont.

  8. #8
    Senior Member
    Join Date
    Oct 2001
    Posts
    638
    I can spot another problem with your code which is a problem most people encounter when they first start programming in C. You have to understand the way local and global variables are stored on the stack and what happens to the stack during a function call. To illustrate the point. This code is not good.

    Code:
    int GetChoice( void )
    {
        int choice;
    
        printf("\n\nChoose one of the following:");
        printf("\n1 - Get character from number");
        printf("\n2 - Get number from character");
        printf("\n3 - Quit");
        printf("\nYour Choice: ");
    
        scanf("%d", &choice);
    
        return choice;
    }
    What this code does is declare a local variable of type int called choice. This variable exists within the function and so it is pushed to the stack after the function call has been made (ie its a local variable). The function then does some useful work and stores a value in choice and returns this value. To simplify things, when the function returns it pops all its local values from the stack. This means that there is no guarantee that the return value has not changed by the time it is used by the next command after the function call.

    You can get away with this 99.9% of the time because it is vary rare that another program will overwrite the value stored in your local variable in the very short space of time between the function return and next command. And it is even less likely that the variable will be overwritten with a value that cause the program to exit in a fashion that lets you know that you've got problems. You'll probably only notice a problem during the countdown before the shuttle blasts off .

    There are two main ways around this:
    1) allocate memory inside the function with malloc/calloc and return the value.
    2) pass a variable to the function by reference and store the return value in there.

    Good luck
    OpenBSD - The proactively secure operating system.

  9. #9
    Senior Member
    Join Date
    Aug 2001
    Posts
    356
    um, smirc...whats the problem with using return? every class i've taken so far has told me to use return when i only have 1 value/stucture i need to get back to the calling function...
    -8-

    There are 10 types of people in this world: those who understand binary, and those who dont.

  10. #10
    Senior Member
    Join Date
    Oct 2001
    Posts
    638
    um, smirc...whats the problem with using return? every class i've taken so far has told me to use return when i only have 1 value/stucture i need to get back to the calling function...
    There's nothing wrong with using return. It's what you return that counts. You can return constants with no problems. You can return global variables with no problems. The thing is that you can't just return the value of a local variable and rely on the fact that it will keep its value even after the function has returned*. Like I said, you can get away with this 99.9% of the time but it will come back to haunt you eventually.

    * see above post.
    OpenBSD - The proactively secure operating system.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •