Doubt program with structures

0

The code works fine, but I do not know how I can do so by calling the function ' pedirdatos ' and enter everything, return to the main screen function, showing me the possibility of entering another contact or visit the contacts .
When trying with this clarification -> // pant_principal(n+1); , it does not let me write the option I want, it closes directly.

#include <iostream>

using namespace std;

struct contacto {
  char nombre[30];
  char apellido[30];
  char numtelf[11]; 
} cont[100];


int pant_principal(int n);

void pedirdatos(int n) {
  cout<<"digame el nombre del contacto -> "; 
  cin.getline(cont[n].nombre,30,'\n');
  cin.ignore();
  cout<<"digame el apellido del contacto -> "; 
  cin.getline(cont[n].apellido,30,'\n');
  cin.ignore();
  cout<<"digame el numero del contacto -> "; 
  cin.getline(cont[n].numtelf,11); cin.ignore();

  // pant_principal(n+1);       
}

void mostrardatos(contacto cont[], int n){  
  cout<<"======================================================"<<endl;
  cout<<"                      CONTACTOS"<<endl;
  cout<<"======================================================"<<endl;

  cout<<endl;
  cout<<"Nombre: "<<cont[n].nombre<<endl;
  cout<<"Apellido: "<<cont[n].apellido<<endl;
  cout<<"Numero de Telefono: "<<cont[n].numtelf<<endl;
}

int main() {
  int n=0;
  n=pant_principal(n);

  return 0;
}

int pant_principal( int n){     
  int eleccion;

  cout<<"======================================================"<<endl;
  cout<<"                BIENVENIDO A TU AGENDA"<<endl;
  cout<<"======================================================"<<endl;
  cout<<endl;

  cout<<"[1] - visitar agenda"<<endl;
  cout<<"[2] - agregar contacto"<<endl;
  cin>>eleccion;

  switch(eleccion){
  case 1:
    mostrardatos(cont,n);
    break;

  case 2: pedirdatos(n);
    n+=1;
    break;

  default: break;
  }

  return n; 
}
    
asked by pepe martinez 25.06.2017 в 10:43
source

2 answers

0

For these things, there are the control structures: while( ) { } , do { } while( ) , for( ) { } , and, if necessary, goto .

In the case that you expose, it occurs to me to modify pant_principal( ) so that, if the option is unknown, return -1 :

switch( ) {
...
default:
  n = -1;
  break;
}

and modify main( ) :

int main( ) {
  int n = 0;

  do {
    n = pant_principal( n );
  } while( n != -1 );

  return 0;
}

With the above, you should re-call at pant_principal( ) until it returns -1 , which will occur when the user enters an option other than 1 and 2 .

    
answered by 25.06.2017 в 12:15
-1

I know that people do not like this option because it is not very practical, but for this case, and I stress THIS CASE, I usually use the goto function. In such a way that when you finish, the goto takes you back to the beginning. But yes, only for this type of case where you just go back to the beginning of the menu.

    
answered by 26.06.2017 в 05:42