Thread: my file created wont work when i insert a menu. why????

  1. #1
    Unregistered
    Guest

    Angry my file created wont work when i insert a menu. why????

    i have a program that its purpose is to ask a person to name his file. I take the name and make it a file in where the person puts some data in. Then I'm supposed to open it. It works when i have this code
    [/code]
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>

    int answer ;
    char nombreapellido[20], filename[20], filename2[20], pesa[5], alture[5], edad[5], keep_going, respuesta;
    char buffer[256] ;

    void crear_usuario ()
    {
    cout <<"Ingrese el nombre que quiera para su cuenta"<<endl;
    cin.getline (filename, 20);
    ofstream cuenta ;
    cuenta.open (filename);
    if (cuenta.is_open () )
    {
    cout <<"Nombre y Apellido: ";
    cin.getline (nombreapellido, 20);
    cuenta <<nombreapellido <<endl;
    cout <<"Peso (kg): ";
    cin.getline (pesa, 5) ;
    cuenta <<pesa<<endl;
    cout <<"Altura (m): ";
    cin.getline (alture, 6);
    cuenta <<alture<<endl;
    cout <<"Edad: ";
    cin.getline (edad, 5);
    cuenta <<edad <<endl;
    cuenta.close ();
    cout <<"**Cuenta Grabada. Bienvenido**"<<endl;
    }

    }


    void cuenta_usuario ()
    {
    char buffer[256];
    cout <<"Por favor ingrese el nombre de su cuenta"<<endl;
    cin.getline (filename, 20) ;
    fstream cuenta (filename, 20);
    if (! cuenta.is_open())
    {
    cout << "Error opening file"; exit (1);
    }
    while (! cuenta.eof() )
    {
    cuenta.getline (buffer, 256);
    cout <<buffer <<endl;
    }
    }


    int main ()
    {
    crear_usuario ();
    cuenta_usuario ();
    return 0;
    }

    Code:
    It stops working when i introduce a menu into it.
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>

    int answer ;
    char nombreapellido[20], filename[20], filename2[20], pesa[5], alture[5], edad[5], keep_going, respuesta;
    char buffer[256] ;

    void crear_usuario ()
    {
    cout <<"Ingrese el nombre que quiera para su cuenta"<<endl;
    cin.getline (filename, 20);
    ofstream cuenta ;
    cuenta.open (filename);
    if (cuenta.is_open () )
    {
    cout <<"Nombre y Apellido: ";
    cin.getline (nombreapellido, 20);
    cuenta <<nombreapellido <<endl;
    cout <<"Peso (kg): ";
    cin.getline (pesa, 5) ;
    cuenta <<pesa<<endl;
    cout <<"Altura (m): ";
    cin.getline (alture, 6);
    cuenta <<alture<<endl;
    cout <<"Edad: ";
    cin.getline (edad, 5);
    cuenta <<edad <<endl;
    cuenta.close ();
    cout <<"**Cuenta Grabada. Bienvenido**"<<endl;
    }

    }


    void cuenta_usuario ()
    {
    char buffer[256];
    cout <<"Por favor ingrese el nombre de su cuenta"<<endl;
    cin.getline (filename, 20) ;
    fstream cuenta (filename, 20);
    if (! cuenta.is_open())
    {
    cout << "Error opening file"; exit (1);
    }
    while (! cuenta.eof() )
    {
    cuenta.getline (buffer, 256);
    cout <<buffer <<endl;
    }
    }

    void menu ()
    {
    cout <<" 1. Crear un nuevo usuario"<<endl;
    cout <<" 2. Ingresar a su archivo"<<endl;
    cout <<" 3. AYUDA"<<endl;
    cout <<" 4. SALIR"<<endl;
    cin >>answer;
    if (answer == 1)
    crear_usuario ();
    if (answer == 2 )
    cuenta_usuario ();
    }
    int main ()
    {
    menu ();
    crear_usuario ();
    cuenta_usuario ();
    return 0;
    }



    [code]

  2. #2
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    i didn't understand most of that code, must be a translation error

    M.R.
    I don't like you very much. Please post a lot less.
    Cheez
    *and then*
    No, I know you were joking. My point still stands.

  3. #3
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    The problem is in

    Code:
    menu();
    crear_usuario (); 
    cuenta_usuario ();

    remove crear_usuario(); and cuenta_usuario(); from there.

    since you are using the menu() to input the choice and then proceed to the function opted by the user, you dont need to call those to functions again.


    PS you have reversed the code tags use [/code] at the end and [code] at the begining
    -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM