Thread: struct prob

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    55
    Rep Power
    12

    struct prob


    I can't seem to get this to work:

    #include <stdio.h>
    #include <stdlib.h>

    struct stock_type{
    char item_name[30];
    }apple;

    int main(void)
    {
    printf("Enter item name");
    gets(stock_type apple);
    printf("%c",stock_type apple);
    return(0);
    system ("pause");
    }

    gives following:

    C:\Program Files\c++\Dev-Cpp\ben-prog\array.cpp
    parse error before `)'

    C:\Program Files\c++\Dev-Cpp\ben-prog\array.cpp
    [Warning] In function `int main()':

    I think it's something to do with the way I written the gets?
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Melbourne Australia
    Posts
    5
    Rep Power
    0
    I think that the problem is that gets wants to write to a string but instead it is given a struct. I think if you put.

    gets((stock_type apple).item_name)

    or somthing to similar effect it might work.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222

    Re: struct prob


    Originally posted by leftfield
    I can't seem to get this to work:

    #include <stdio.h>
    #include <stdlib.h>

    struct stock_type{
    char item_name[30];
    }apple;

    int main(void)
    {
    printf("Enter item name");
    gets(stock_type apple);
    printf("%c",stock_type apple);
    return(0);
    system ("pause");
    }

    gives following:

    C:\Program Files\c++\Dev-Cpp\ben-prog\array.cpp
    parse error before `)'

    C:\Program Files\c++\Dev-Cpp\ben-prog\array.cpp
    [Warning] In function `int main()':

    I think it's something to do with the way I written the gets?
    Several problems.

    First, after you have declared a variable, you no longer need to include its data type when you use it. In fact, it is a syntax error to do so.

    Second, you are wanting to use the field within apple, so you need to reference it instead of the struct it is in.

    Third, when you do a return in main, then you have exited the program. The system call at the end is unreachable code that will never be executed.

    Fourth, you are wanting to print out the name of the product as a string, not as a single character. Use %s instead of %c .

    Fifth, you would probably want to add some new-lines, AKA "carriage-return-line-feeds". Insert a '\n' in the appropriate places in the format string of the printf.

    Sixth, use code tags. Start a quote reply to this post to see how I used code tags on the following corrected version of your listing to retain indentation and keep the code listing readable:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct stock_type
    {
        char item_name[30];
    } apple;
    
    int main(void)
    {
        printf("Enter item name: ");
        gets(apple.item_name);
        printf("Item Name is %s\n",apple.item_name);
        system ("pause");
        return(0);
    }
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    14
    Rep Power
    0
    from gets(stock_type apple);
    how u are accessing item_name from apple structure
    stock_type apple; wrong
    apple.item_name; item_name is access from this statment

    from printf("%c", stock_type apple);
    u are using wrong format specifier, %c only for single character, %s is used for string.
    here also stock_type apple is wrong
    use this statement apple.item_name;
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    55
    Rep Power
    12
    Ahhh.... thankyou!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    55
    Rep Power
    12
    Oh - just one prob, it's not stopping at gets to let me input a name ie

    "Enter item nameItem name is"

    It just goes straight on. How do u let me input a name?
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    Originally posted by leftfield
    Oh - just one prob, it's not stopping at gets to let me input a name ie

    "Enter item nameItem name is"

    It just goes straight on. How do u let me input a name?
    It works for me. I'm running the code that I posted earlier. What does that part of your code look like?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    55
    Rep Power
    12
    Um.. I don't think Bloodshed Dev likes gets very much, but I've managed to do it with scanf.
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    Originally posted by leftfield
    Um.. I don't think Bloodshed Dev likes gets very much, but I've managed to do it with scanf.
    I did use the MinGW gcc port that comes with Bloodshed's Dev-C++ and it works just fine for me.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    138
    Rep Power
    12
    I would suggest u clear the buffer. I dont remember the syntax off hand. I think the problem I have noticed when I was using Turbo C was the buffer had to be cleared each time before and character was being read. Hope this helps

IMN logo majestic logo threadwatch logo seochat tools logo