Why my code crashes

This is a discussion on Why my code crashes within the C Programming forums, part of the General Programming Boards category; Hi, I need the user to enter coordinates x,y . The x needs to go in front of the number ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    104

    Why my code crashes

    Hi, I need the user to enter coordinates x,y . The x needs to go in front of the number like so : x8.2 . My code works , but it accepts x8.2 just the same as 8.2x. This is not allowed to happen ie . it must reject the point if it sees the x at the end . Here is what I came up with :

    Code:
    #include <stdio.h>
    int main(void)
    
    {
    
    FILE * output = stdout;
    FILE * input = stdin;
    
    char stringValue[8];
    float value;
    fscanf(input, "%s", stringValue); 
    
    // check last character
    if (stringValue[ strlen( stringValue ) - 1] == 'x')
    
    {
    	fprintf(output,"You have entered incorrect value - put the x at the front !");// wrong value
    	return 0;
    }
    
    else
    
    {
    	fscanf(input, "%f", value);
    	fprintf(output,"The program will continue");
    }
    
    }
    I run that , it works for any number with x at the end , it says incorrect put at x at front , but if a correct number is putin, it will crash ? If Im not doing this correctly can you show me the right way ?
    Thank you

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Im an idiot it crashes because I forgot the &value , instead of just value .

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Maybe someone can help me with another problem when I try to compare strings using strcmp() I get a segmentation fault . Here is an exampl of what I mean :

    Code:
    #include <stdio.h>
    
    #include <string.h>
    
    
    int main() 
    
    {
    char *name = "EDDIE";
    char *guess;
    int correct = 0;
    
    printf("Enter a name in uppercase: ");
    
    while(!correct)
    
    {
    	fgets(guess ,10, stdin);
    
    	if(strcmp(name, guess)==0)
    {
    	printf("Correct!\n");
                    correct = 1;
    }	
    
        
    else 
    
    {
    	printf("Try again: ");
    }
    
    
    }
    
    return 0;
    
    }
    I appreciate your help
    Last edited by ICool; 09-08-2007 at 03:44 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    guess needs to be a sufficiently big array, not a pointer. You're writing into memory that you don't own.

    Edit: Also,
    1) Way too much white space.
    2) Please fix your indentation.
    3) You don't need the variable correct. Just use an infinite loop and use "break;" to get out when done.
    Last edited by robatino; 09-08-2007 at 03:41 AM.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Quote Originally Posted by robatino View Post
    guess needs to be a sufficiently big array, not a pointer. You're writing into memory that you don't own.

    Edit: Also,
    1) Way too much white space.
    2) Please fix your indentation.
    3) You don't need the variable correct. Just use an infinite loop and use "break;" to get out when done.

    Thanks , just to clarify do you mean I should use char guess[#] ? Sorry if my questions sound dumb but I am just a beginner . I don't know much about indenting , but hopefully it now looks better. Thanks
    /Edit just did a bit of research maybe something like :
    int guess[] = {"EDDIE"};
    Last edited by ICool; 09-08-2007 at 03:51 AM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Since you're telling fgets() to read at most 10 characters (including the final null character), you need to allocate at least that many, so you should use
    Code:
    char guess[10];

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Do you mean I should use char guess[#] ?
    Yes. The way you have guess declared:
    Code:
    char *guess;
    guess is a pointer to char. In order to use a pointer, first you must allocate some memory for it using a function such as malloc(). Or simply declare guess as an array of char, as you show above.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Ok I am now using
    Code:
    char guess[10]
    However no matter what I put in it says Try again . This includes if i enter EDDIE and "EDDIE". Any idea why it's doing this ? ( Ichanged the code posted before to show what I did )

    I have also tried changing the code to this :
    Code:
    int name[] = {"EDDIE"};
    char guess[10];
    int correct = 0;
    But I highly doubt that the second method can work .
    Thanks for your time

  9. #9
    ZuK
    ZuK is online now
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by ICool View Post
    However no matter what I put in it says Try again . This includes if i enter EDDIE and "EDDIE". Any idea why it's doing this ?
    fgets leaves the terminating '\n' at the end of the returned string.
    one way to get rid of that is
    Code:
            char *p;
            p = strchr(guess, '\n');
            if ( p ) 
                *p = 0;
    Kurt

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    As said before, it is the '\n' char which is playing your problem. Remove the \n char and replace that with the '\0' char. Beforem you compare the two string.

    That should work. And work along your code indendation.

    ssharish2005

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Quote Originally Posted by ZuK View Post
    fgets leaves the terminating '\n' at the end of the returned string.
    one way to get rid of that is
    Code:
            char *p;
            p = strchr(guess, '\n');
            if ( p ) 
                *p = 0;
    Kurt
    Can you please clarify what you mean . How would I implement the above code . This compiles but crashes :
    Code:
    printf("Enter a name in uppercase: ");
    char *p;
    p = strchr(guess, '\n');
    if ( p ) 
    *p = 0;
    
    while(!correct)

  12. #12
    ZuK
    ZuK is online now
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()  {
        char *name = "EDDIE";
        char guess[10];
        int correct = 0;
        char * p;
    
        printf("Enter a name in uppercase: ");
    
        while(!correct) {
            fgets(guess ,10, stdin);
            p = strchr(guess, '\n');
            if ( p ) 
                *p = 0;
                
    	if(strcmp(name, guess)==0) {
    	    printf("Correct!\n");
                correct = 1;
            }	  
            else  {
                printf("Try again: ");
            }
        }
    }
    kurt

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Thank you so much . That works perfectly

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Is this how I would do the same but for more than 1 name ?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    int name[] = {"EDDIE","BEN","DAVE"};
    
    char guess[10];
    int correct = 0;
    char * p;
    
    printf("Enter a name in uppercase: ");
    
    while(!correct) {
    fgets(guess ,10, stdin);
    p = strchr(guess, '\n');
    if ( p ) 
    *p = 0;
    
    if(strcmp(name, guess)==0) {
    printf("Correct!\n");
    correct = 1;
    } 
    else {
    printf("Try again: ");
    }
    }
    }

  15. #15
    ZuK
    ZuK is online now
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    does it compile ?

    Kurt

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. When your code crashes
    By caroundw5h in forum C Programming
    Replies: 9
    Last Post: 06-19-2004, 10:40 PM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. code crashes on Solaris
    By watcher in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 07:58 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21