如何在C++的菜单中使用范围

How to work with scope in a menu in C++

本文关键字:菜单 使用范围 C++      更新时间:2023-10-16

在这个程序中,我需要在控制台中制作一个菜单,用户放置两个数组,它向他们显示一些与它们相关的选项,例如查找它们的并集和交集,但在用户指定离开这里之前,此菜单必须与这两个数组一起使用。

所以我让用户输入每个数组的大小和内容,每次显示选项时,我也打印数组,这不是问题。

当用户在菜单中选择一个选项时,它会在 main.cpp 中调用一个 void(( 函数,而该 void 函数会调用一个通过 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;
};

当用户在菜单中选择一个选项时,它会在 main 中调用一个 void(( 函数.cpp

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

并且该 void 函数在 .h 的帮助下通过 aux cpp 调用一个函数

#ifndef CONJUNTOS_H_INCLUDED
#define CONJUNTOS_H_INCLUDED
int* unido(int* arr1,int cant1,int* arr2, int cant2);

#endif // CONJUNTOS_H_INCLUDED

这是辅助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;
};

我的问题是,在void conjuntos(( 中,菜单在哪里,我有数组,我需要将它们与此菜单调用的每个函数一起使用,但是当我使用像 void unido((这样的函数时,它说 arr1、cant1、arr2 和 cant2 没有在函数中指定,我如何将这些变量从conjuntos ** 菜单发送到 **unido函数?

我认为你应该使用一个对象来包含你的数组,它会使代码更清晰,正如其他人提到的,看看 std::vector/(std::array(/std::string: 在学习如何使用它们之后,你会发现它们提供了一个非常好的抽象级别。

然后,如果你想让一个对象被函数1修改并由函数2使用,在C++中你可以执行以下操作:

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;
}

或者,您也可以让第一个函数创建和返回对象,如下所示:

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;
}