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;
}
#include <iostream.h>Code:It stops working when i introduce a menu into it.
#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]