You are misusing char arrays. You've got some nasty memory bugs are a result.
Problem #1: Your buffers are not large enough:
1 2
|
char input[1];
char value[1];
| |
If you have an array of 1 character... that is big enough for a string of length
ZERO (because you need 1 for the null terminator). IE, it is impossible for this buffer to hold any data at all without overflowing and causing memory corruption. You need to up this to a bigger size:
1 2
|
char input[100]; // now capable of holding up to 99 characters
char value[100]; // ditto
| |
Problem #2: "%s" with scanf is unsafe unless you use qualifiers to specify a maximum input length.
So we have a string buffer that's capable of holding 99 characters. But what if the user inputs 100 character? Then we have memory corruption. We can make the buffer bigger... but what if the user inputs even more? There's no way we know how much the user is going to input, so there's no way to prevent them from overflowing the input buffer here.
So instead... you need to qualify %s so it doesn't exceed a maximum amount of characters:
|
scanf( "%99s", input ); // don't exceed 99 characters
| |
Problem #3: You can't use the == operator to compare char buffers to string literals.
This code:
if (input == "1")
Is not comparing the contents of your 'input' variable like you think. It's actually comparing the
address of your input buffer to the
address of a fixed string literal in memory. These two addresses will
never be the same, and therefore this if block will never execute.
Instead... when comparing string data, you need to use strcmp:
if( !strcmp(input,"1") )