#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
void menu();
void generarDatos(string fichero);
void listadoCodigo(string fichero);
void listadoNota(string fichero);
void listadoTotal(string fichero);
int main(int argc, char *argv[])
{
string fichero;
char opcion;
cout << "\nFichero: "; getline(cin, fichero);
do {
menu();
cin >> opcion;
switch (opcion)
{
case '1':
generarDatos(fichero);
break;
case '2':
listadoCodigo(fichero);
break;
case '3':
listadoNota(fichero);
break;
case '4':
listadoTotal(fichero);
break;
case '5':
cout << "\n\nFIN DEL PROGRAMA" << endl;
break;
default:
cout << "\n\nOPCION NO VALIDA" << endl;
break;
}
} while (opcion != '5');
return 0;
}
void menu()
{
cout << "\n MENU"
<< "\n======================"
<< "\n1.- Guardar datos"
<< "\n2.- Listado por Codigo"
<< "\n3.- Listado por Nota"
<< "\n4.- Listado Total"
<< "\n5.- Salir"
<< "\n======================"
<< "\nOPCION (1-5): ";
}
void generarDatos(string fichero)
{
ofstream out(fichero, ios_base::app);
string codigo, nombre;
float nota;
char opcion;
if (out.is_open())
{
cout << endl;
do {
cin.ignore(80, '\n');
cout << "Codigo:"; cin >> codigo;
cout << "Nombre: "; cin >> nombre;
cout << "Nota: "; cin >> nota;
out << codigo << " " << nombre << " " << nota << "\n";
cout << "\nOtro registro (S/n): ";
cin >> opcion;
opcion = toupper(opcion);
cout << endl;
} while (opcion == 'S');
}
else cout << "\n\nERROR - " << fichero << " NO EXISTE" << endl;
out.close();
}
void listadoCodigo(string fichero)
{
ifstream in(fichero);
string codigo, nombre;
float nota;
string buscado;
if (in.is_open())
{
cout << "\nCodigo: "; cin >> buscado;
cout << endl;
in >> codigo >> nombre >> nota;
while (!in.eof())
{
if (buscado == codigo)
cout << codigo << " " << nombre << " " << nota << endl;
in >> codigo >> nombre >> nota;
}
}
else cout << "\n\nERROR - " << fichero << " NO EXISTE" << endl;
in.close();
}
void listadoNota(string fichero)
{
ifstream in(fichero);
string codigo, nombre;
float nota;
float buscado;
if (in.is_open())
{
cout << "\nNota: "; cin >> buscado;
cout << endl;
in >> codigo >> nombre >> nota;
while (!in.eof())
{
if (buscado == nota)
cout << codigo << " " << nombre << " " << nota << endl;
in >> codigo >> nombre >> nota;
}
}
else cout << "\n\nERROR - " << fichero << " NO EXISTE" << endl;
in.close();
}
void listadoTotal(string fichero)
{
ifstream in(fichero);
string codigo, nombre;
float nota;
if (in.is_open())
{
cout << endl;
in >> codigo >> nombre >> nota;
while (!in.eof())
{
cout << codigo << " " << nombre << " " << nota << endl;
in >> codigo >> nombre >> nota;
}
}
else cout << "\n\nERROR - " << fichero << " NO EXISTE" << endl;
in.close();
}