Stop running, adding and displaying files with C ++ delimiters

0

I have a program in which I capture and show with delimiters, I try to use tokens with the function strtok to separate the record by the same delimiters but at the time of printing it only prints the first record of the file and then stops work and I have no idea why, does anyone know what my error is?

According to me, the problem is with the show function, because it adds them well in the file.

Thank you very much in advance.

#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdlib>
using namespace std;

class User{

    public:
        void capture();
        void show();
        void deleteU();
        void searchU();
        void edit();

        char idUser[30];
        char name[30];
        char mail[40];
        char tel[20];
        char ranking[30];
        char idProfile[30];
};

int main(){
    int op;
    User x;
    int z=1;
    cout<<"|||||||||||||||||||||||||||||||||||||||||FILE EDITOR||||||||||||||||||||||||||||||||||||||||"<<endl<<endl;
    cout<<"PLEASE TYPE EVERYTHING WITH CAPITAL LETTERS, OTHERWISE YOU MAY HAVE PROBLEMS FINDING A FILE"<<endl;
    while(z!=0){
        cout<<"What do you want to do?"<<endl;
        cout<<"\t 1)Capture"<<endl;
        cout<<"\t 2)Show"<<endl;
        cout<<"\t 3)Delete"<<endl;
        cout<<"\t 4)Search"<<endl;
        cout<<"\t 5)Edit"<<endl;
        cin>>op;

            switch(op)
            {
                case 1:
                    x.capture();
                    break;
                case 2:
                    x.show();
                    break;
                case 3:
                    x.deleteU();
                    break;
                case 4:
                    x.searchU();
                    break;
                case 5:
                    x.edit();
                    break;
                default:
                    cout<<"Your option was invalid, type a valid number."<<endl;
            }
    }
    return 0;
}

void User::capture()
{
    cout<<"Give me the ID USER: ";
    cin.ignore();
    cin.get(idUser,30);
    cout<<"Give me the NAME: ";
    cin.ignore();
    cin.get(name,30);
    cout<<"Give me the MAIL: ";
    cin.ignore();
    cin.get(mail,40);
    cout<<"Give me the TELEPHONE NUMBER: ";
    cin.ignore();
    cin.get(tel,20);
    cout<<"Give me the RANKING: ";
    cin.ignore();
    cin.get(ranking,30);
    cout<<"Give me the ID PROFILE: ";
    cin.ignore();
    cin.get(idProfile,30);

    ofstream write("User.txt",ios::app);
    if (!write.good())
    {
        cout<<"\n\n\tFile not found."<<endl;
    }
    else
    {
        write<<idUser<<'|'<<name<<'|'<<mail<<'|'<<tel<<'|'<<ranking<<'|'<<idProfile<<'|'<<'\n';
    }
    write.close();
}
void User::show()
{
    ifstream read;
    read.open("User.txt");
    char line[200];
    read.getline(line,sizeof(line));
    while(!read.eof()){
        for(int i=0;i<6;i++){
            char *pointer;
            if(i==0){
                pointer = strtok(line,"|");
                strcpy(idUser,pointer);
            }
            else if(i==1){
                pointer = strtok(NULL,"|");
                strcpy(name,pointer);
            }
            else if(i==2){
                pointer = strtok(NULL,"|");
                strcpy(mail,pointer);
            }
            else if(i==3){
                pointer = strtok(NULL,"|");
                strcpy(tel,pointer);
            }
            else if(i==4){
                pointer = strtok(NULL,"|");
                strcpy(ranking,pointer);
            }
            else if(i==5){
                pointer = strtok(NULL,"|");
                strcpy(idProfile,pointer);
            }
        }
                    cout<<"ID USER: "<<idUser<<endl;
                    cout<<"NAME: "<<name<<endl;
                    cout<<"MAIL: "<<mail<<endl;
                    cout<<"TEL: "<<tel<<endl;
                    cout<<"RANKING: "<<ranking<<endl;
                    cout<<"ID PROFILE: "<<idProfile<<endl;
                    cout<<"_________________________________"<<endl<<endl;
    }
    read.close();
}

void User::deleteU()
{
}
void User::searchU()
{
}
void User::edit()
{/*
    ofstream aux;
    ifstream lectura;
    encontrado=false;
    int auxClave=0;
    char auxNombre[30];
    aux.open("auxiliar.txt",ios::out);
    lectura.open("alumnos.txt",ios::in);
    if(aux.is_open() && lectura.is_open()){
        cout<<"Ingresa la Clave del Alumno que deseas Modificar: ";
        cin>>auxClave;
        lectura>>clave;
        while(!lectura.eof()){
            lectura>>nombre>>semestre>>grupo>>edad;
            if(auxClave==clave){
                encontrado=true;
                cout<<"__________________________"<<endl;
                cout<<"Clave: "<<clave<<endl;
                cout<<"Nombre: "<<nombre<<endl;
                cout<<"Semestre: "<<semestre<<endl;
                cout<<"Grupo: "<<grupo<<endl;
                cout<<"Edad: "<<edad<<endl;
                cout<<"__________________________"<<endl;
                cout<<"Ingresa el Nuevo Nombre del alumno con Clave "<<clave<<": ";
                cin>>auxNombre;
                aux<<clave<<" "<<auxNombre<<" "<<semestre<<" "<<grupo<<" "<<edad<<endl;
                cout<<"Registro Modificado"<<endl;
            }else{
                aux<<clave<<" "<<nombre<<" "<<semestre<<" "<<grupo<<" "<<edad<<endl;
            }
            lectura>>clave;
        }
    }else{
        cout<<"No se pudoAbrir el Archivo o aun no ha sido Creado"<<endl;
    }
    if(encontrado==false){
        cout<<"No se encontro ningun registro con clave "<<auxClave<<endl;
    }
    aux.close();
    lectura.close();
    remove("alumnos.txt");
    rename("auxiliar.txt","alumnos.txt");*/
}
    
asked by Abril Barrera 04.09.2017 в 06:28
source

1 answer

0

Check your algorithm ... you're just reading the first line of the file:

read.getline(line,sizeof(line)); // <-- fuera del bucle
while(!read.eof()){ // <-- Aqui comienza el bucle
  // Aqui no hay mas lecturas
}

The simplest solution is surely to add a second reading at the end of the loop:

read.getline(line,sizeof(line));
while(!read.eof()){
  // ...
  read.getline(line,sizeof(line));
}

On the other hand, I do not understand the meaning of the for loop that you have installed there ... you can load the loop and the code would still be functional:

//    for(int i=0;i<6;i++){
        char *pointer;
//        if(i==0){
            pointer = strtok(line,"|");
            strcpy(idUser,pointer);
//        }
//        else if(i==1){
            pointer = strtok(NULL,"|");
            strcpy(name,pointer);
//        }
//        else if(i==2){
            pointer = strtok(NULL,"|");
            strcpy(mail,pointer);
//        }
//        else if(i==3){
            pointer = strtok(NULL,"|");
            strcpy(tel,pointer);
//        }
//        else if(i==4){
            pointer = strtok(NULL,"|");
            strcpy(ranking,pointer);
//        }
//        else if(i==5){
            pointer = strtok(NULL,"|");
            strcpy(idProfile,pointer);
//        }

By the way, if you are not preventing the exercise, consider using std::string instead of char[] to store the character strings.

    
answered by 04.09.2017 в 08:41