Thread: need help please due in 3 hourse

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    need help please due in 3 hourse

    hi i want to know why is this not printf out the value

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    struct computer_part
    {    
        char name;
        int quantity;
        int price;     
           
    };
    main()
    {
         struct computer_part cp[5];
         int i;
         
         printf("enter price");
         scanf("%i",&cp[i].price);
          
          printf("%i",cp[i].price);
          getch();
    
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    int i;
         
         printf("enter price");
         scanf("%i",&cp[i].price);
    Any guesses what the value of i is?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    the value of i is the couting for array

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    the value of i is the couting for array
    That might be true if you had a loop and counting going on...I do not see either
    My Website

    "Circular logic is good because it is."

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    No - literally - what is the value of i? If I printf'd it, what would I see?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Nobody knows (quite literally) what the value of i is.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    struct computer_part
    {    
        char name;
        int quantity;
        int price;     
           
    };
    int main()
    {
        struct computer_part cp[5];
        int i;
    
        for(i = 0; i < 5; i++) {     
          printf("enter price");
          scanf("%d",&cp[i].price);
          
          printf("\n %d",cp[i].price);
        }
        getch();
        return 0;
    }

    I wouldn't use i without it first being given a value, and it's just confusing using %i as the format specifier, and i as the index to your cp array, right on the same line of code.

    Maybe the above is what you wanted?

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by Adak View Post
    I wouldn't use i without it first being given a value,
    Right. In C, local variables are not initialised when you define ("int i") them. You need to assign it a value. Adak's example is probably what you were after.

    and it's just confusing using %i as the format specifier, and i as the index to your cp array, right on the same line of code.
    Glad I'm not the only one who didn't like that :-)

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Adak View Post
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    struct computer_part
    {    
        char name;
        int quantity;
        int price;     
           
    };
    int main()
    {
        struct computer_part cp[5];
        int i;
    
        for(i = 0; i < 5; i++) {     
          printf("enter price");
          scanf("%d",&cp[i].price);
          
          printf("\n %d",cp[i].price);
        }
        getch();
        return 0;
    }

    I wouldn't use i without it first being given a value, and it's just confusing using %i as the format specifier, and i as the index to your cp array, right on the same line of code.

    Maybe the above is what you wanted?

    Damnit Adak, you ruined the whole learning experience with your genius brilliance. lol


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A little homework help?
    By nwkegan in forum C++ Programming
    Replies: 11
    Last Post: 03-07-2010, 09:45 AM
  2. c++ Payroll Due Today!!!!
    By jbuhrman in forum C++ Programming
    Replies: 2
    Last Post: 02-21-2010, 11:13 AM
  3. This may be due to a corruption of the heap
    By krishnampkkm in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2009, 03:19 AM
  4. Replies: 2
    Last Post: 01-31-2008, 09:52 AM
  5. Help Someone! First program/errors and its due tonight!
    By LittleLotte in forum C Programming
    Replies: 4
    Last Post: 02-02-2005, 09:21 AM