Hi! i started my c++ tutorial about 3 weeks ago, they instructed me to do a program that stores the name and phone of a person, and be able to seek for the phone of an inputed name and viceversa, the problem is that my program has problem to identify which data to output, for example if i input peter's phone, insted of getting "peter" i get joshep's phone, i read my code like 9999 times searching for a human mistake and i came to the conclusion that the malfunction is caused by my lack of knowledge, so i came here
sorry for the gotos, i plan to restructure the program but my priority is to make it function correctly, even with the gotos it is very easy to undestand, thanks!
Code:
#include <iostream>
using std::endl;
#include <string.h>
using namespace std;
using std::cout;
struct directorio {
struct uno{
char nombre[60];
int telefono;
bool usado;
}persona1;
struct dos{
char nombre[60];
int telefono;
bool usado;
}persona2;
struct tres{
char nombre[60];
int telefono;
bool usado;
}persona3;
struct cuatro{
char nombre[60];
int telefono;
bool usado;
}persona4;
struct cinco{
char nombre[60];
int telefono;
bool usado;
}persona5;
struct seis{
char nombre[60];
int telefono;
bool usado;
}persona6;
struct siete{
char nombre[60];
int telefono;
bool usado;
}persona7;
struct ocho{
char nombre[60];
int telefono;
bool usado;
}persona8;
struct nueve{
char nombre[60];
int telefono;
bool usado;
}persona9;
struct diez{
char nombre[60];
int telefono;
bool usado;
}persona10;
}datos;
int main()
{
int seleccion;
char apodo[60];
int fon;
char A = A;
char B = B;
char C = C;
menu:
cout << "Bienvenido\n";
cout << "1 Nuevo registro\n" << endl;
cout << "2 Buscar por nombre\n" << endl;
cout << "3 Buscar por telefono\n" << endl;
cout << "4 Salir\n" << endl;
cout << endl;
cout << endl;
cout << "Que deseas hacer?\n" << endl;
cin>> seleccion;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
if (seleccion == 1) goto registro;
if (seleccion == 2) goto buscanombre;
if (seleccion == 3) goto buscatelefono;
if (seleccion == 4) goto salida;
registro:
if (datos.persona1.usado== false)
{
cout << "Ingresa el nombre\n" << endl;
cin >> datos.persona1.nombre;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Ingresa el telefono\n" << endl;
cin >> datos.persona1.telefono;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
datos.persona1.usado = true;
goto menu;
}
if (datos.persona2.usado == false)
{
cout << "Ingresa el nombre\n" << endl;
cin >> datos.persona2.nombre;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Ingresa el telefono\n" << endl;
cin >> datos.persona2.telefono;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
datos.persona2.usado = true;
goto menu;
}
if (datos.persona3.usado == false)
{
cout << "Ingresa el nombre\n" << endl;
cin >> datos.persona3.nombre;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Ingresa el telefono\n" << endl;
cin >> datos.persona3.telefono;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
datos.persona3.usado = true;
goto menu;
}
if (datos.persona4.usado == false)
{
cout << "Ingresa el nombre\n" << endl;
cin >> datos.persona4.nombre;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Ingresa el telefono\n" << endl;
cin >> datos.persona4.telefono;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
datos.persona4.usado = true;
goto menu;
}
if (datos.persona5.usado == false)
{
cout << "Ingresa el nombre\n" << endl;
cin >> datos.persona5.nombre;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Ingresa el telefono\n" << endl;
cin >> datos.persona5.telefono;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
datos.persona5.usado = true;
goto menu;
}
if (datos.persona6.usado == false)
{
cout << "Ingresa el nombre\n" << endl;
cin >> datos.persona6.nombre;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Ingresa el telefono\n" << endl;
cin >> datos.persona6.telefono;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
datos.persona6.usado = true;
goto menu;
}
if (datos.persona7.usado == false)
{
cout << "Ingresa el nombre\n" << endl;
cin >> datos.persona7.nombre;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Ingresa el telefono\n" << endl;
cin >> datos.persona7.telefono;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
datos.persona7.usado = true;
goto menu;
}
if (datos.persona8.usado == false)
{
cout << "Ingresa el nombre\n" << endl;
cin >> datos.persona8.nombre;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Ingresa el telefono\n" << endl;
cin >> datos.persona8.telefono;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
datos.persona8.usado = true;
goto menu;
}
if (datos.persona9.usado == false)
{
cout << "Ingresa el nombre\n" << endl;
cin >> datos.persona9.nombre;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Ingresa el telefono\n" << endl;
cin >> datos.persona9.telefono;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
datos.persona9.usado = true;
goto menu;
}
if (datos.persona10.usado == false)
{
cout << "Ingresa el nombre\n" << endl;
cin >> datos.persona10.nombre;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Ingresa el telefono\n" << endl;
cin >> datos.persona10.telefono;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
datos.persona10.usado = true;
goto menu;
}
buscanombre:
cout << "Ingresa el nombre a buscar\n" << endl;
cin >> apodo;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
{
if (apodo == datos.persona1.nombre)
cout << datos.persona1.nombre << endl;
cout << datos.persona1.telefono << endl;
goto menu;
}
{
if (apodo == datos.persona2.nombre)
cout << datos.persona2.nombre << endl;
cout << datos.persona2.telefono << endl;
goto menu;
}
{
if (apodo == datos.persona3.nombre)
cout << datos.persona3.nombre << endl;
cout << datos.persona3.telefono << endl;
goto menu;
}
{
if (apodo == datos.persona4.nombre)
cout << datos.persona4.nombre << endl;
cout << datos.persona4.telefono << endl;
goto menu;
}
{
if (apodo == datos.persona5.nombre)
cout << datos.persona5.nombre << endl;
cout << datos.persona5.telefono << endl;
goto menu;
}
{
if (apodo == datos.persona6.nombre)
cout << datos.persona6.nombre << endl;
cout << datos.persona6.telefono << endl;
goto menu;
}
{
if (apodo == datos.persona7.nombre)
cout << datos.persona7.nombre << endl;
cout << datos.persona7.telefono << endl;
goto menu;
}
{
if (apodo == datos.persona8.nombre)
cout << datos.persona8.nombre << endl;
cout << datos.persona8.telefono << endl;
goto menu;
}
{
if (apodo == datos.persona9.nombre)
cout << datos.persona9.nombre << endl;
cout << datos.persona9.telefono << endl;
goto menu;
}
{
if (apodo == datos.persona10.nombre)
cout << datos.persona10.nombre << endl;
cout << datos.persona10.telefono << endl;
goto menu;
}
goto menu;
buscatelefono:
cout << "Ingresa el telefono a buscar\n" << endl;
cin >> fon;
cin.ignore(numeric_limits<streamsize>::max(),'\n');
{
{if (fon == datos.persona1.telefono)
cout << datos.persona1.nombre << endl;
cout << datos.persona1.telefono << endl;
goto menu;}
{if (fon == datos.persona2.telefono)
cout << datos.persona2.nombre << endl;
cout << datos.persona2.telefono << endl;
goto menu;}
{if (fon == datos.persona3.telefono)
cout << datos.persona3.nombre << endl;
cout << datos.persona3.telefono << endl;
goto menu;}
{if (fon == datos.persona4.telefono)
cout << datos.persona4.nombre << endl;
cout << datos.persona4.telefono << endl;
goto menu;}
{if (fon == datos.persona5.telefono)
cout << datos.persona5.nombre << endl;
cout << datos.persona5.telefono << endl;
goto menu;}
{ if (fon == datos.persona6.telefono)
cout << datos.persona6.nombre << endl;
cout << datos.persona6.telefono << endl;
goto menu;}
{if (fon == datos.persona7.telefono)
cout << datos.persona7.nombre << endl;
cout << datos.persona7.telefono << endl;
goto menu;}
{if (fon == datos.persona8.telefono)
cout << datos.persona8.nombre << endl;
cout << datos.persona8.telefono << endl;
goto menu;}
{if (fon == datos.persona9.telefono)
cout << datos.persona9.nombre << endl;
cout << datos.persona9.telefono << endl;
goto menu;}
{if (fon == datos.persona10.telefono)
cout << datos.persona10.nombre << endl;
cout << datos.persona10.telefono << endl;
goto menu;}
}
salida:
cout << "Presiona enter para salir" << endl;
cin.get();
}