D
D
DeHaker2021-08-27 22:19:23
C++ / C#
DeHaker, 2021-08-27 22:19:23

Why doesn't cout output a message?

Here is the whole code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

bool IsLoggedIn()
{
  string username, password, un, pw;
  cout << "Enter username: "; cin >> username;
  cout << "Enter password: "; cin >> password;

  ifstream read("D:\\" + username + ".txt");
  getline(read, un);
  getline(read, pw);

  if (un == username && pw == password)
  {
    return true;

  }
  else
  {
    return false;
  }
}


int main()
{
  int choice;

  cout << "1: Register\n2: Login\n Your choice: "; cin >> choice;
  if (choice == 1)
  {
    string username, password;

    cout << "select a username: "; cin >> username;
    cout << "select a password: "; cin >> password;

    ofstream file;
    file.open("D:\\" + username + ".txt");
    file << username << endl << password;
    file.close();

    main();
  }
  else if (choice == 2)
  {
    bool status = IsLoggedIn();

    if (!status)
    cout << "False Login!\n";
    system("pause");
    return 0;
  }
  else
  {
    cout << "Succesfully logged in!\n";
    system("pause");
    return 1;
  }
}
Вроде ведь всё верно написано.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris the Animal, 2021-08-28
@DeHaker

You didn't cause the message to be displayed on success. Cleaned up the code a bit and removed what you can just copy from your code back. If I were you, I would immediately learn to write code so that each function is responsible for some of its own functionality. Don't mix everything in one place.

#ifdef _WIN32
#include <windows.h>
#endif

#include <iostream>
#include <fstream>
#include <string>

bool IsLoggedIn()
{
    using namespace std;

   // ...

    return un == username && pw == password;
}

int main()
{
    using namespace std;

#ifdef _WIN32
    // https://habr.com/ru/sandbox/108750/
    // Устраняет проблемы выводом кириллицы на консоль Windows
    // Файл должен быть сохранён с кодировкой Windows 1251
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
#endif

    int choice;
    do
    {
        cout << "1: Register\n2: Login\n Your choice: ";
        cin >> choice;

        if (choice == 1)
        {
            // ...
        }
        else if (choice == 2)
        {
            bool status = IsLoggedIn();
            if (!status)
            {
                cout << "False Login!\n";
            }
            else
            {
                cout << "Succesfully logged in!\n";
            }

#ifdef _WIN32
            system("pause");
#endif
            return status ? 1 : 0;
        }

    } while (choice == 1);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question