If i use the switch statementthe read part doenst work int this code i the same "codigo" cant be used twice so i'm using the mensage Feedback
"esse codigo ja existe"
pls help me find whats is wrong with this code
#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Paciente
{
private:
int codigo, fixo, celular;
string nomePaciente, nomeCovenio;
public:
void setCodigo(int x) {
codigo = x;
}
int getCodigo() {
return codigo;
}
void setFixo(int x) {
fixo = x;
}
void setCelular(int x) {
celular = x;
}
void setNomePaciente(string x) {
nomePaciente = x;
}
void setNomeCovenio(string x) {
nomeCovenio = x;
}
int getFixo() {
return fixo;
}
int getCelular() {
return celular;
}
string getNomeCovenio() {
return nomeCovenio;
}
string getNomePaciente() {
return nomePaciente;
}
};
<i>#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
#include "Paciente.cpp"
using namespace std;
int main() {
fstream texto1("paciente.txt", ios::in |ios::out| ios::app);
int s;
Paciente p;
string q,z,r;
cout << "1-Cadastramento" << endl << "2-Agendamento" << endl << "3-Alteracao de Paciente" << endl << "4-Vizualizacao de Consultas" << endl;
cin >> s;
switch (s) {
case 1:
int w;
cout << "diga seu codigo" << endl;
cin >> s;
p.setCodigo(s);
cout << "diga seu nome" << endl;
cin >> q;
p.setNomePaciente(q);
cout << "diga seu nome do covenio" << endl;
cin >> q;
p.setNomeCovenio(q);
cout << "diga o seu telefone fixo" << endl;
cin >> w;
p.setFixo(w);
cout << "diga o seu telefone celular" << endl;
cin >> w;
p.setCelular(w);
texto1 << endl << "Paciente " << p.getNomePaciente() << endl;
texto1 << "Covenio " << p.getNomeCovenio() << endl;
texto1 << "Fixo " << p.getFixo() << endl;
texto1 << "Celular " << p.getCelular() << endl;
texto1 << "Codigo " << p.getCodigo() << endl;
texto1.clear();
while (texto1 >> z >> r) {
if (z == "Codigo") {
int x = atoi(r.c_str());
if (x == 12) {
cout << "esse codigo ja existe" << endl;
};
};
};
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
cout << "Nao eh uma opcao" << endl;
break;
};
texto1.close();
system("pause");
return 0;
}</i>,
An fstream
only keeps one file position, shared between read and write.
Each time you switch between reading and writing, you have to do a seek to set the position for the next operation. If you really want to use the current position, you can use the minimum seek of
texto1.seekp(0, ios_base::cur);
which "moves" the file position 0 bytes from the current position. But it's still formally a seek, and you can then either read or write from that position.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.