0

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);

}
Andrea D.
  • 1
  • 1
  • 2
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Aug 31 '16 at 14:25
  • Also see: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – NathanOliver Aug 31 '16 at 14:26
  • Hello Nathan, 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? – Andrea D. Aug 31 '16 at 14:36
  • That is because in string literals like `"C:\Documenti\studenti.txt"` the `\ ` is a special character and need to be escaped with another `\ ` like `"C:\\Documenti\\studenti.txt"` – NathanOliver Aug 31 '16 at 14:42
  • I tried to use \\ but it seems it doesn't create the file at this point. Before It used to create it. – Andrea D. Aug 31 '16 at 14:49

1 Answers1

0

According to my interpretation your file is storing information in this format:_

nome1 classe1

nome2 classe2

...

...

...

since file reads data from the stream , it will extract the next string available in the stream. In your code the classe is not read from the file and text corresponding to classe is entered into nome which explains absurd output.Moreover while updating nome you should do that outside the if part because file must be read in any case irrespective of the fact whether the classe matches cla or not. Have a look at this code:-

void cl(){ 
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;
    while(!fin.eof()){
       fin>>nome>>classe;//change here
       if(cla==classe){
          cout<<nome<<endl;
       }
    }------------------"<<endl;
}
rishabh
  • 96
  • 6