Thread: Code issue scanf() string line 28

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    6

    Code issue scanf() string line 28

    Hi,

    I'm developing a very small project, and while implementing it I got some issue. I already did this kind of exercises a few times, but I don't seem to understand what is wrong (the scanf to get string/char array input. A compiler error related to the type of variable and or number of arguments.)
    I'm using code blocks and windows10 at this moment.

    This is still a code in progress, and as a newbie I will also appreciate best practice's tips.

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    #define MAXINPUT 20 //use this value to max loop (apply later)
    
    
    struct Book // Structure type definition
    {
     char title [10];
     int pages;
    
    
    }books[10];
    
    
    int main()
    {
        int i = 0;
        int option;
    
    
        while (i < 10)
        {
        printf("Do you want to enter details of BOOK 1 (yes) or 0 (No)? ");
        scanf("%d", &option);
    
            if (option == 1)
            {
                printf("\n Title name: ");
                scanf("%s", &books[i].title);
    
    
                printf("\n Number of pages: ");
                scanf("%d \n", &books[i].pages);
    
    
            }
            else if (option == 0)
            {
                printf("It was not yes \n");
                break;
    
    
            }
            else{
    
    
                printf("Valid input, go back to main menu \n");
                break;
    
    
            }
            i++;
    
    
        }
    
         // save records to file// to be written
    
        //display records created
    
    
        for (i = 0; i < 2; i++) {
    
    
           printf("Book title: ");
           puts(books[i].title);
           printf("Page number:  %d", books[i].pages);
           printf("\n");
        }
    
    
        //display all saved record//to be written
    
    
    
    
    
    
        return 0;
    }
    warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[10]' [-Wformat=]
    Last edited by JazzDev; 01-28-2020 at 04:29 AM. Reason: its no longer line 28

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You mentioned string input, but it looks like you intended integer input.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    6
    Quote Originally Posted by laserlight View Post
    You mentioned string input, but it looks like you intended integer input.
    When I past the code it seems it added extra lines. The issue seems to be here.

    Code:
    printf("\n Title name: ");
                scanf("%s", &books[i].title);
    Also, the code doesn't perform anything else after getting the input.

    EDITED:

    Ok, so after a needed break from being hours looking at this I understood the difference between having & and not having it when getting a string input. I took it out from the example above. But the program now crashes.
    Last edited by JazzDev; 01-28-2020 at 04:35 AM. Reason: code edited

  4. #4
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    I found a few errors.

    Compare to this...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    #define MAXINPUT 20 //use this value to max loop (apply later)
     
    struct Book {
        char title[10];
        int pages;
    };
     
    int main() {
        int i = 0, option = 0, bookCount = 0;
     
        struct Book books[10];
    
        while (i < 10) {
            printf("Do you want to enter details of BOOK 1 (yes) or 0 (No)? ");
            scanf("%d", &option);
    
            if (option == 1) {
                printf("\n Title name: ");
                scanf("%s", books[i].title);
                printf("\n Number of pages: ");
                scanf("%i", &books[i].pages);
                bookCount++;
            } else if (option == 0) {
                printf("It was not yes \n");
                break;
            } else {
                printf("Valid input, go back to main menu \n");
                break;
            };
            i++;
        };
     
         // save records to file// to be written
     
        //display records created
        for (i = 0; i < bookCount; i++) {
           printf("Book title: ");
           puts(books[i].title);
           printf("Page number:  %d", books[i].pages);
           printf("\n");
        };
     
        //display all saved record//to be written
        return 0;
    };
    tried to keep it close to the original.
    Last edited by Structure; 01-28-2020 at 09:39 AM.
    "without goto we would be wtf'd"

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    @structure, the ; on the ends of statement block braces are unnecessary.

    > scanf("%d \n", &books[i].pages);
    @JazzDev, trailing characters on the ends of scanf format strings are seldom ever what you want to do.
    What this will do is prevent scanf from returning until you type in something else.

    Just a regular
    scanf("%d", &books[i].pages);
    will suffice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    the ; on the ends of statement block braces are unnecessary.
    It helps me keep a uniform design. For instance structures require it, it would be confusing because i'm easily confused lol. It is also a habit i have been doing it forever. Looking at code without the semicolon after the brackets looses me faster. I can look at the code and see what is going on easier. It's a preference really. I have a lot of preferences...

    i.e: i would rather
    Code:
    int main() {
      return 0;
    };
    instead of...
    Code:
    int main()
    {
      return 0;
    }
    The second one just looks ridiculous.
    "without goto we would be wtf'd"

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Structure
    For instance structures require it, it would be confusing because i'm easily confused lol.
    That's because they wanted to allow declaring of variables of the struct type in a compact form... which ironically is generally seen as a poor practice these days because such variables would almost certainly be in file scope and hence be global variables. I cannot even think of another case offhand where a semi-colon is required after a closing brace (and of course it isn't even required here: you can still declare a variable of that type, even if it may be poor style). Hence, it looks like you took a special case and made it into a general rule for some notion of uniformity.

    Quote Originally Posted by Structure
    Looking at code without the semicolon after the brackets looses me faster. I can look at the code and see what is going on easier. It's a preference really. I have a lot of preferences...
    In this case your preference goes against common C style, and may even be illegal syntax in other programming languages whose syntax was inspired by C (but tried to clean up C's syntax warts, which is a common pastime for such newer programming languages). I suggest that you change your preference when posting code on this forum. You're free to do what you want with your own code, but it isn't good form to use a quirky style in code meant to be read by others.

    Quote Originally Posted by Structure
    The second one just looks ridiculous.
    Since it looks ridiculous to you, don't use it. But you should be aware that it is also extremely common style in C and a host of other programming languages, so you will have to live with it. The closest common style in C would be:
    Code:
    int main() {
      return 0;
    }
    That is, simply omitting the useless semi-colon. I'm not entirely sure what that semi-colon actually means in the context of file scope: I suspect it may still be a null statement, which is what you're certainly doing when you use it for if statements and loops: littering your code with null statements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by laserlight View Post
    That is, simply omitting the useless semi-colon. I'm not entirely sure what that semi-colon actually means in the context of file scope: I suspect it may still be a null statement, which is what you're certainly doing when you use it for if statements and loops: littering your code with null statements.
    I'm not sure what it means either. But compiling with -pedantic both clang and gcc emit a warning. In the case of gcc: warning: ISO C does not allow extra ‘;’ outside of a function [-Wpedantic]

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by JazzDev
    Ok, so after a needed break from being hours looking at this I understood the difference between having & and not having it when getting a string input.
    Yes, a string will be converted to a pointer to its first char, so you shouldn't be taking its address with the address-of operator.

    However, you should also be aware that using a %s without specifying the field with for scanf makes your code vulnerable to buffer overflow: the user can enter as long a string as they want, and the program will attempt to store it all into the destination array of char, even if there is not enough space to store it. In your case, title is an array of 10 char, so the scanf call should be: scanf("%9s", books[i].title) as one char is reserved for the terminating null character.

    Quote Originally Posted by JazzDev
    But the program now crashes.
    What is your current code and test input?

    Quote Originally Posted by Hodor
    But compiling with -pedantic both clang and gcc emit a warning. In the case of gcc: warning: ISO C does not allow extra ‘;’ outside of a function [-Wpedantic]
    That would be consistent with the semi-colon being a null statement: not being declarations, statements are not permitted at file scope outside a more local scope.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > It helps me keep a uniform design.
    If you ever aspire to be a professional programmer or work in any kind of software team, it will be a source of friction for you.

    People will not accept your ; riddled code into their larger code bases.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    If you ever aspire to be a professional programmer
    I do not. It is a hobby, and i will keep my preferences.

    A semicolon terminates a statement.
    If your statement doesn't do anything, that's your prerogative as a programmer.
    Last edited by Structure; 01-29-2020 at 08:56 AM.
    "without goto we would be wtf'd"

  12. #12
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Quote Originally Posted by Structure View Post
    I do not. It is a hobby, and i will keep my preferences.
    Then don't try to "help" others if you don't care about proper style.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  13. #13
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    proper style
    not a thing.
    "without goto we would be wtf'd"

  14. #14
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by Structure View Post
    not a thing.
    It is a thing actually, without it things like linux would not have been born because the code would be chaos and noone could understand each others code, I may be a newb to coding styles but I at least get the point of a common style

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf issue
    By cooper1200 in forum C Programming
    Replies: 16
    Last Post: 04-16-2019, 04:18 AM
  2. Multiple printf/scanf code issue
    By kumczak in forum C Programming
    Replies: 2
    Last Post: 04-08-2014, 08:33 AM
  3. N00b @ C: Code is failing at line 12 which is a basic scanf
    By Mike Garber in forum C Programming
    Replies: 3
    Last Post: 09-01-2013, 10:25 AM
  4. 'cin' and 'scanf()' issue
    By Brain Cell in forum C++ Programming
    Replies: 4
    Last Post: 11-07-2004, 10:53 AM
  5. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM

Tags for this Thread