data structures

This is a discussion on data structures within the C Programming forums, part of the General Programming Boards category; Hi basically what im trying to do is create a shopping program in C. I will be adding extra features ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Birmingham UK
    Posts
    8

    Question data structures

    Hi

    basically what im trying to do is create a shopping program in C. I will be adding extra features later on but for now all i want it to do is for the user to enter a number 1-3 and depending on the number chosen the program displays the corresponding product and price.

    Im not very good with data structures and im sure im on the wrong track. Could someone give me a few pointers. Thanks

    Heres what ive got so far

    DVD Player 29.99
    MP3 player 39.99
    Walkman 19.99

    Code:
    #include <stdio.h>
    
    typedef struct 
    {
    char name[20];
    float price;
    
    } product;
    
    product p1,p2;
    
    
    struct product p1;
    
    p1.name="DVD Player";
    p1.price=29.99;
    
    product p2;
    
    p2.name="MP3 Player";
    p2.price=39.99;
    
    product p3;
    
    p3.name="Walkman";
    p3.price=19.99;
    
    
    int main(void)
    {
    scanf ("enter a product number %d",product),
    
        if (number == 1)                                    
            printf("%c\t%f",p1.name,p1.price); 
    
        else if (number == 2)                                     
            printf("%c\t%f",p2.name,p2.price); 
    
        else if (number == 3)                                    
            printf("%c\t%f",p3.name,p3.price);        
    
    
    
    	return 0;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Opps, sorry, made a typo; see the below post.
    Last edited by dwks; 05-12-2005 at 05:16 PM. Reason: Typo

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048

    Thumbs up

    Code:
    #include <stdio.h>
    
    int main() {
        int number;
        struct {
            char name[20];
            float price;
        } p1, p2, p3;
    
        strcpy(p1.name, "DVD Player");
        p1.price=29.99;
    
        strcpy(p2.name, "MP3 Player");
        p2.price=39.99;
    
        strcpy(p3.name, "Walkman");
        p3.price=19.99;
    
        printf("\nEnter a product number: ");
        scanf ("%d", &number);
    
        if (number == 1)
            printf("%c\t%f", p1.name, p1.price); 
    
        else if (number == 2)
            printf("%c\t%f", p2.name, p2.price); 
    
        else if (number == 3)
            printf("%c\t%f", p3.name, p3.price);        
    
        return 0;
    }
    Hope this helps.
    Last edited by dwks; 05-12-2005 at 05:20 PM. Reason: Put the code in main().

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Birmingham UK
    Posts
    8

    Smile cheers mate!

    thanks very much for sorting that out.

    Dont think i ever would have got there!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's a good generalized data structures book?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-16-2006, 01:01 PM
  2. i need advice about data structures
    By sawer in forum C Programming
    Replies: 2
    Last Post: 04-22-2006, 04:40 AM
  3. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 05:19 AM
  4. array of structures, data from file
    By nomi in forum C Programming
    Replies: 5
    Last Post: 01-11-2004, 01:42 PM
  5. Array Data Structures
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 06:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21