<HTML><HEAD></HEAD>
<BODY dir=ltr>
<DIV dir=ltr>
<DIV style="FONT-FAMILY: 'Calibri'; COLOR: #000000; FONT-SIZE: 12pt">
<DIV>Hola que tal como andan.</DIV>
<DIV> </DIV>
<DIV>He escrito mi primer programa, de 100 y pico líneas, con satisfacción. Lo 
he querido ampliar haciendo que se escriban los datos en un fichero, la idea es 
que cuando abriese el programa de nuevo, al pedir el resumen leyese del archivo. 
El problema es que se cargan datos que salen de solo Dios sabe que cosa 
aleatoria de memoria saca, sé que tiene que ver con que no se cargan los datos 
que yo quiero para las variables que quiero, y por eso me saca datos 
aleatorios.</DIV>
<DIV> </DIV>
<DIV>El programa saca un pequeño menú de 4 opciones al principio, y según lo que 
se elija va a una función u otra, todo dentro de la misma clase. Seguro que hay 
mil formas de mejorarlo, pero ya os digo que es mi primer programa 
completo.</DIV>
<DIV> </DIV>
<DIV>La opción que falla es la tercera : <FONT face="Courier New">3.- Mostrar 
informe. </FONT>Creo que la escritura en el archivo sí la hace bien, 
creo...</DIV>
<DIV> </DIV>
<DIV>He buscado algo por internet, pero es poco lo que se ofrece con el tema de 
manejo de ficheros, al menos en binario, lo tocan todo muy básico (luego es más 
complicado de lo que parece).</DIV>
<DIV> </DIV>
<DIV>Aquí os pongo el código amigos, muchas gracias de antemano:</DIV>
<DIV> </DIV>
<DIV><FONT face="Courier New">#include <iostream></FONT></DIV>
<DIV><FONT face="Courier New">#include <fstream></FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">using namespace std;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">class controldegastos</FONT></DIV>
<DIV><FONT face="Courier New">{</FONT></DIV>
<DIV><FONT face="Courier New">public:</FONT></DIV>
<DIV><FONT face="Courier New">    void establecesueldo(int 
a);</FONT></DIV>
<DIV><FONT face="Courier New">    void establecegastos(int a, int 
b, int c, int d, int e, int f);</FONT></DIV>
<DIV><FONT face="Courier New">    void 
muestragastos();</FONT></DIV>
<DIV><FONT face="Courier New">    void menu();</FONT></DIV>
<DIV><FONT face="Courier New">private:</FONT></DIV>
<DIV><FONT face="Courier New">    int sueldo;</FONT></DIV>
<DIV><FONT face="Courier New">    int gastos;</FONT></DIV>
<DIV><FONT face="Courier New">    int queda;</FONT></DIV>
<DIV><FONT face="Courier New">    int coche;</FONT></DIV>
<DIV><FONT face="Courier New">    int movil;</FONT></DIV>
<DIV><FONT face="Courier New">    int visa;</FONT></DIV>
<DIV><FONT face="Courier New">    int comida;</FONT></DIV>
<DIV><FONT face="Courier New">    int gasolina;</FONT></DIV>
<DIV><FONT face="Courier New">    int abono;</FONT></DIV>
<DIV><FONT face="Courier New">};</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">void controldegastos::establecesueldo(int 
a)</FONT></DIV>
<DIV><FONT face="Courier New">{</FONT></DIV>
<DIV><FONT face="Courier New">    sueldo = a;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">    ofstream file;</FONT></DIV>
<DIV><FONT face="Courier New">    file.open("file.dat", ios::out 
| ios::binary);</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">    
file.write(reinterpret_cast<const char *>(&sueldo), 
sizeof(sueldo));</FONT></DIV>
<DIV><FONT face="Courier New">    file.close();</FONT></DIV>
<DIV><FONT face="Courier New">}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">void controldegastos::establecegastos(int a, int 
b, int c, int d, int e, int f)</FONT></DIV>
<DIV><FONT face="Courier New">{</FONT></DIV>
<DIV><FONT face="Courier New">    coche = a;</FONT></DIV>
<DIV><FONT face="Courier New">    movil = b;</FONT></DIV>
<DIV><FONT face="Courier New">    visa = c;</FONT></DIV>
<DIV><FONT face="Courier New">    comida = d;</FONT></DIV>
<DIV><FONT face="Courier New">    gasolina = e;</FONT></DIV>
<DIV><FONT face="Courier New">    abono = f;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">    gastos = a + b + c + d + e + 
f;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">    ofstream file;</FONT></DIV>
<DIV><FONT face="Courier New">    file.open("file.dat", ios::out 
| ios::binary);</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">//    file.write((char *) 
&coche, sizeof(coche));</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">    
file.write(reinterpret_cast<const char *>(&coche), 
sizeof(coche));</FONT></DIV>
<DIV><FONT face="Courier New">    
file.write(reinterpret_cast<const char *>(&movil), 
sizeof(movil));</FONT></DIV>
<DIV><FONT face="Courier New">    
file.write(reinterpret_cast<const char *>(&visa), 
sizeof(visa));</FONT></DIV>
<DIV><FONT face="Courier New">    
file.write(reinterpret_cast<const char *>(&comida), 
sizeof(comida));</FONT></DIV>
<DIV><FONT face="Courier New">    
file.write(reinterpret_cast<const char *>(&gasolina), 
sizeof(gasolina));</FONT></DIV>
<DIV><FONT face="Courier New">    
file.write(reinterpret_cast<const char *>(&abono), 
sizeof(abono));</FONT></DIV>
<DIV><FONT face="Courier New">    
file.write(reinterpret_cast<const char *>(&gastos), 
sizeof(gastos));</FONT></DIV>
<DIV><FONT face="Courier New">    file.close();</FONT></DIV>
<DIV><FONT face="Courier New">}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">void controldegastos::muestragastos()</FONT></DIV>
<DIV><FONT face="Courier New">{</FONT></DIV>
<DIV><FONT face="Courier New">    ifstream file;</FONT></DIV>
<DIV><FONT face="Courier New">    file.open("file.dat", ios::in | 
ios::binary);</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">    if (!file)</FONT></DIV>
<DIV><FONT face="Courier New">        cout 
<< "No se puede abrir el archivo...";</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">//    file.read((char *) 
&coche, sizeof(coche));</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">    file.read((char *) &coche, 
sizeof(coche));</FONT></DIV>
<DIV><FONT face="Courier New">    file.read((char *) &movil, 
sizeof(movil));</FONT></DIV>
<DIV><FONT face="Courier New">    file.read((char *) &visa, 
sizeof(visa));</FONT></DIV>
<DIV><FONT face="Courier New">    file.read((char *) &comida, 
sizeof(comida));</FONT></DIV>
<DIV><FONT face="Courier New">    file.read((char *) 
&gasolina, sizeof(gasolina));</FONT></DIV>
<DIV><FONT face="Courier New">    file.read((char *) &abono, 
sizeof(abono));</FONT></DIV>
<DIV><FONT face="Courier New">    file.read((char *) &gastos, 
sizeof(gastos));</FONT></DIV>
<DIV><FONT face="Courier New">    file.read((char *) &sueldo, 
sizeof(sueldo));</FONT></DIV>
<DIV><FONT face="Courier New">    file.close();</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">    cout << "Gastos de este 
mes :" << endl;</FONT></DIV>
<DIV><FONT face="Courier New">    cout << 
"---------------------" << endl;</FONT></DIV>
<DIV><FONT face="Courier New">    cout << 
endl;</FONT></DIV>
<DIV><FONT face="Courier New">    cout << "Coche : " 
<< coche << endl;</FONT></DIV>
<DIV><FONT face="Courier New">    cout << "Comida : " 
<< comida << endl;</FONT></DIV>
<DIV><FONT face="Courier New">    cout << "Abono : " 
<< abono << endl;</FONT></DIV>
<DIV><FONT face="Courier New">    cout << "Gasolina : " 
<< gasolina << endl;</FONT></DIV>
<DIV><FONT face="Courier New">    cout << "Seguro / Visa : 
" << visa << endl;</FONT></DIV>
<DIV><FONT face="Courier New">    cout << "Móvil : " 
<< movil << endl;</FONT></DIV>
<DIV><FONT face="Courier New">    cout << 
endl;</FONT></DIV>
<DIV><FONT face="Courier New">    cout << "Sueldo : " 
<< sueldo << endl;</FONT></DIV>
<DIV><FONT face="Courier New">    cout << "Gastos : " 
<< gastos << endl;</FONT></DIV>
<DIV><FONT face="Courier New">    cout << "Te sobra : " 
<< (sueldo - gastos) << endl;</FONT></DIV>
<DIV><FONT face="Courier New">    cout << 
endl;</FONT></DIV>
<DIV><FONT face="Courier New">}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">void controldegastos::menu()</FONT></DIV>
<DIV><FONT face="Courier New">{</FONT></DIV>
<DIV><FONT face="Courier New">char eleccion;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">cout << "Elige la opción :" << 
endl;</FONT></DIV>
<DIV><FONT face="Courier New">cout << "---------------------" << 
endl;</FONT></DIV>
<DIV><FONT face="Courier New">cout << "1.- Establecer sueldo." << 
endl;</FONT></DIV>
<DIV><FONT face="Courier New">cout << "2.- Establecer gastos." << 
endl;</FONT></DIV>
<DIV><FONT face="Courier New">cout << "3.- Mostrar informe." << 
endl;</FONT></DIV>
<DIV><FONT face="Courier New">cout << "4.- Salir del programa." << 
endl;</FONT></DIV>
<DIV><FONT face="Courier New">cout << endl;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">cin >> eleccion;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">        switch 
(eleccion)</FONT></DIV>
<DIV><FONT 
face="Courier New">            
{</FONT></DIV>
<DIV><FONT 
face="Courier New">            
case '1':</FONT></DIV>
<DIV><FONT 
face="Courier New">                
int valor;</FONT></DIV>
<DIV><FONT 
face="Courier New">                
cout << "Introduce un valor: "; cin >> valor; cout << 
endl;</FONT></DIV>
<DIV><FONT 
face="Courier New">                
establecesueldo(valor);</FONT></DIV>
<DIV><FONT 
face="Courier New">                
menu();</FONT></DIV>
<DIV><FONT 
face="Courier New">                
break;</FONT></DIV>
<DIV><FONT 
face="Courier New">            
case '2':</FONT></DIV>
<DIV><FONT 
face="Courier New">                
int a, b, c, d, e, f;</FONT></DIV>
<DIV><FONT 
face="Courier New">                
cout << "Introduce cuanto dinero vas a gastar en coche este mes : "; cin 
>> a; cout << endl;</FONT></DIV>
<DIV><FONT 
face="Courier New">                
cout << "Introduce cuanto dinero vas a gastar en comida este mes : "; cin 
>> d; cout << endl;</FONT></DIV>
<DIV><FONT 
face="Courier New">                
cout << "Introduce cuanto dinero vas a gastar en abono este mes : "; cin 
>> f; cout << endl;</FONT></DIV>
<DIV><FONT 
face="Courier New">                
cout << "Introduce cuanto dinero vas a gastar en gasolina este mes : "; 
cin >> e; cout << endl;</FONT></DIV>
<DIV><FONT 
face="Courier New">                
cout << "Introduce cuanto dinero vas a gastar en seguro/visa este mes : "; 
cin >> c; cout << endl;</FONT></DIV>
<DIV><FONT 
face="Courier New">                
cout << "Introduce cuanto dinero vas a gastar en móvil este mes : "; cin 
>> b; cout << endl;</FONT></DIV>
<DIV><FONT 
face="Courier New">                
establecegastos(a, b, c, d, e, f);</FONT></DIV>
<DIV><FONT 
face="Courier New">                
menu();</FONT></DIV>
<DIV><FONT 
face="Courier New">                
break;</FONT></DIV>
<DIV><FONT 
face="Courier New">            
case '3':</FONT></DIV>
<DIV><FONT 
face="Courier New">                
muestragastos();</FONT></DIV>
<DIV><FONT 
face="Courier New">                
menu();</FONT></DIV>
<DIV><FONT 
face="Courier New">                
break;</FONT></DIV>
<DIV><FONT 
face="Courier New">            
case '4':</FONT></DIV>
<DIV><FONT 
face="Courier New">                
return;</FONT></DIV>
<DIV><FONT 
face="Courier New">            
default:</FONT></DIV>
<DIV><FONT 
face="Courier New">                
cout << "Escoge una opción de las que ofrece el menú..." << 
endl;</FONT></DIV>
<DIV><FONT 
face="Courier New">                
cout << endl;</FONT></DIV>
<DIV><FONT 
face="Courier New">                
menu();</FONT></DIV>
<DIV><FONT 
face="Courier New">                
break;</FONT></DIV>
<DIV><FONT 
face="Courier New">            
}</FONT></DIV>
<DIV><FONT face="Courier New">}</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">int main()</FONT></DIV>
<DIV><FONT face="Courier New">{</FONT></DIV>
<DIV><FONT face="Courier New">    controldegastos 
miclase;</FONT></DIV>
<DIV><FONT face="Courier New">    miclase.menu();</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">    return 0;</FONT></DIV>
<DIV><FONT face="Courier New">}</FONT></DIV>
<DIV style="FONT-FAMILY: 'Calibri'; COLOR: #000000; FONT-SIZE: 12pt">
<META name=GENERATOR content="LibreOffice 3.4  (Win32)">
<META name=CREATED content=20111024;9105492>
<META name=CHANGED content=20111024;9124326>
<STYLE type=text/css>
        <!--
                @page { margin: 2cm }
                p { margin-bottom: 0.21cm }
        -->
        </STYLE>

<P style="MARGIN-BOTTOM: 0cm"><BR><FONT color=#548dd4><FONT 
face="Arial, sans-serif"><FONT size=2><I><B>Oscar Aparicio 
Holgado</B></I></FONT></FONT></FONT><BR><FONT color=#e36c09><FONT 
face="Tahoma, sans-serif"><FONT size=2>Administrador de 
sistemas.</FONT></FONT></FONT></P></DIV></DIV></DIV></BODY></HTML>