how can i make the data that i input in a function appear in another one?

This is a discussion on how can i make the data that i input in a function appear in another one? within the C++ Programming forums, part of the General Programming Boards category; No, you're not getting it. You're still filling local members with data and you're NOT filling the struct. I don't ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    No, you're not getting it.
    You're still filling local members with data and you're NOT filling the struct.
    I don't know why it crashes either, but you should get that code right first.

  2. #17
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    The code as provided is more appropriate to the C board, not the C++ board here but...

    Code:
    struct Discos
    {
        ...
    };
    
    void ingresar(Dicos& dicos, int& index);
    void revisar(Dicos& dicos);
    Type names do not match.



    If you're gonna post on the C++ board you might as well start using some C++ concepts such as containers and standard console I/O streams (std::cin/std::cout). Start with your Discos struct/container, it should contain information for a single CD and the items should not be arrays since we will only store information for a single CD (I've changed the name to a singular Disco instead of plural Discos):
    Code:
    struct Disco
    {
        int cap;
        int miliseg;
        float cost;
        float prec;
        char codven;
    };
    Now your program should have a container (such as a std::vector) to hold all the Disco objects (as many as the user wants to enter). This object should be declared in main and passed into the functions to get/display the data for a given CD:
    Code:
    #include <vector>
    
    ...
    
    int main()
    {
        std::vector<Disco> discos;
    Now, your functions should take a reference to this std::vector container. The ingresar function will push new objects into the vector and passing by reference will preserve changes between function calls to the vector. The revisar function should take a const reference to the vector (since it is not the type of function that will be making changes to the vector).

    Code:
    void ingresar(std::vector<Disco>&);
    void revisar(const std::vector<Disco>&);
    
    int main()
    {
        ...
        
            switch (op)
            {
            case 1:
            {
                ingresar(discos);
                break;
            }
            case 2:
            {
                revisar(discos);
                break;
            }
        ...
    }
    
    void ingresar(std::vector<Disco>& discos)
    {
        ...
    }
    
    void revisar(const std::vector<Disco>& discos)
    {
        ...
    }
    Now, the ingresar function needs to populate an instance of a Disco object and then push that onto the vector:
    Code:
    #include <iostream>
    
    ...
    
    void ingresar(std::vector<Disco>& discos)
    {
        Disco temp;  // Temp object
    
        std::cout << "\nFavor de ingresar los siguientes datos:\n\n\nCapacidad del disco en byte: ";
        std::cin >> temp.cap;
        std::cout << "\nTiempo de acceso en milisegundos: ";
        std::cin >> temp.miliseg;
        std::cout << "\nCodigo del vendedor (A, B, C, D): ";
        std::cin >> temp.codven;
        std::cout << "\nCosto de fabrica: ";
        std::cin >> temp.cost;
        std::cout << "\nPrecio de venta al publico: ";
        std::cin >> temp.prec;
    
        discos.push_back(temp);  // Add the latest Disco object entered by the user to the vector
    }
    Work from that. Try to do the revisar function yourself. You'll need to iterate over the container with some type of loop and output the data for all Disco objects in the vector.
    Last edited by hk_mp5kpdw; 10-22-2007 at 07:58 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #18
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    no wonder i never understood some of these stuff lol sorry


    any way a mod can move this thread over there so i can get help in C and not C++? sorry for annoying you people :P

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

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