i'm new to the forum so forgive me if i've done something wrong. I'm a bit in a hurry and I couldn't even traslate what I've written in this code. I create the file and it works, I add new records and it works, I also print all the file and it works. So, the problem is void cl(). I give a classroom as an input and it should give me the students in that classroom as an output, the problem is that this doesnt work, actually sometimes it prints a student and the same classroom for many times. What should I do? Keep in mind that I haven't done much about files, so if there's a simple way to solve this I would like it more, but well I accept all the possible help. Thanks! EDIT: I also forgot to say I have some strange warning messages, like "[Warning] Unknown escape sequence: '\D' " and \S. This works for every void, i presume it's about the file name, but is it a problem?
#include <iostream>
#include <string>
#include <cstdlib>
#include<fstream>
using namespace std;
string nome, classe;
ofstream fout;
ifstream fin;
void crea(){ /*this is the file creation*/
cout<<"-----------------------"<<endl;
char risp;
fout.open("C:\Documenti\studenti.txt",ios::out);
do{
cout<<"Inserisci nome: "<<endl;
cin>>nome;
cout<<"Inserisci classe: "<<endl;
cin>>classe;
fout<<nome<<" "<<classe<<endl;
cout<<"un altro record (s/n): "<<endl;
cin>>risp;
}while(risp=='s');
fout.close();
cout<<"--------------------------------"<<endl;
}
void aggiungi(){ /*this adds new records to the file*/
cout<<"----------------------"<<endl;
char risp;
fout.open("C:\Documenti\studenti.txt", ios::out | ios::app);
do{
cout<<"Inserisci nome: "<<endl;
cin>>nome;
cout<<"Inserisci classe: "<<endl;
cin>>classe;
fout<<nome<<" "<<classe<<endl;
cout<<"Un altro record (s/n)?"<<endl;
cin>>risp;
}while(risp=='s');
fout.close();
cout<<"-------------------------"<<endl;
}
void visualizza(){ /*this prints all the file on the video*/
cout<<"--------------------------"<<endl;
fin.open("C:\Documenti\studenti.txt",ios::in);
if(!fin)
cout<<"File inesistente, impossibile procedere. "<<endl;
else{
fin>>nome>>classe;
while(!fin.eof()){
cout<<nome<<" "<<classe<<endl;
fin>>nome>>classe;
}
cout<<"----------------------------"<<endl;
fin.close();
}
}
void cl(){ /*this SHOULD work like i say in the description*/
cout<<"--------------------------"<<endl;
string cla;
fin.open("C:\Documenti\Studenti.txt",ios::in);
if(!fin)
cout<<"File inesistente, impossibile procedere. "<<endl;
else{
cout<<"Inseire classe per sapere i nomi degli studenti: "<<endl;
cin>>cla;
cout<<"**********************************"<<endl;
fin>>nome;
while(!fin.eof()){
if(cla==classe){
cout<<nome<<endl;
fin>>nome;
}
}------------------"<<endl;
}
}
int esci(){ /*this is the exit status*/
char risp;
cout<<"Hai selezionato l'opzione 'esci', sei sicuro? s/n "<<endl;
cin>>risp;
}
int main(){ /*main with menu*/
int n;
do{
cout<<"Scegliere cosa fare: "<<endl;
cout<<"1) Creare il file con classi e nomi degli studenti. "<<endl;
cout<<"2) Aggiungere ulteriori studenti."<<endl;
cout<<"3) Visualizzare classe e nome di ogni studente. "<<endl;
cout<<"4) Visualizzare tutti gli studenti di una data classe. "<<endl;
cout<<"5) Esci. "<<endl;
cin>>n;
switch(n){
case 1:
crea();
break;
case 2:
aggiungi();
break;
case 3:
visualizza();
break;
case 4:
cl();
break;
case 5:
esci();
break;
default:
system("pause");
return 0;
}
}while(n!=5);
}