Help with a phone manager

This is a discussion on Help with a phone manager within the C++ Programming forums, part of the General Programming Boards category; Hi! i started my c++ tutorial about 3 weeks ago, they instructed me to do a program that stores the ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    Help with a phone manager

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

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    You have bigger problems with your structure than just the gotos but if you just want your program to work, look up strcmp()
    Don't quote me on that... ...seriously

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Are you sure your tutorial has not covered arrays and loops yet. What you need is one struct definition and an array or std::vector of it - something to store multiple instances in. Otherwise, how would you code the same problem if you had 10 000 directory entries? unknown number of directory entries?

    It is also strange if it hasn't taught about std::string which is a standard class for character strings instead of raw character arrays.

    It is strange that it teaches about goto (or you are coming from BASIC?). When I once wanted to use it I had to look it up on the Internet.
    Last edited by anon; 07-18-2007 at 02:42 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    As, hopefully, you'll soon realise, you could re-write your program to do exactly the same thing in about 30-40 lines, as opposed to the several hundred you've written there..

    In any general case, wherever you've got a huge amount of repeating code which looks almost identical, there's almost always a quick and easy solution (Which will give you far less errors too)

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    1.
    You are making multiple structs when you can make a typedef for a struct and create an array of them like so.

    Code:
    typedef struct 
    {
           char nombre[60];
           int telefono;
           bool usado;
    } caller; //whatever caller is in spanish

    This creates a type definition, saying that caller is a type and it has a name, a telephone number, and an usado...whatever that is.

    Then you can declare
    std::vector<caller> callers;

    Then you can add callers to it via a for loop just to test
    Code:
    for(int i =0; i < 5; ++i)
    {
    add randomized callers
    }
    The "if(...); if(...); if(...); goto; goto; goto" statements are not intuitive. You should have something like
    Code:
    exit = false;
    while(!exit)
    {
    //
    //Displays menu with choices
    //
    if (some condition)
       exit = functionA(); //defined as bool functionA();
    elseif(...)
       exit = functionB(); //bool functionB();
    else
       exit = false
    }
    and each function returning the status of whether the user would like to return to the menu or quit.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    2
    Thanks for your replies, i kinda suspected a general mistake
    and my tut hasnt covered anything you mentioned, and it wasnt the first time, most of the time i end up googling like hell, even at this point they havent mentioned cin, cout or print, i had to look for them myself, and yet they prompted with this type of exercise(i was able to solve like 4 of them but i couldn't this one hehe), i will try to make this program work with your feedback and after that ill be changing tutorial thanks again!

  7. #7
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    typedef struct
    {
    char nombre[60];
    int telefono;
    bool usado;
    } caller; //whatever caller is in spanish
    You would not have to use a typedef. You can just declare the struct
    Code:
    struct Name
    {
    int data1;
    int data2;
    char data3; //etc
    };
    
    and use it like you would a class
    Name var1;
    
    var1.data1=2;
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a phone book program
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-19-2007, 07:31 AM
  2. strtok() a phone number
    By Sure in forum C Programming
    Replies: 3
    Last Post: 06-27-2005, 07:46 PM
  3. Moral dillema
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 08-22-2004, 04:23 PM
  4. M$ phone suit
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 12-26-2002, 05:58 PM
  5. Phone Class
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2002, 07:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21