0

I'm setting up a program in C that manipulates strings where a user enters the number of transitions and cells, along with a string they wanted to manipulate and a starting a string and an "initial string"

The user will input the information line by line, so I decided to use "scanf", however, all my variables seem to work except for the integer of transitions, in which it's constantly being assigned 0 despite the user input.

int main()
{
    int num;
    int changes;
    char string[10];
    char startingString[25];

    scanf("%d", &num);
    scanf("%d", &changes);
    scanf("%s", string);
    scanf("%s", startingString);

    printf("number: %d\n",num);
    printf("Changes: %d\n",changes);
    printf("String: %s\n",string);
    printf("String Before: %s\n", startingString);
}
input:
20
10
teststring
goodbye

output:
20
0 <----
hello
goodbye
Daniel Larson
  • 49
  • 1
  • 9
  • 2
    Would help if you checked the return value of `scanf`. Should be 1 in all your cases. – Bathsheba Oct 09 '19 at 16:57
  • 1
    Shouldn't the newline character be specified and consumed? People lean on `scanf` way too heavily. Reading a raw buffer and parsing, even with `strtok`, can be a better approach. – tadman Oct 09 '19 at 16:59
  • 2
    @tadman scanf skips whitespace, so newline shouldn't be the problem – smac89 Oct 09 '19 at 17:00
  • If this is `C`, you need to `#include` the appropriate headers. The behavior of a program can change if the headers are not included. – PaulMcKenzie Oct 09 '19 at 17:02
  • does it still happen if you add a `return 0` to the end of `main`? – MooseBoys Oct 09 '19 at 17:12
  • @MooseBoys that is not necessary. In any case, the results have already been output. – Weather Vane Oct 09 '19 at 17:13
  • @hydra-bslash-0 those inputs do not cause the behaviour you stated. What were the *actual* inputs? – Weather Vane Oct 09 '19 at 17:14
  • To be more exact, you should be including ``. Otherwise I do not know how the program will behave, since the prototype for `scanf` and `printf` will be missing. – PaulMcKenzie Oct 09 '19 at 17:16
  • @WeatherVane depending on the version of [tag:c], it may not be optional. – MooseBoys Oct 09 '19 at 17:17
  • @MooseBoys `int main()` is the one function that defaults to `return 0;` if no return statement is provided. Please see [this](https://stackoverflow.com/questions/18402853/must-the-int-main-function-return-a-value-in-all-compilers) since C99. – Weather Vane Oct 09 '19 at 17:22
  • @WeatherVane yes, *"since C99"* e.g. not linux (C89) – MooseBoys Oct 09 '19 at 17:51
  • @MooseBoys if it was missing, the compiler would have reported it. And Linux is not C, it is an OS. The issue is certianly not relevant to this question. – Weather Vane Oct 09 '19 at 17:55
  • Umm... some of us do not believe that you used 'Hello' and 'Goodbye' as test strings:( – Martin James Oct 09 '19 at 19:44
  • Initialize all the variables; e.g,. `int num = 999; int changes = 999;`. Print the return values of `scanf` calls: `printf("%d\n", scanf("%d", &num))`. We want to know whether `scanf` put a zero into `changes` or whether it it did nothing to `changes`, and the zero is just the uninitialized value. – Kaz Oct 09 '19 at 22:54
  • This question is very confusing. Instead of editting it with the solution, edit it to include the proper test case. You did great by adding a small reproducible example, but you forgot to make sure the test cases would trigger the error you were seeing. – giusti Oct 10 '19 at 00:54

2 Answers2

1

taking two consecutive scanf() is the problem here. the variable is overridden once it sees

0

I can not reproduce the behavior of the program for the input data you showed.

However such a call of scanf

scanf("%s", string);

is unsafe.

So it is possible that when you are entering the variable string you are overwritting the variable changes.

Using such a call of scanf

scanf("%s", string);

you have to enter less than 10 characters (or more generally less than sizeof( string )), because the function appends the terminating zero to the entered sequence of characters stored in the argument.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • This is the answer. I completely forgot to take into account the terminating character. Thank you. – Daniel Larson Oct 09 '19 at 17:13
  • 1
    @hydra-bslash-0 -- The advice given is correct, but I still don't see how you could have gotten an error given the input you stated in your question. Something isn't adding up... – PaulMcKenzie Oct 09 '19 at 17:20
  • 1
    @hydra-bslash-0 But your example shows "hello" and "goodbye" as input, which are both short enough that the terminating character shouldn't be a problem. – Fabio says Reinstate Monica Oct 09 '19 at 17:20
  • 1
    Yeah...I immediately suspected buffer overflow, but was put off by the undersize strings that were, supposedly, used in testing:( – Martin James Oct 09 '19 at 19:42