R
R
RG06092021-08-28 20:22:04
C++ / C#
RG0609, 2021-08-28 20:22:04

I do not understand how to implement the program correctly?

I do not understand how to implement the program correctly. I want the program to read the value from the file (for example, 2)
and pass this number to the langsave variable .. But it can only read in string, while it is not possible to transfer a value from a string to an int. Help, my brain is already boiling.
Oh yes. Sorry for the shield code. I'm just starting.

the code

#include <iostream>
#include <Windows.h>
#include <fstream>
#include <string>

using namespace std;

string login;
string pass;
string login2;
string pass2;
ofstream fout;
ifstream fin;
int menu;
int lang;
int langsave = 0;
string langsave1;

int main()
{
  back:
  setlocale(LC_ALL, "Rus");
  string langpath = "D:\\RGWare Loader\\lang.txt";
  fin.open(langpath);
  getline(fin, langsave1);
  langsave = langsave1;
  if (langsave == 0)
  {
    
    cout << "Select language:\n";
    cout << "1. Русский\n";
    cout << "2. English\n";
    cout << "Select: ";
    cin >> lang;
    string langpath = "D:\\RGWare Loader\\lang.txt";
    fout.open(langpath);
    fout << lang;
    fout.close();
  }
  langsave = lang;
  if (lang == 2)
  {
    cout << "1. Authorization\n";
    cout << "2. Registration\n";
    cout << "Select: ";
    cin >> menu;
    if (menu == 2)
    {
      cout << "login: ";
      cin >> login;
      cout << "pass: ";
      cin >> pass;
      string path = "D:\\RGWare Loader\\" + login + ".txt";
      fout.open(path);
      if (fout.is_open())
      {
        cout << "File open: Success!\n";
      }
      else
      {
        cout << "File open: Error!\n";
        system("pause>nul");
        return 0;
      }
      fout << login;
      fout << "\n";
      fout << pass;
      fout.close();
      cout << "File close: Success!\n";
      cout << endl;
      goto back;
      system("pause>nul");
      return 0;
    }
    else if (menu == 1)
    {
      cout << "login: ";
      cin >> login;
      cout << "pass: ";
      cin >> pass;
      string path = "D:\\RGWare Loader\\" + login + ".txt";
      fin.open(path);
      if (fin.is_open())
      {
        cout << "File open: Success!\n";
      }
      else
      {
        cout << "File open: Error!\n";
        system("pause");
        return 0;
      }
      getline(fin, login2);
      getline(fin, pass2);
      if (login == login2 && pass == pass2)
      {
        cout << "Authorization: Success!\n";
      }
      else
      {
        cout << "Authorization: Error!\n";
        system("pause>nul");
        return 0;
      }
      fin.close();
      cout << "File close: Success!\n";
      cout << endl;
      goto back;
      system("pause>nul");
      return 0;
    }
  }
  else if (lang == 1)
  {
    cout << "1. Авторизация\n";
    cout << "2. Регистрация\n";
    cout << "Выбор: ";
    cin >> menu;
    if (menu == 2)
    {
      cout << "логин: ";
      cin >> login;
      cout << "пароль: ";
      cin >> pass;
      string path = "D:\\RGWare Loader\\" + login + ".txt";
      fout.open(path);
      if (fout.is_open())
      {
        cout << "Открытие файла: Успешно!\n";
      }
      else
      {
        cout << "Открытие файла: Ошибка!\n";
        system("pause>nul");
        return 0;
      }
      fout << login;
      fout << "\n";
      fout << pass;
      fout.close();
      cout << "Закрытие файла: Успешно!\n";
      cout << endl;
      goto back;
      system("pause>nul");
      return 0;
    }
    else if (menu == 1)
    {
      cout << "логин: ";
      cin >> login;
      cout << "пароль: ";
      cin >> pass;
      string path = "D:\\RGWare Loader\\" + login + ".txt";
      fin.open(path);
      if (fin.is_open())
      {
        cout << "Открытие файла: Успешно!\n";
      }
      else
      {
        cout << "Открытие файла: Ошибка!\n";
        system("pause>nul");
        return 0;
      }
      getline(fin, login2);
      getline(fin, pass2);
      if (login == login2 && pass == pass2)
      {
        cout << "Авторизация: Успешно!\n";
      }
      else
      {
        cout << "Авторизация: Ошибка!\n";
        system("pause>nul");
        return 0;
      }
      fin.close();
      cout << "Закрытие файла: Успешно!\n";
      cout << endl;
      goto back;
      system("pause>nul");
      return 0;
    }
  }
  else
  {
    cout << "Wrong language!\n";
    goto back;
  }
  system("pause>nul");
  return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wataru, 2021-08-28
@wataru

Firstly, you can perfectly read a number from a file at once. How do you enter numbers from the console? Here is exactly the same, only from the file. Are you reading a line? Here, pass there not a string, but a conditional int. Well, also, if you read scanf, then you need to transfer % d instead of % s there. And so there are also functions for converting a number to a string. Read help on atoi, sscanf, stringstream.

C
cunion, 2021-08-28
@0hquazEd

std::atoi(some_string.c_str())
Even though it won't work right

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question