1

I wrote a code that takes input data, writes it on a string then it is sent to a temp file that gets encrypted and the temp sends the encrypted file to the main file. Somehow I managed to make it work (only the register) but I didn't do anything to the code and now the data isn't written on the files. Any kind of help will be welcomed. Thanks!

#include<iostream>
#include<istream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>


using namespace std;

void login();
void reg();
void encrypt();

int main() {

    int rep;
    cout << "\t\t\t Welcome to Protek 0.1.\n" << "\t\t\t Do you already have a account?\n\n";
    cout << "\t\t\t\t 1 = YES    2 = NO\n";
    cin >> rep;

    
    switch (rep) {
        case 1:
        login();
        break;

        case 2:
        reg();
        break;

        default:
        break;
    }

 
}

void login() {
    int count;
    string LUserN, LUserP, LN, LP;

    system("cls");


    cout << "Please enter your name.";
    cin >> LUserN;

    cout << "Please enter your pass.";
    cin >> LUserP;

    ifstream input("data/pn.txt");
    while (input>>LN>>LP) {
        if (LN==LUserN && LP==LUserP) {
            count = 1;
     }
    }
    input.close();

    if (count == 1) {

        cout << "\t\t\t SUCCESS! \n";
        cin.get();
        cin.get();
        main();
    }

    else {
        cout << "\t\t\t ERROR";
        main();
    }
}

void reg() {
    string RUserN, RUserP;
    system("cls");

    cout << "\t\t\t Enter your username. \n";
    cin >> RUserN;

    cout << "\t\t\t Enter pass. \n";
    cin >> RUserP;

    encrypt();

    cout<< "\t\t\t Reg Complete!\n";
    main();


}

void encrypt() {
    char ch;
    fstream fps, fpt;
    fps.open("data/pn.txt", fstream::in);
    fpt.open("data/pt.txt", fstream::out);

    while (fps >> ch) {
        ch = ch * 22;
        fpt << ch;
    }

    fps.close();
    fpt.close();
    
 fps.open("data/pn.txt", fstream::out);
    fpt.open("data/pt.txt", fstream::in);

    while (fpt >> ch) 
        fps << ch;
        fpt.close();
        fps.close();

 
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Cosmin909
  • 21
  • 1
  • 2
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mre], with emphasis on the *minimal* part. If you have a problem writing to a file, then create a small and simple program that *only* writes to a file. If that works, then the problem you have is something else. – Some programmer dude Apr 12 '22 at 05:56
  • 2
    On another note, in C++ it's *invalid* to call [the `main` function](https://en.cppreference.com/w/cpp/language/main_function) yourself. If you need a loop, use an actual loop. – Some programmer dude Apr 12 '22 at 05:57
  • 1
    Calling `main()` causes undefined behavior. [Can main function call itself in C++?](https://stackoverflow.com/questions/2128321/can-main-function-call-itself-in-c) – Retired Ninja Apr 12 '22 at 05:58
  • I fail to see where you are checking, whether opening the files was actually successful. Opening a file may fail for writing if for example the directory does not exist. – Refugnic Eternium Apr 12 '22 at 05:58
  • You are also using uninitialized variables (what value does `count` in `login` have if username/password *don't* match, i.e. the `if` in the loop is never entered?) Then there's the problem of your "encryption" not doing anything useful, and having undefined behavior (integer overflow) if `char` is signed. – Sebastian Redl Apr 12 '22 at 07:22

0 Answers0