0
I am trying to make a program that stores some data and then shows in the format ( matricula , name, Nota1, nota2) but when I run the code the program does not read the txt file and shows another part code that has already passed. In cmd it looks like this [![insert the image description here][1]]
I have to use some specific function to show the results?
#include <fstream>
using namespace std;
int main() {
std::fstream arquivo;
int n , nota1, nota2, matricula;
string linha , opcao ("opcao: ") , nome ;
cout << " 0 - sair \n 1 - criar arquivo \n 2 - incluir registros no arquivo \n 3 - mostrar arquivos \n \n" ;
cout << opcao ;
cin >> n ;
while (n!= 0 ){
if (n==1){
arquivo.open ("C:\\Users\\dougl\\Documents\\teste\\teste.txt");
cout << "\n\n arquivo criado corretamente\n \n \n ";
cout << "0 - sair \n 1 - criar arquivo \n 2 - incluir registros no arquivo \n 3 - mostrar arquivos \n \n" << "\n" << opcao ;
cin >> n ;
}
if(n==2) {
cout << "matricula: " ;
cin >> matricula ;
cout << "nome: " ;
cin >> nome ;
cout << "nota 1: " ;
cin >> nota1 ;
cout << "nota 2: " ;
cin >> nota2 ;
arquivo << "matricula: // nome: // nota1: // nota2: " << matricula << nome << nota1 << nota2;
cout << "\n\n";
arquivo.close();
cout << " 0 - sair \n 1 - criar arquivo \n 2 - incluir registros no arquivo \n 3 - mostrar arquivos \n \n" << opcao ;
cin >> n;
}
if (n== 3) {
arquivo.open ("C:\\Users\\dougl\\Documents\\teste\\teste.txt",std::ifstream::in);
if (arquivo.is_open())
{
while(getline(arquivo,linha))
{
cout << linha << endl ;
}
}else {
cout << "\n erro no arquivo" ;
}
}}
if (n==0){
return 0;
}
}
error happens in the
arquivo.open ("C:\\Users\\dougl\\Documents\\teste\\teste.txt",std::ifstream::in);
if (arquivo.is_open())
{
while(getline(arquivo,linha))
{
cout << linha << endl ;
}
}else {
cout << "\n erro no arquivo" ;
} ```
Here:
if(n=1) {you are assigning 1 to the variablen. If you want to compare with 1 use:if(n==1) {. Idemifs.– anonimo
i update ifs , but now if (n=3) starts a loop spamming error in the file, I don’t understand why not to open txt
– DouglaSz
You close the file after including a single record. If you try to include a second record the file will be closed. That’s right?
– anonimo
would be a program to include more than one record, would be more or less like this: matricula, name , Nota1 and nota2, and close, dps editing the same file by putting another person
– DouglaSz