Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,218 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,161 people online right now. Registration is fast and FREE... Join Now!




Typedef struct

 
Reply to this topicStart new topic

Typedef struct, Need help using Typedef struct's

ShotokanDeity
20 Nov, 2007 - 07:54 PM
Post #1

D.I.C Head
Group Icon

Joined: 13 Sep, 2007
Posts: 79


My Contributions
Hey everyone,

I am having trouble actually using my typedef struct. The point of the program is basic inventory reporting. I have my program set up to make heavy use of functions (as a side note - is this good programming practice?). Anyway, I have the typedef declared along with all the prototype functions and global variables. I can create the program to either have the user give all the information for each individual item, or I can define the items on my own and just print a list of the items so the user knows what he has.

So here is my question: how do I declare my initial inventory items? I have Googled this and checked the forums here for awhile now, but I cannot find what I need (perhaps I'm just blind...or stupid.gif ). Any help, tutorials, code examples, or nudges in the right direction are greatly appreciated!

The code below is my (obviously) unfinished work thus far.

CODE

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

/* prototype functions */
void header(void);
void bye(void);
void menu(void);
void itemsold(void);
void dailyreport(void);
void weeklycheck(void);
void monthlyupdate(void);

typedef struct INVEN
    {
        char name[13];            /* name of item */
        int ID;                    /* ID number of item */
        int curr_month;            /* current month */
        int actual_stock;        /* number of items in-stock */
        int wanted_stock;        /* number of items wanted to be in-stock */
        double cost;            /* cost of the item */
        double price;            /* price of the item */
        char supplier[7];        /* name of the supplier */
        int month_volume;        /* monthly volume */
        int total_month_sales;    /* total sales for current month */
    } inven;

/*  global variables */
int choice = 5;    /* for the menu */
char a[50];        /* to catch non-integer input during the menu */

int main(void)
{

    header();
    menu();


    bye();
    return 0;
}

void header(void)
{
    printf("This program is a creation of ShotoBomb.\n");
    printf("       a.k.a. Kevin VanUs\n\n\n");
}

void menu(void)
{
    printf ("0: Exit\n");
    printf ("1: Item Sold\n");
    printf ("2: Daily Report\n");
    printf ("3: Weekly Check\n");
    printf ("4: Monthly Update\n\n");
    printf ("Please enter the number of the menu item you desire: ");
    scanf ("%d", &choice);
    gets(a);
    if (choice == 5)    /* If any non-integer is input, */
    {                    /* then 'choice' is set to make */
        choice;            /* the SWITCH go to 'default'.  */
    }                    /* ---------------------------- */
    switch (choice)
    {
        case 0:    puts("\nThank you for using Inventory Manager by ShotoBomb.\n\n\n");
                break;
        case 1: puts("\nitemsold()\n");
                itemsold();
                break;
        case 2: puts("\ndailyreport()\n");
                dailyreport();
                break;
        case 3: puts("\nweeklycheck()\n");
                weeklycheck();
                break;
        case 4: puts("\nmonthlyupdate()\n");
                monthlyupdate();
                break;
        default: puts("\nPlease enter a menu choice 0 - 4.\n\n");
                 menu();
                 break;
    }
}

void itemsold(void)
{
    //blah blah blah
    menu();
}

void dailyreport(void)
{
    //blah blah blah
    menu();
}

void weeklycheck(void)
{
    //blah blah blah
    menu();
}

void monthlyupdate(void)
{
    //blah blah blah
    menu();
}

void bye(void)
{
    puts("Normal Termination!\n");
    printf("Press 'ENTER' to quit.\n");
    fflush(stdout);
    (void)getchar();
}


edit: minor grammatical corrections

This post has been edited by ShotokanDeity: 20 Nov, 2007 - 07:57 PM
User is offlineProfile CardPM
+Quote Post

harshakirans
RE: Typedef Struct
20 Nov, 2007 - 10:16 PM
Post #2

D.I.C Head
Group Icon

Joined: 26 Apr, 2006
Posts: 122



Thanked: 3 times
Dream Kudos: 150
My Contributions
Hi ShotokanDeity,

Typedef is keyword used to alias a user defined datatype ( typedef int harsha), such that next time he can instantiate variables with the new aliased names( harsha a=10;).

I guess that clears your doubt.

In your code typedef struct ...... does not serve the purpose you want it to.



you have to define the struct before and then alias it usinf typedef..
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Typedef Struct
20 Nov, 2007 - 11:28 PM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 4,032



Thanked: 38 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
You can initialize a structure variable like this:
CODE
INVEN i1 = { "Name", 123, 1, 2, 3, 4.50, 1.50, "Supplier", 50, 250 };


User is offlineProfile CardPM
+Quote Post

Bench
RE: Typedef Struct
21 Nov, 2007 - 02:45 AM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(born2c0de @ 21 Nov, 2007 - 07:28 AM) *

You can initialize a structure variable like this:
CODE
INVEN i1 = { "Name", 123, 1, 2, 3, 4.50, 1.50, "Supplier", 50, 250 };


That would work in C++, but I assume he's using 'C', so that would need to change to either

struct INVEN i1 = { "Name", 123, 1, 2, 3, 4.50, 1.50, "Supplier", 50, 250 };

because 'C' needs to know that INVEN is a struct type. or, he can use the typedef, which is the lowercase 'inven'.

inven i1 = { "Name", 123, 1, 2, 3, 4.50, 1.50, "Supplier", 50, 250 };
User is offlineProfile CardPM
+Quote Post

ShotokanDeity
RE: Typedef Struct
21 Nov, 2007 - 07:53 AM
Post #5

D.I.C Head
Group Icon

Joined: 13 Sep, 2007
Posts: 79


My Contributions
Thanks for the replies! Thats exactly what I was looking for! I kept using the regular "()"s instead of the curly brackets. Thanks for clearing this up!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 05:36AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month