Good day! This question is a continuation of what I did a few weeks ago: How to use Arrays and loops to calculate the weighted average of the university?
In that question Loops and Arrays were used for the calculation of the weighted average of the university, the problem is that the user must put the information (name of the subject, qualification, credits) each time that he opens the program and wanted to automate that part so I used 'fstream' to create a file for each of these variables (the only thing that will change each time the user uses the program is the qualification of each subject so it is not necessary to create a file for be variable, but I did it anyway "to practice": D).
Everything works very well but now I do not know how to read the data from the files and use them in the program. I was thinking of creating an 'if' to ask the user if he wants to use the same subjects and credits or if he wants to enter others (in case the semester has passed or a friend wants to calculate his weighted average), after receiving the answer of the user, how do I use the information already stored in the files? (assuming that the user has already used the program once so that said information is already stored in the files). In short, how to read the file knowing that the one with the names has words and the one with the credits has numbers? Here my code:
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
#define NOTA_MIN 0.0f
#define NOTA_MAX 5.0f
#define CRED_MIN 0
#define CRED_MAX 10
int main(){
//Nombre del programa
cout << "\t\t Promedio Ponderado\n";
int num_Mat;
float sumCreditos = 0, sumPonderacion = 0;
// Solicita el numero de materias que luego es usado en el array
cout << "Ingrese el numero de materias\n";
cin >> num_Mat;
float notas [num_Mat];
int creditos [num_Mat];
// Archivo para guardar los nombres de las materias
ofstream file1; // Lo crea
file1.open("Nombre de materias.txt"); // Lo abre y nombra
// Declara un array de tipo string con "n" elementos, definidos por la cantidad de materias
string nombres [num_Mat];
// Loop para obtener el nombre de las materias y guardarlas en el archivo
for (int i = 0; i < num_Mat; i++){
cout << "Ingresa el nombre de la materia " << i+1 << endl; // "+1" porque no existe Materia 0
cin >> nombres[i];
file1 << nombres[i] << endl;
}
file1.close(); // Cierra el archivo 1
// Archivo para guardar las calificaciones
ofstream file2; // Lo crea
file2.open("Calificaciones.txt"); // Lo abre y nombra
// Archivo para guardar los creditos
ofstream file3; // Lo crea
file3.open("Creditos.txt"); // Lo abre y nombra
for (int i = 0; i < num_Mat; i++){
cout << nombres[i] << ": \t" << endl;
// Loop para obtener las calificaciones
do {
cout << "nota:\t\t";
cin >> notas[i];
file2 << notas[i] << endl;
cin.clear();
} while (notas[i] < NOTA_MIN || notas[i] > NOTA_MAX);
// Loop para obtener los creditos
do {
cout << "credito:\t";
cin >> creditos[i];
file3 << creditos[i] << endl;
cin.clear();
} while (creditos[i] < CRED_MIN || creditos[i] > CRED_MAX);
// sumar las notas*creditos y los creditos
sumCreditos += creditos[i];
sumPonderacion += notas[i] * creditos[i];
}
file2.close(); // Cierra el archivo 2
file3.close(); // Cierra el archivo 3
cout << "Tu promedio semestral es: " << sumPonderacion / sumCreditos << endl;
cin.get();
return 0;
}
I am aware that my code is a little messy but first I want to finish the program and then I will optimize it and use functions to make it better.
Thank you very much!