Delete
Delete
Last edited by Sietor; 08-15-2016 at 05:22 PM. Reason: Problem Fix
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!) ;^)
This is the original code:
I was trying to make the user pick up the Sword of Reese.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; }
The Fix of the code was a simple =
in the
SolutionCode:if(Weapon1 = 0)
Code:if(Weapon1 == 0)
Last edited by Sietor; 08-15-2016 at 06:39 PM.
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
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.It's generally bad practice to read character
strings using scanf as there is no safe buffer overloads.
Jim
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
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
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.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.
Jim
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