Thread: Question about Structures in Code

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    2

    Question about Structures in Code

    To start i apologize for my english but its a little hard for me.

    My problem is that i have to do a program that manages items in a supermarket.

    What I dont not sure about is after you make a structure outside of main, how can i properly do a menu to call that structure.

    I did the structures and the strings for the structures fairly easy what am not sure about is how do start the main since i have to do a menu and am not sure how to call a structure.

    Code:
     
    
    #include<iostream.h>
    #include<string.h>
    #include<stdlib.h>
    
    struct supermercado {
    	char Articulo [50];
    	char Tipo [50];
    	int Cantidad;
    	int Ventas;
    	float Precio;
    	int Suplidor;
    
    };
    	supermercado bistec;
    	supermercado pollo;
    	supermercado leche;
    	supermercado huevos;
    	supermercado pan;
    
    	supermercado carnes;
    	supermercado lacteos;
    	supermercado escolar;
    	supermercado general; 
    
    struct suplidor {
    	char Nombre [50];
    	char Direccion [50];
    	int Precio;
    };
    
    void main () {
    	
    	int opcion;
    
    	do{
    		cout<<"Supermercado:"<<endl;
    		cout<<"Menu del Supermercado: "<<endl;
    		cout<<"1. Crear Supermercado: "<<endl;
    		cout<<"2. Comprar articulo a suplidor o vender articulo a cliente: "<<endl;
    		cout<<"3. Buscar Suplidor: "<<endl;
    		cout<<"4. Buscar Articulo: "<<endl;
    		cout<<"5. Reportes: "<<endl;
    		cout<<"6. Salir: "<<endl;
    		cin>>opcion;
    
    		switch(opcion) {
    		case 1:
    			int x, y=0;
    		cout<<"Entre la cantidad de articulos en el supermercado: ";
    		cin>>x;
    		cout<<"Entre los articulos del supermercado: ";
    		do{
    		cin.getline (bistec.Articulo,50);
    		cin.getline (pollo.Articulo,50);
    		cin.getline (leche.Articulo,50);
    		cin.getline (huevos.Articulo,50);
    		cin.getline (pan.Articulo,50);
                      y++
    		}while(x != y); 
    		break;
    		}// end switch
    
    
    	}while(opcion >= 1 && opcion <= 5);
    }//end main

    If you can show me an example of how to call a structure inside a menu i would appreciated it. thank you. The elements are in spanish cuase thats the language that i use but its the same thing. Thank you in advanced.

    edit: edited code with what i have so far. I still have no idea on how to call the structures and make them do what i want but am trying. Again the elements are in spanish if you need me to translate it let me know.
    Last edited by Satellite; 01-11-2004 at 08:28 AM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is instantiation just as you would when using class.

    Kuphryn

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    What problems (specifically) are you having? A quick glance doesn't reveal anything that is glaringly incorrect: you create instances, and then make use of them within your menu loop.

    Are there any compiler errors you are getting, or is it just unexpected behavior?

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    As Zach L. said there is nothing wrong with your code as long as it works as inteended. But my advice would be encapsulate everything in a class. There you can implemente some form of meny
    Code:
    int main()
    {
    	CSupermarket mySupermarket;
    	int choice = 0;
    
    	while(choice != 9)
    	{
    		mySupermarket.Meny();
                    cin >> choice;
    
    		switch(choice)
    		{
    			case 1: mySupermarket.dosomething1(); break;
    			case 2: mySupermarket.dosomething2(); break;
    			case 3: mySupermarket.dosomething3(); break;
    			case 4: mySupermarket.dosomething4(); break;
    			case 5: mySupermarket.dosomething5(); break;
    			case 6: mySupermarket.dosomething6(); break;
    			case 7: mySupermarket.dosomething7(); break;
    			case 8: mySupermarket.dosomething8();break;
    			case 9: break;
    		}
    	}	
    return 0;
    }
    P.S Donīt use void main()
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  5. #5
    Board Conservative UnregdRegd's Avatar
    Join Date
    Jul 2003
    Posts
    154
    Right. It's basically a good idea to uses classes if you've already learned that.

    Code:
    #include <iostream>
    #include <string>
    
    class Supermarket
    {
    private:
        int noAvocados;
        int noTomatos;
        double balance;
    public:
        std::string customerPurchase(std::string item, float quantity);
    // ...
    };
    
    int main()
    {
        std::string input;
        Supermarket myGroceryStore;
        std::cout << "Welcome to My Grocery "
                  << "Store! Buy something: " << std::endl;
        std::cout << "Tomatos" << std::endl << "Avocados" << std::endl << ">";
        std::cin >> input;
        return 0;
    }
    I am a programmer. My first duty is to God, then to nation, then to employer, then to family, then to friends, then to computer, and finally to myself. I code with dignity, honor, and integrity.

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    2
    Yeah i havent been taught classes yet.
    Wondering if you guys can help me change this code to use functions ive been trying to change it to it using functions but all i do is mess up the program. Each time I try and end up reverting it to the original program since I make a big mess tht doesnt compile. The program works how its supposed to work without any funcions but when i try to change it to use functions it doesnt work out. Please help if you can. Thank you. Again the elements are in spanish cuase thats the language that i use. Thank you.

    Code:
    #include<iostream.h>
    #include<string.h>
    #include<stdlib.h>
    
    struct suplidor {
    	char Nombre [50];
    	char Direccion [50];
    	char Articulo [50];
    	int Precio;
    };
    	suplidor suplidor_a;
    	suplidor direccion_a;
    	suplidor articulos;
    
    struct supermercado {
    
    	char Articulo [50];
    	char Tipo [50];
    	int Cantidad;
    	int Ventas;
    	float Precio;
    	int Suplidor;
    
    };
    	
    	supermercado comidas;
    	supermercado tipos;
    	
    
    void main () {
    	
    	int opcion, opcion2, option, y=0;
    
    	do{
    		cout<<"Supermercado:"<<endl;
    		cout<<"Menu del Supermercado: "<<endl;
    		cout<<"1. Crear Supermercado: "<<endl;
    		cout<<"2. Comprar articulo a suplidor o vender articulo a cliente: "<<endl;
    		cout<<"3. Buscar Suplidor: "<<endl;
    		cout<<"4. Buscar Articulo: "<<endl;
    		cout<<"5. Reportes: "<<endl;
    		cout<<"6. Salir: "<<endl;
    		cin>>opcion;
    
    		switch(opcion) {
    
    		case 1:
    
    			int x;
    			double x1, x2, z;
    
    			do{
    
    		cout<<"Entre la cantidad de articulos en el supermercado: ";
    		cin>>x;
    
    		if(x < 10 || x > 300) {
    
    			cout<<"La cantidad de articulos tiene que ser entre 10 y 300 por favor trate de nuevo: "<<endl;
    
    		}//end if
    
    		else {
    
    		}//end else
    
    			}while( x < 10 || x > 300);
    
    		cout<<"Entre los articulos del supermercado: ";
    
    		do{
    			
    		cin.getline (comidas.Articulo,50);
    		y++;
    		
    		}while( y != x );
    		
    		cout<<"Entre el nombre del suplidor: ";
    		cin.getline (suplidor_a.Nombre,50);
    
    		cout<<"Entre la direccion del suplidor: ";
    		cin.getline (direccion_a.Direccion,50);
    
    		cout<<"Entre el nombre del articulo que vende el suplidor: ";
    		cin.getline (articulos.Articulo,50);
    
    		cout<<"Entre el tipo del articulo: "<<endl;
    		cout<<"1. Carnes: "<<endl;
    		cout<<"2. Lacteos: "<<endl;
    		cout<<"3. Escolar: "<<endl;
    		cout<<"4. General: "<<endl;
    		cin>>opcion2;
    
    		if( opcion2 == 1)
    		{
    			cout<<"Entre el precio del articulo para anadirle el tax: ";
    			cin>>z;
    			x1=z*0.06;
    			x2=x1+z;
    			cout<<x2<<endl;
    		}//end if
    
    		if( opcion2 == 2)
    		{
    			cout<<"Entre el precio del articulo para anadirle el tax: ";
    			cin>>z;
    			x1=z*0.04;
    			x2=x1+z;
    			cout<<x2<<endl;
    		
    		}//end if
    
    		if( opcion2 == 3)
    		{
    			cout<<"Entre el precio del articulo para anadirle el tax: ";
    			cin>>z;
    			x1=z*0.05;
    			x2=x1+z;
    			cout<<x2<<endl;
    		
    		}//end if
    
    		if( opcion2 == 4)
    		{
    			cout<<"Entre el precio del articulo para anadirle el tax: ";
    			cin>>z;
    			x1=z*0.10;
    			x2=x1+z;
    			cout<<x2<<endl;
    		
    		}//end if
    
    		
    
    		break;
    
    		case 2:
    
    			do{
    				cout<<"Menu de comprar articulo a suplidor o vender a clientes: "<<endl;
    				cout<<"1. Comprar: "<<endl;
    				cout<<"2. Vender: "<<endl;
    				cout<<"3. Salir: "<<endl;
    				cin>>option;
    				
    				switch(option) {
    
    				case 1:
    
    				cout<<articulos.Articulo<<endl;
    				cout<<opcion2<<endl;
    				cout<<"
    
    
    					break;
    
    				case 2:
    
    				cout<<articulos.Articulo<<endl;
    
    					break;
    
    				}//end switch
    
    			}while(option >=1 && option <=2);
    
    			break;
    
    		case 3:
    				cout<<"Nombre del suplidor: "<<suplidor_a.Nombre<<endl;
    				cout<<"Nombre del articulo que vende: "<<articulos.Articulo<<endl;
    
    			break;
    
    		case 4:
    				
    				cout<<"Nombre del articulo: "<<articulos.Articulo<<endl;
    				cout<<"Tipo del articulo: "<<opcion2<<endl;
    				cout<<"Precio del articulo con el tax: "<<x2<<endl;
    				
    
    			break;
    
    		case 5:
    
    			break;
    		}// end switch
    
    
    	}while(opcion >= 1 && opcion <= 5);
    }//end main
    Last edited by Satellite; 01-14-2004 at 05:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Tutorial sample code question
    By Kalleos in forum C Programming
    Replies: 2
    Last Post: 01-16-2009, 12:20 PM
  2. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  5. Replies: 1
    Last Post: 01-23-2002, 03:34 AM