Home > Software design >  How can I fix the scanf to take data into the array?
How can I fix the scanf to take data into the array?

Time:05-10

Can someone please advise regarding the scanf? "message" is an array of 4 rows by 16 columns. After the user enters the numbers (all 1 digit integers), when hitting "Enter" there is an error message. As said - probably something with the scanf.

for (int i = 0; i < M; i  ) {
        for (int j = 0; j < columns; j  ) {
            scanf("%d", message[i][j]);
        }
}

CodePudding user response:

You're missing a "&" before "message[i][j]".

for (int i = 0; i < M; i  ) {

    for (int j = 0; j < columns; j  ) {

        scanf("%d", &message[i][j]);
    }
}

CodePudding user response:

[Answer copied from a now-deleted former question.]

Since you didn't even know about &, it sounds like no one has taught you how to use scanf properly. It can be surprisingly difficult, and it's chock-full of pitfalls. Here are some handy rules. (For reasons I fail to understand, everybody tells beginners to use scanf, but nobody ever teaches rules like these along with it.)

  1. Do not use scanf and fgets together in the same program.
  2. Do not use scanf and getchar together in the same program.
  3. Remember to always use & in front of the variables you're trying to read. (This is a special rule, for scanf only. Don't try to use that & on the variables you give to printf, for example.)
  4. Exception to rule 3: Do not use the & when you are reading strings with %s.
  5. If you are reading strings using %s, make sure the variable you read into is either a character array that's big enough for what the user is likely to type, or a pointer to malloc'ed memory that's big enough for what the user is likely to type.
  6. Be aware that %s reads strings that don't contain space characters. You can't use %s to read strings (like full names) that might contain spaces. (For now, please don't worry about how you might read a string that might contain spaces. See also rule 14.)
  7. Use only one % sign in the format string, to read one variable. Don't try to read two or more variables in one scanf call.
  8. Always check scanf's return value. If it returns 0, or the negative value EOF, that means it didn't successfully read anything. If it returns 1, that means it successfully read one value. (And if you break rule 7, and try to read multiple values, it'll return the number of values it did successfully read, anywhere between 0 and the number you asked for.)
  9. If scanf returns 0 or EOF, indicating that the user did not type a valid value, just print an error message and exit. Don't try to write code that asks the user to try again, because the user's wrong input is still sitting on the input stream, and there's no good, simple way to get rid of it. (If you really want to write user-friendly code that re-prompts in case of error, scanf is not the right tool for the job.)
  10. Use only the format specifiers %d, %s, %f, and %lf, to read into variables of type int, string (see rule 5), float, and double, respectively.
  11. If you want to read a character into a variable of type char, you can use " %c", but the mysterious extra explicit space character there is vital.
  12. Never put whitespace after the format string. That includes the newline character \n. (That is, use "%d", not "%d " or "%d\n".)
  13. Don't try to use the %[…] specifier.
  14. If you break rule 13 (perhaps because someone told you that %[…] might be a way to read a string containing spaces, or a whole line), do not put an s after it. The format is %[…], not %[…]s.
  15. Don't try to use any of the other format specifiers.

These rules may seem quite restrictive, but if you follow these rules, you should be able to simply and easily and reliably get simple inputs into your simple programs, which is the goal here. scanf is otherwise remarkably hard to use, and experience has shown that there are something like 17 different horribly frustrating problems that invariably come up, and trying to solve them is a completely unnecessary distraction from your goal of learning C by writing simple C programs.

These rules are somewhat numerous. For a simpler set of rules, see this answer. Putting rules 7, 10, 11, and 12 together, there are really only five complete format strings you should ever use with scanf: "%d", "%s", "%f", "%lf", or " %c". (But no commas, no fixed strings, no whitespace other than the explicit space in " %c", nothing else.)

If you need to do something more complicated, that you can't do while staying within these rules, it's time to either:

  1. Learn enough about how scanf works (and doesn't work) so that you can try to do the thing you want, but without getting stuck on one of those 17 problems, or
  2. Learn how to do input using better and more powerful methods than scanf, perhaps by reading whole lines using fgets and then parsing them.
  • Related