Thread: Displaying Structs???

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    11

    Displaying Structs???

    I am having a hard time printing out a struct.. I get an error in my function that I cannot perform this operation. The main idea is to make a program that takes in a struct and displays a menu with options, gets a user choice and returns it to main. Thing is I cant get the menu to display when I pass the struct to the function. Can anyone see how I could display the data from in the struct 'menu' ?

    Code:
    #include <iostream>
    #include <iomanip>
    
    const long MAX = 100;
    
    struct stMenuItem
    {
    	int index;
    	char desc[MAX];
    };
    
    struct stMenu
    {
    	char caption[MAX];
    	stMenuItem menu[MAX];
    	int elements;
    };
    
    int show(struct stMenu[MAX]);
    
    using namespace std;
    
    int main ()
    {	
    	int i = 1;
    	
    	stMenuItem main[MAX] = {
                    {1,"Load Database"},
                    {2,"Manage Products"},
                    {3,"Manage Vendors"},
                    {4,"View Purchase Orders"},
                    {5,"Save Database"},
                    {6,"Exit Application"}};
    	
                    stMenuItem prod[MAX] = {
                    {1,"Find a Product"}, 
                    {2,"Add New Product"},
                    {3,"Delete A Product"},
                    {4,"Update Product Preferred Vendor"},
                    {5,"Update Product Stock Amount"},
                    {6,"Load Database"},
                    {7,"Manage Products"}};
    	
                    stMenuItem vend[MAX] = {   
                    {1,"View All Vendors"},
                    {2,"Find Vendor"},
                    {3,"Add New Vendor"},
                    {4,"Delete a Vendor"},
                    {5,"Update Vendor Data"},    
                    {6,"Return To Main Menu"}};			  
    	
    	stMenu menu[MAX] = {
                    {"Inventory Control Executive",main[MAX],6},
                    {"Manage Products",prod[MAX],7},
                    {"Manage Vendors",vend[MAX],6}};
    	
    	show(menu);
    
    	return 0;
    }
    
    int show(struct menu[MAX])
    {
    	int i=0;
    	cout << menu[i].caption;
    	return 1;
    }
    Also I am aware this will just flash up and disapear in the event that it does work, but there is more to the program.. I just wanna figure out this.

    Main idea is to use the show function to declare a header title, and under it a list of sub menu so the user can pick one and that int can be returned to main.

    Please help

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    Well I ran that program and it didnt work at all, I got a windows error. Anyhow thi sis the main program design I have to follow.

    Code:
    Notice that you will be creating many menus in this project. This suggests that menus
    can be modeled to be data types. A menu consists of a caption and multiple menu
    items. A menu item, consists of an index and a description. Therefore, create a struct
    called stMenuItem. Then create a struct called stMenu that contains appropriate
    fields. Note that one of the fields in your stMenu struct should be an array of
    stMenuItem. Feel free to set the maximum sizes for all arrays used to some large
    constant value. (Constants are the only global variables allowed.)

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    Structs define types, just like an int or a double is a type. When you declare a variable, you list its type and the variable name, e.g:
    Code:
    int num;
    Similarly, if you want to declare a variable of type stMenuItem, then you would do this:
    Code:
    stMenuItem varName;
    If you want to declare an array of type stMenuItem, you also have to list the type and a variable name and in addition you give the array size:
    Code:
    stMenuItem myArray[10];
    However, when you define your show() function, you do this:
    Code:
    int show(struct menu[MAX])
    {
            ...
    }
    That says the array will be of type "struct", the array name is menu, and it will have a size of MAX. There is no struct type.
    Last edited by 7stud; 11-26-2005 at 02:06 AM.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    I still dont understand.. I mean if I take away the whole show function, and just try and get the first string in the stMenu menu[MAX] to show I would do this right:?
    Code:
    cout << menu[0].caption;
    but that doesnt work..

    Another question is when you declare a structure you have to put struct before it as I did globally, using 'st' and then the name after it is just a goin to be part of the variable name right? I could have just as easily globally declared the structure as:
    Code:
    struct menuitems{
       int asdf;
    };
    correct?

    I'm so lost its pathetic. thanks for the response though.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    1) You are trying to do waaaay too much. The strategy for writing programs--especially when you are a beginner--is not to write the whole program, close your eyes, and then click on the compile button. There will be so many errors you won't know what to do.

    What you want to do is write a few lines of code, like one of your structs, and then test it out in main(). Try creating a struct, assigning its members some values, and displaying the values. Then, move on and write the next struct, etc. Writing a few lines at a time and then testing them will allow you to catch any errors immediately.

    2) You have to try and use a little more creativity with your variable names. You have more than one variable name that is identical to another name in your program. What two entities in your program are called 'main'? How about 'menu'?

    When you do this:
    Code:
    stMenu menu[MAX] = {
                    {"Inventory Control Executive",main[MAX],6},
                    {"Manage Products",prod[MAX],7},
                    {"Manage Vendors",vend[MAX],6}};
    You are trying to initialize the following stMenu members:
    Code:
    char caption[MAX];  <-----"Inventory Control Executive"
    stMenuItem menu[MAX]; <------main[MAX]
    int elements; <----6
    Here is the error:
    error C2440: 'initializing' : cannot convert from 'struct stMenuItem [100]' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    The first occurrence of the name 'main' in your program happens to be:
    Code:
    int main()
    {
    and the compiler thinks you are trying to assign the return value of main() to the second stMenu member. When you get careless with variable names and duplicate other names in your program, you are going to generate errors that are very hard to track down. That's the first time I've seen that 'main' error and it completely had me baffled.
    Last edited by 7stud; 11-26-2005 at 02:53 AM.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    Wow I am stupid, I didnt even see where you where goin with that 'main' thing until I read it over. but ya that would describe the 'int' in the error, cause main is a interger return function. Thanks for the explaination that cleared up alot!

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    Ok still not working, I have declared the structs correct.. I hope. as here
    Code:
    struct stMenuItem
    {
    	int index;
    	char desc[MAX];
    };
    and..
    Code:
    struct stMenu
    {
    	char caption[MAX];
    	stMenuItem list[MAX];
    	int elements;
    };
    But when I go to try and put this structure inside another structure I end up getting an error once again. I did what you said and went through the process slowly. I delcared the following types of structures from my template stMenuItems and could cout to the screen all of the parts:
    Code:
    stMenuItem MainMenu[MAX] = {{1,"Load Database"},{2,"Manage Products"}};
    stMenuItem Prods[MAX] = {{3,"ASDF"},{4,"QWER"}};
    the second I try to initialize the struct stMenu with the struct stMenuItems inside it, I get errors. How do you go about initializing a struct that contains a struct? here is how I tried..
    Code:
    stMenu options[MAX] = {{"char caption",MainMenu[MAX],1},{"Char caption2",Vend[MAX],3}};
    why doesnt this work? I have in location one a string of characters, in location two an array of structures of type stMenuItem and in location 3 an integer value..

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    damn! Ok I got it now.. dont declare the struct in stMenu as an array haha. Yay it finally works and here I seem to be talkin to myself!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. Problem displaying (structs functions)
    By dayknight in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2004, 06:11 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM