Compare strings within C++ file

Asked

Viewed 41 times

-1

Good afternoon everyone, here’s the problem. I have data inside the txt file and when in the program the user is asked to enter login and password for comparison, it simply stays in a loop. Can someone give me a light please? I’ve researched enough and still can’t solve my problem! Below I’ll leave the code.

void entrar(void) { 
usu.open("usu_admin.txt", ios::in);

if(!usu) { 
    cout << "Sistema de cadastro | Infra.V.1.0 - Entrar" 
         << endl << endl << "Usuario admin nao cadastrado!" 
         << endl << endl << "Aguarde um momento...";
    
    this_thread::sleep_for(chrono::seconds(3));
    
    system("cls");
        
    cad_admin();
} 
else { 
    while(getline(usu, linha)) { 
        cout << "Sistema de cadastro | Infra.V.1.0 - Entrar" 
             << endl << "Login admin: ";
        
        cin >> l;
        cin.clear();
        
        cout << "Senha admin: ";
        
        for(i=0;i<6;i++) { 
            cin.clear();
            s[i]=getch ();
            putchar ('*');
        } 
        
        cout << endl << endl << "Aguarde um momento..."; 
        
        this_thread::sleep_for(chrono::seconds(3));
        
        char* linTemp;
        
        linTemp=&linha[0];
        
        if((strcmp(l, linTemp)==0) && (strcmp(s, linTemp)==0)) {
            system ("cls");
            
            menu ();
        }
    }
}

usu.close();

}

  • post an entire, compileable program. Help others help you

  • Hello, follow the code. Thanks for the answer. I could not post it in the correct way of code. But all the lines of it are present...

  • :) it was funny. It should be able to join. To make it easier for you can use ``` before and after the code. The crack sign, not the apostrophe, 3 times at the beginning of the line, before and after and the editor Mark-Down understands as code what is in the middle. You can also copy the whole code to the post, select the first letter and go down with the keyboard, keeping shift tight so as to select the whole program, and then fix control-k. More boring but works too.

2 answers

0


#include #include #include #include #include #include <conium. h> #include

using namespace Std;

void beginning(void);

void enter(void);

void cad_admin(void);

void menu(void);

Static char inicio, l[15], login[15], password[7], s[7]; Static int i, cont=0; Static string line;

fstream Usu, usu_general;

int main() { beginning();

return 0;

}

void comeco(void) { while(start!='2') { Cout << "Registration system | Infra.V.1.0" << Endl << Endl << "[1] - Login" << Endl << "[2] - Exit" << Endl << Endl << "Select your option: ";

    cin >> inicio;
    cin.clear();
    
    switch(inicio) {
        case '1':
            system("cls");
        
            entrar();
        break;
        
        case '2':
            system("cls");
        
            cout << "Sistema de cadastro | Infra.V.1.0" << endl
                << endl << "Obrigado por utilizar o sistema!";
        
            this_thread::sleep_for(chrono::seconds(3));
    }
    
    system("cls");
}

}

void enter(void) { Usu.open("usu_admin.txt", Ios::in);

if(!usu) {
    cout << "Sistema de cadastro | Infra.V.1.0 - Entrar" 
         << endl << endl << "Usuario admin nao cadastrado!" 
         << endl << endl << "Aguarde um momento...";
    
    this_thread::sleep_for(chrono::seconds(3));
    
    system("cls");
        
    cad_admin();
} 
else { 
    while(getline(usu, linha)) {
        cout << "Sistema de cadastro | Infra.V.1.0 - Entrar" 
             << endl << "Login admin: ";
        
        cin >> l;
        cin.clear();
        
        cout << "Senha admin: ";
        
        for(i=0;i<6;i++) { 
            cin.clear();
            s[i]=getch ();
            putchar ('*');
        } 
        
        cout << endl << endl << "Aguarde um momento..."; 
        
        this_thread::sleep_for(chrono::seconds(3));
        
        char* linTemp;
        
        linTemp=&linha[0];
        
        if((strcmp(l, linTemp)==0) and (strcmp(s, linTemp)==0)) {
            system ("cls");
            
            menu ();
        }
    } 
}

usu.close();

}

void cad_admin(void) { Usu.open("usu_admin.txt", Ios::out); usu_geral.open("usu_geral.txt", Ios::out);

cout << "Sistema de cadastro | Infra.V.1.0 - Cadastrar"
     << endl << endl << "Login admin: ";
     
cin >> l;
cin.clear();

usu << l << endl;
usu_geral << l << endl;

cout << endl;

do {
    cout << "Senha admin, numerica, 6 digitos!" << endl
        << "Informar senha: ";
     
     for(i=0;i<6;i++) {
        cin.clear();
        senha[i]=getch();
        putchar('*');
     } 
     
   cout << endl << "Confirmar senha: ";
     
     for(i=0;i<6;i++) {
        cin.clear();
        s[i]=getch();
        putchar('*');
    } 
    
    if(strcmp(s, senha)!=0) {
        cout << endl << endl << "As senhas nao coincidem!";
        
        this_thread::sleep_for(chrono::seconds(3));
        
        cout << endl << endl << "Aguarde um momento...";
        
        this_thread::sleep_for(chrono::seconds(3));
        
        cout << endl << endl;
    }
} while(strcmp(s, senha)!=0);

cout << endl << endl << "Aguarde um momento...";

this_thread::sleep_for(chrono::seconds(3));

cout << endl << endl << "Usuario cadastrado com sucesso!";

this_thread::sleep_for(chrono::seconds(3));

usu << senha << endl << endl;
usu_geral << senha << endl << endl;

usu.close();
usu_geral.close();

}

void menu(void) { Cout << "Successful access realido!" << Endl << Endl;

system("pause");

}

0

    void comeco(void);
    void entrar(void);
    void cad_admin(void);
    void menu(void);
 
    static char inicio, l[15], login[15], senha[7], s[7]; static int i, cont = 0; 
    static string linha;
    
    fstream usu, usu_geral;
    
    int main() {
        comeco();
    
        return 0;
    }

I highly recommend avoiding this type of construction.

  • a function void f(void) in general it is a disaster. A black hole. Nothing enters, since it has no arguments, nothing leaves since it does not return anything. Use arguments, return values, never use anything global.

  • main() yeah, well, main(). When finished the program. And your program has a single module. Why declare something like static in that context? What difference can it make?

A command like that

        cout << "Sistema de cadastro | Infra.V.1.0 - Entrar"
            << endl << endl << "Usuario admin nao cadastrado!"
            << endl << endl << "Aguarde um momento...";

is the same thing as that

        cout << "\
Sistema de cadastro | Infra.V.1.0 - Entrar\
\n\
\n\
Usuario admin nao cadastrado!\
\n\
\n\
Aguarde um momento...\n";

which is the same thing as that

        cout << "\
Sistema de cadastro | Infra.V.1.0 - Entrar\n\n\
Usuario admin nao cadastrado!\n\n\
Aguarde um momento...\n";

Avoid using endl needlessly and even these << everyone. There’s no need.

Your program seems to have something wrong with the \n just. While reading the password I noticed that there is still a \n in front of the value.

Avoid conio.h. It’s hard to justify in an environment like C++ the use of a library from the '80s that came on 2 diskettes of 1.44MB TOGETHER with the compiler and the Borland C IDE. And it’s derived from another library that came in Apple II!

Do not use system(). It is banned in many places, insecure and does nothing: just sends a string to the shell. It will not learn anything and will not be doing anything. system("entrar em orbita") does not create a program for space racing :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.