Thread: cannot enter anything else than name!! help with the structures!

  1. #1
    Registered User
    Join Date
    Aug 2013
    Location
    Kota,Rajashthan,INDIA
    Posts
    7

    Exclamation cannot enter anything else than name!! help with the structures!

    insert
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    struct prod
        {
         char name[25];
         float price,total;
         int qty;
         }p[11];
    
    void main ()
        {
        int i;
        clrscr();
        printf("\n\t\tNAME        QUANTITY        PRICE");
        for(i=1;i<=10;i++)
        {
        printf("\n\t%d",i);
        scanf("%s%d%f",&p[i].name,&p[i].qty,&p[i].price);
        }
        p[i].total=p[i].price*p[i].qty;
        printf("\n\t\t%s        %d        %f        %f",p[i].name,p[i].qty,p[i].price,p[i].total);
    
        getch();
        }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The identifier of an array already degrades to a pointer to the first element when passed to a function, so you don't need the amperstand (the '&' symbol) for "name" in the scanf call. Also, using scanf in such a way can lead to potential overflows - you can limit the amount of characters to read like so:

    Code:
    scanf("%24s%d%f",p[i].name,&p[i].qty,&p[i].price);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    After removing the non-portable aspects, and indenting the code (learn to indent, it will save you more times than you can imagine).
    Code:
    #include<stdio.h>
    
    struct prod {
      char name[25];
      float price, total;
      int qty;
    } p[11];
    
    int main()
    {
      int i;
      printf("\n\t\tNAME        QUANTITY        PRICE");
      for (i = 1; i <= 10; i++) {
        printf("\n\t%d", i);
        scanf("%s%d%f", &p[i].name, &p[i].qty, &p[i].price);
      }
      p[i].total = p[i].price * p[i].qty;
      printf("\n\t\t%s        %d        %f        %f", p[i].name, p[i].qty, p[i].price, p[i].total);
    
    }
    Do you see now that part of your code isn't part of your for loop?
    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.

  4. #4
    Registered User
    Join Date
    Aug 2013
    Location
    Kota,Rajashthan,INDIA
    Posts
    7

    Help needed!

    Quote Originally Posted by Sebastiani View Post
    The identifier of an array already degrades to a pointer to the first element when passed to a function, so you don't need the amperstand (the '&' symbol) for "name" in the scanf call. Also, using scanf in such a way can lead to potential overflows - you can limit the amount of characters to read like so:

    Code:
    scanf("%24s%d%f",p[i].name,&p[i].qty,&p[i].price);

    this didn't help much!
    still,i cannot enter quantity & price.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Lits Tiwari View Post
    still,i cannot enter quantity & price.
    well, I do

    Code:
    NAME        QUANTITY        PRICE
    abcdefg 12 3.5
         abcdefg       12     3.50    42.00
    Press any key to continue . . .
    With this code:
    Code:
    #include<stdio.h>
    
    struct prod
    {
        char name[25];
        float price,total;
        int qty;
    };
    
    int main ()
    {
        struct prod p;
        printf("NAME        QUANTITY        PRICE\n");
        scanf("%24s%d%f",p.name,&p.qty,&p.price);
        p.total=p.price*p.qty;
        printf("%12s %8d %8.2f %8.2f\n",p.name,p.qty,p.price,p.total);
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Aug 2013
    Location
    Kota,Rajashthan,INDIA
    Posts
    7
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    struct prod
        {
         char name[25];
         int price;
         int total;
         int qty;
         }p[11];
    
    void main ()
        {
        int i;
        clrscr();
    
        for(i=1;i<=3;i++)
        {
        printf("\n\t%d).",i);
        printf(" NAME OF THE ITEM : ");
        scanf("%s",&p[i].name);
        printf("\nQUANTITY : ");
        scanf("%d",&p[i].qty);
        printf("\nPRICE : ");
        scanf("%d",&p[i].price);
    
        }
        p[i].total=p[i].price*p[i].qty;
        printf("\n\t\tNAME        QUANTITY        PRICE        TOTAL");
        for(i=1;i<=3;i++)
        printf("\n\t\t%s        %d        %d       %d",p[i].name,p[i].qty,p[i].price,p[i].total);
    
        getch();
        }
    now i am unable to calculate total!. any help?

  7. #7
    Registered User
    Join Date
    Aug 2013
    Location
    Kota,Rajashthan,INDIA
    Posts
    7
    oh.. i got my mistake
    i was calculating TOTAL out of the loop!
    Silly me!.
    Code:
       printf("\n\t\tNAME        QUANTITY        PRICE        TOTAL");
    
        for(i=1;i<=3;i++){
           p[i].total=p[i].price*p[i].qty;
    
        printf("\n\t\t%s        %d        %d       %d",p[i].name,p[i].qty,p[i].price,p[i].total);
          }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > i was calculating TOTAL out of the loop!
    Interesting, did you read this from 3 days ago?
    Quote Originally Posted by me
    Do you see now that part of your code isn't part of your for loop?
    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.

  9. #9
    Registered User
    Join Date
    Aug 2013
    Location
    Kota,Rajashthan,INDIA
    Posts
    7
    i did!. You were making the mistake as well!

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Lits Tiwari View Post
    i did!. You were making the mistake as well!
    You failed to understand. Salem didn't make the mistake. He highlighted it. There is a significant difference.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Aug 2013
    Location
    Kota,Rajashthan,INDIA
    Posts
    7
    Quote Originally Posted by grumpy View Post
    You failed to understand. Salem didn't make the mistake. He highlighted it. There is a significant difference.
    okay!. Sorry Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  2. Replies: 9
    Last Post: 02-22-2009, 11:50 PM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Accessing structures contained in structures
    By enigmaatj in forum C Programming
    Replies: 1
    Last Post: 04-18-2002, 08:53 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread