0

in this program i'm required to make a menu in the console, the user puts two arrays and it shows them some options to do with them, like finding their union and intersection, but until the user specifies to go out of here this menu has to work with those two arrays.

So i make the user input how big and which things go in each array, and each time the options are shown i print the arrays too, not a problem over there.

When the user picks a choice in the menu, it calls a void() function inside the main.cpp, and that void function calls a function over the aux cpp,

void conjuntos(){
cout<< "----------------------------------------"<<endl;
cout<< "                Conjuntos               "<<endl;
cout<< "----------------------------------------"<<endl;
char** opciones = new char*[6];
    opciones[0] = "Union";
    opciones[1] = "Interseccion";
    opciones[2] = "Diferencia";
    opciones[3] = "Diferencia simetrica";
    opciones[4] = "Pertenece";
    opciones[5] = "Contenido";
int opc;
//Here the user inputs each array
                            cout<<"Ingrese dos arreglos"<<endl;
                            cout<<"Arreglo 1 (cantidad): ";
                            int cant1;
                            cin>>cant1;
 int arr1[cant1];

                                    for(int i= 0;i<cant1;i++){
                                        cout<<i+1<<". ";
                                        cin>>arr1[i];
                                    };
                            cout<<"Arreglo 2 (cantidad): ";
                            int cant2;
                            cin>>cant2;
 int arr2[cant2];
                                    for(int i= 0;i<cant2;i++){
                                        cout<<i+1<<". ";
                                        cin>>arr2[i];
                                    };


do{
cout<< "----------------------------------------"<<endl;
cout<< "                Conjuntos               "<<endl;
cout<< "----------------------------------------"<<endl;

//Here the pc outputs the arrays
                             cout<<"Arreglo 1: [";
                              for(int i= 0;i<cant1;i++){
                                    if( i==cant1-1){
                                        cout<<arr1[i];
                                    }else{
                                        cout<<arr1[i]<<",";
                                    };
                              };
                                cout<<"]"<<endl;
                                cout<<"Arreglo 2: [";
                              for(int i= 0;i<cant2;i++){
                                    if( i==cant2-1){
                                        cout<<arr2[i];
                                    }else{
                                        cout<<arr2[i]<<",";
                                    };
                              };
                                cout<<"]"<<endl;
 // !!!!!
cout<< "----------------------------------------"<<endl;


  opc = menu(opciones, 6);
  switch( opc ){
    case 1: unido(); break;
    case 2: interconjunto(); break;
    case 3: diferencia(); break;
    case 4: simetrica();break;
    case 5: pertenece();break;
    case 6: contenido();break;
  };
}while(opc!=0);

cout <<" ------------------------------------------"<<endl;
cout <<"               MENU PRINCIPAL              "<<endl;
cout <<" ------------------------------------------"<<endl;
};

When the user picks a choice in the menu, it calls a void() function inside the main.cpp

 void unido(){
  cout<< "----------------------------------------"<<endl;
  cout<< "El arreglo union de ambos conjuntos es: "<<endl;
  cout<< unido(arr1,cant1,arr2,cant2);

};

And that void function calls a function over the aux cpp, with help of the .h

    #ifndef CONJUNTOS_H_INCLUDED
#define CONJUNTOS_H_INCLUDED

int* unido(int* arr1,int cant1,int* arr2, int cant2);


#endif // CONJUNTOS_H_INCLUDED

Heres the aux cpp

    #include <iostream>
#include "Enteros.h"
#include "Reales.h"
#include "Char.h"
#include "Conjuntos.h"

using namespace std;

int* unido(int* arr1,int cant1,int*arr2,int cant2){
    cout<<"cant1";
    return 0;
};

My question is that in void conjuntos() where is the menu, i have the arrays, i need to use them with each function that is called by this menu, but when i use a function like in void unido() it says the arr1, cant1, arr2 and cant2 are not specified in the functions, how do i send those variables from the conjuntos ** menu over the **unido function?

  • 3
    Begin by replacing `char** opciones = new char*[6];` by `std::vector`. – Jarod42 May 25 '18 at 16:02
  • And get rid of the VLAs. I assume you are not permitted to use the `c++` standard library. The program would be completely different if you were using modern `c++`. – drescherjm May 25 '18 at 16:03
  • Also [get rid of `using namespace std;`](https://stackoverflow.com/q/1452721/10077). – Fred Larson May 25 '18 at 16:06

1 Answers1

0

I think you should use an object to contain your arrays, it will make the code clearer, and as others mentionned, look at std::vector / (std::array) / std::string: after learning how to use them, you will see that they provide a very nice level of abstraction.

Then, if you want to have an object modified by a function1 and used by a function2, in C++ you can do:

void function1(MyObject & o) {...}
void function2(MyObject const & o) {...}

int main() {

    MyObject o;
    function1(o); // function1 must take the object by reference to be able to modify it.
    function2(o); // function2 can take the object by const reference if no modification is required.

    return 0;
}

Alternatively you can also have the object created and returned by the first function, like so:

MyObject function1() {...}
void function2(MyObject const & o) {...}

int main() {

    MyObject o = function1();
    function2(o); // function2 can take the object by const reference if no modification is required.

    return 0;
}
Olivier Sohn
  • 1,292
  • 8
  • 18
  • im studying this thing in the college, so its very hard to me to know, but thanks for the help, im going to look how to make those arrays in a object and that std::vector thing you told me. Thank you... – David Leon May 25 '18 at 16:44