error when searching by program name c ++

0

that such friends I am creating a program in c ++ with structures: 2. Store the names and ages of 10 people in an arrangement. Find the name corresponding to a certain age and the age corresponding to a person. Both options must be accessible through a menu: a) Search by name. b) Search by age. c) Exit.

but at the time of making the query by name it tells me that the person of that name does not exist in the registry I leave the complete code I would appreciate prompt help

#include <iostream>
#include <stdlib.h> //NUEVA LIBRERIA
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <string>
#include <D:\c++\proyecto final\libreria validacion de enteros\bibliotecavalidaint.cpp>
#define maximo 2
using namespace std;
using namespace validacionIngresoint;

struct per{
char codigo[25];
char nomb[50];
char nombre[50];
char direccion[50];
char sexo[30];
int edad;
}persona[maximo];
char inicio[1];


/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {

char opc,resp,aux,entero;
string estudiante;
int contador, i, j,c,k,bandera,edadbuscar;
int p=0;


do{
    opc=aux;
    system ("cls");

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
    printf("\tBien Venido Usuario\n");
    printf("Elige alguna de las siguientes Opciones\n");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
    printf("\n\ta) Ingresar 10 Personas\n");
    printf("\tb) Buscar por nombre \n");
    printf("\tc) Buscar por Edad \n");
    printf("\td) Salir\n");
    cout<<"--> ";cin>>opc;
    do{
    bandera=aux;
    opc=tolower(opc);
    if((opc!='a')&&(opc!='b')&&(opc!='c')&&(opc!='d'))
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
    printf("\n---<Por favor elija una opcion valida>---: ");
    bandera=1;
    cin>>opc;
    }
    }while(bandera==1);

    switch(opc){

                case 'a':
                    {   

                        system("cls");
                        p=0;
                        for(contador=0;contador<maximo;contador++)
                        {
                            p=p+1;
                            cout << "\n Codigo de persona: "<<p;
                            cin.getline(persona[contador].codigo,25);
                            cout << "\nIngrese nombre: ";
                            cin.getline(persona[contador].nombre,50);
                            cout << "Ingrese direccion: ";
                            cin.getline(persona[contador].direccion,50);
                            cout << "Ingrese sexo: ";
                            cin.getline(persona[contador].sexo,30);
                            cout << "Ingrese edad: ";

                            do{
                                persona[contador].edad=tomarInt();
                                if(persona[contador].edad<1||persona[contador].edad>100)
                                {
                                 cout << "\nEdad Ingresada no valida: \n";
                                }                       
                            }while (persona[contador].edad<1||persona[contador].edad>100);
                            }
                         cout<<"Usted ingreso "<<p<<" de 10 alumnos: ";
                         system ("pause");

                            break;
                     }

                case 'b':
                 {
                        do{
                        cin.getline(inicio,1);
                        p=0;
                        system ("cls");
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
                        cout << "\n Ingrese nombre a buscar: ";

                        cin.getline(persona[1].nomb,50);
                        cout<<persona[1].nomb<<endl;
                        system("pause");
                        for(i=0; i<maximo; i++)
                        {
                            cout<<persona[i].nombre<<endl;
                            cout<<persona[1].nomb<<endl;
                               if (persona[1].nomb == persona[i].nombre)
                            {
                                p=p+1;
                                cout<<p<<endl;
                                cout << "SU CODIGO" << "----> " << persona[i].codigo << endl;
                                cout << "SU NOMBRE" << "----> " << persona[i].nombre << endl;
                                cout << "DIRECCION" << "----> " << persona[i].direccion << endl;
                                cout << "EDAD" << "----> " << persona[i].edad << endl;
                                cout <<"________________________________________________________________________\n";
                                putchar('\a');
                            }
                        }
                        if(p==0)
                        {
                            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
                            cout<<"\n No existen alumnos "<<persona[1].nomb<<endl;
                            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
                            cout<<"\n Si decea realizar otra busque digite s: ";
                            cin>>resp;
                        }
                        if(p>0)
                        {
                            cout<<"\n Si decea realizar otra busque digite s: ";
                            cin>>resp;

                           }

                }while(resp=='s'||resp=='S');
    break;
                }


                   case 'c':
                   {
                        do{
                        cin.getline(inicio,1);
                        system ("cls");
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
                        cout << "\n Ingrese edad a buscar: " << endl;

                        do
                        {
                        cin>>edadbuscar;

                        for(k=0; k<maximo; k++)
                        {
                            if (edadbuscar== persona[k].edad)
                            {
                                cout << "SU CODIGO" << "----> " << persona[k].codigo << endl;
                                cout << "SU NOMBRE" << "----> " << persona[k].nombre << endl;
                                cout << "DIRECCION" << "----> " << persona[k].direccion << endl;
                                cout << "EDAD" << "----> " << persona[k].edad << endl;
                                cout <<"________________________________________________________________________\n";
                                putchar('\a');
                            }
                        }
                        if(edadbuscar!=persona[k].edad)
                        {
                            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
                            cout<<"\n No existen alumnos de "<<edadbuscar<<" años\e"<<endl;
                            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
                            cout<<"\n Si decea realizar otra busque digite s: ";
                            cin>>resp;
                        }

                        }while((edadbuscar!=persona[k].edad)&&(resp=='s'||resp=='S'));

                            cout<<"\n Si decea realizar otra busque digite s: ";
                            cin>>resp;
                }while(resp=='s'||resp=='S');

                        break;
                   }

                   case 'd':{


                    break;
                   }

                   }

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);             
    cout<<"\nDigite S para Realizar otra operacion segun opciones de menu: ";
    cin>>resp;
    }while(resp=='s'||resp=='S');
    system("pause");
    return 0;
}

    
asked by ricardocuc 09.11.2018 в 06:15
source

1 answer

0

When comparing strings, as is your case when comparing names, it is not good to use == , it is better to use a string comparer. I suggest changing the line if (persona[1].nomb == persona[i].nombre) to the following: int strcmp ( const char * str1, const char * str2 ) . This condition will return a 0 when the two strings are equal.

Here is an example of how that command works:

char key[] = "apple";
  char buffer[80];
  do {
     printf ("Guess my favorite fruit? ");
     fflush (stdout);
     scanf ("%79s",buffer);
  } while (strcmp (key,buffer) != 0);
  cout<<"Correct!!"<<endl;

I say it's not good to use == because == will not always work, only in cases where the string overloads the operator. In the case where you do not overload the operator, when using == you will be comparing the memory address instead of the object itself. For this reason I prefer to use strcmp .

    
answered by 09.11.2018 в 08:12