Thread: Problem in my code

  1. #1
    Registered User Sietor's Avatar
    Join Date
    Aug 2016
    Location
    Sweden
    Posts
    13

    Problem in my code

    Delete
    Last edited by Sietor; 08-15-2016 at 05:22 PM. Reason: Problem Fix

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    709
    Don't delete the problem code. Please provide the fix so others can learn from you. Don't be embarrassed by "dumb" mistakes. We all make them! (More than any of us will admit!) ;^)

  3. #3
    Registered User Sietor's Avatar
    Join Date
    Aug 2016
    Location
    Sweden
    Posts
    13
    This is the original code:
    Code:
            if(LocationA == 0 && LocationB == 2){
                printf("You are on a road\n");
                if(Weapon1 = 0){
    
                printf("You found the sword of Reese (15dmg)\n");
    
                scanf(" %s", &Move);
    
                if(Move == 'P'){
    
                    Sword = 15;
                    (Weapon1 = 1);
                    continue;
                }
    
            }else
            continue;
            }
    I was trying to make the user pick up the Sword of Reese.

    The Fix of the code was a simple =
    in the
    Code:
    if(Weapon1 = 0)
    Solution

    Code:
    if(Weapon1 == 0)
    Last edited by Sietor; 08-15-2016 at 06:39 PM.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    If you are only going to read in a single character, use
    %c instead of %s. It's generally bad practice to read character
    strings using scanf as there is no safe buffer overloads.

    Use fgets() to read the character string and then you can further
    format it using sscanf().

    Sample:

    Code:
    char str[BUFFER];
    
    fgets(str, sizeof(str), stdin);
    Last edited by swgh; 08-16-2016 at 01:08 AM. Reason: Missed code edit
    Double Helix STL

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,499
    It's generally bad practice to read character
    strings using scanf as there is no safe buffer overloads.
    What? The scanf() function can be safely used with C-strings as long as you use the correct width specifier to limit the number of characters retrieved.

    Jim

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    Quote Originally Posted by jimblumberg View Post
    What? The scanf() function can be safely used with C-strings as long as you use the correct width specifier to limit the number of characters retrieved.

    Jim
    I apologize, I worded it wrong, I did not mean to say bad practice I was
    meant to say can be unsafe. Sometimes I type so fast what I think comes
    out before I read it

    Sorry Jim.
    Double Helix STL

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,499
    Why is it unsafe if you use the proper width modifier? And don't forget that sscanf() is not any safer if you fail to use the proper width modifier.

    I will agree that without the width modifier scan() is as dangerous as the gets() function, however it can be used safely unlike gets().


    Jim

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    I think the point I am/was trying to make is that lots of people
    who are new to the language perhaps do not know about the
    width modifier as you mentioned.

    Lots of books and tutorials do not touch on this. When I was
    learning (still am) C, I was under the impression that fgets was
    used for strings because it automatically allocates the size of the
    maximum buffer to fit the string (sizeof) which is declared within
    the character array to hold the said string.

    I do understand your point of view Jim, and you are 100% correct.
    It's really a matter of preference in the end. Scanf can be a very safe
    function if used correctly and the programmer understands about buffer
    overloads etc.

    Any function, in any language, can be unsafe, unless they are designed
    flawlessly.
    Double Helix STL

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,499
    that fgets was
    used for strings because it automatically allocates the size of the
    maximum buffer to fit the string (sizeof) which is declared within
    the character array to hold the said string.
    No fgets() does not "allocate" anything. The array of char used in the first argument must already have the memory allocated. This memory must be at least large enough to hold the number of characters indicated by the second argument. If the size of the array is smaller than the value of the second parameter fgets() can overflow the bounds of the array.

    Jim

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    Well my 1994 text book is going in the bin then...

    Thank you Jim. Probably the most concise explained for my
    poorly taught knowledge on fgets. It's a function I use lots too,
    so at least now I can update my understanding to what the
    function is doing under the hood.

    Kudos.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem or compiler problem....?
    By miloki in forum C Programming
    Replies: 4
    Last Post: 03-05-2015, 12:48 AM
  2. Problem with my code
    By eduarda2 in forum C Programming
    Replies: 7
    Last Post: 05-24-2014, 02:57 PM
  3. problem with my C code
    By datainjector in forum C Programming
    Replies: 2
    Last Post: 10-04-2009, 07:32 PM
  4. please help me what problem in this code
    By cresol in forum C# Programming
    Replies: 2
    Last Post: 04-30-2008, 02:48 AM
  5. Problem code
    By face_master in forum Windows Programming
    Replies: 9
    Last Post: 09-28-2002, 04:10 AM

Tags for this Thread