karollek98
New member
- Joined
- Feb 7, 2021
- Messages
- 2
- Programming Experience
- Beginner
Hi guys! Im a beginner in C# beginning my journey towards a experienced programmer. I am creating a "application" in console that you'll see in my code below. I wanted to make a function to determine whether the user wants to log in or register and use it in main function to execute. But whenever I try to use it in main it does not find the function. The code is below and I'll post a picture with the error. Thank you for help guys and I'll hope i'll be to help in future!
}
}
}
C#:
using System;
using System.Threading;
using System.IO;
namespace ObjektProgrammering
{
class Program
{
static string username;
static string password;
static bool menuOpen = true;
static void Main(string[] args)
{
Console.WriteLine("1. Registrering");
Console.WriteLine("2. Inloggning");
startMeny();
///////////////////////////////////////////////////////////////////////////////////
/* Console.WriteLine("Hej, Välkommen till objektiv programmering. Var vänlig och registrera ett konto nedan :)");
if (!readFromFile(@"C:/Users/karol/OneDrive/Skrivbord/Programmering Läxor/Del 2/login.txt"))
{
registrering();
saveToFile(@"C:/Users/karol/OneDrive/Skrivbord/Programmering Läxor/Del 2/login.txt");
}
inloggning();*/
while (menuOpen)
{
menu();
}
}
static void registrering()
{
Console.Write("Användarnamn: ");
username = Console.ReadLine();
Console.Write("Lösenord: ");
password = Console.ReadLine();
}
static void inloggning()
{
string loginUsername;
string loginPassword;
Console.Clear();
Console.WriteLine("Vänligen logga in");
Console.Write("Användarnamn: ");
loginUsername = Console.ReadLine();
Console.Write("Lösenord: ");
loginPassword = Console.ReadLine();
if (loginUsername != username || loginPassword != password)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Fel användarnamn eller lösenord");
Console.ForegroundColor = ConsoleColor.White;
Thread.Sleep(2500);
inloggning();
}
else
{
Console.Clear();
Console.WriteLine("Inloggning Lyckades");
Thread.Sleep(2500);
Console.Clear();
}
}
static void menu()
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Välkommen till din privata funktionsmeny, vänligen välj en funktion");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("1. Datum o tid");
Console.WriteLine("2. Kalkylator");
Console.WriteLine("0. Exit");
Console.Write("Ditt val: ");
string val = Console.ReadLine();
if (val == "1")
{
datumTid();
}
else if (val == "2")
{
kalkylator();
}
else if(val == "0")
{
exit();
}
else
{
Console.WriteLine("Fel val, försök igen..");
}
}
static void datumTid()
{
Console.Clear();
DateTime datum = DateTime.Now;
Console.WriteLine(datum);
}
static void saveToFile(string path) //Funkcja ktora tworzy plik w wybranej sciezce PATH jezeli plik w sciezce PATH nie istnieje. Jezeli plik istnieje, przepisuje zmienne "username, password" do pliku w sciezce PATH
{
if (!File.Exists(path))
{
FileStream fileStream = File.Create(path);
fileStream.Close();
}
File.WriteAllText(path, username + "\n" + password);
}
static bool readFromFile(string path) // Funkcja ktora przepisuje zawartosc pliku na sciezce PATH do array "lines" potem przepisuje array Lines do zmiennych: username i password zwraca boolian True ktory jest uzywany w IF w funkcji main
{
if (File.Exists(path))
{
string[] lines = File.ReadAllLines(path);
username = lines[0];
password = lines[1];
return true;
}
else
{
return false;
}
}
static void exit ()
{
menuOpen = false;
}
static void kalkylator ()
{
Console.Clear();
Console.Write("Skriv in tal nr. 1: ");
double tal1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Skriv in operator: ");
string op = Console.ReadLine();
Console.Write("Skriv in tal nr. 2: ");
double tal2 = Convert.ToDouble(Console.ReadLine());
if(op == "+")
{
Console.WriteLine(tal1 + tal2 + "\n" + "Tryck enter för att fortsätta...");
Console.ReadLine();
}
else if (op == "-")
{
Console.WriteLine(tal1 - tal2 + "\n" + "Tryck enter för att fortsätta...");
Console.ReadLine();
}
else if (op == "*")
{
Console.WriteLine(tal1 * tal2 + "\n" + "Tryck enter för att fortsätta...");
Console.ReadLine();
}
else if (op == "/")
{
Console.WriteLine(tal1 / tal2 + "\n" + "Tryck enter för att fortsätta...");
Console.ReadLine();
}
else
{
Console.Clear();
Console.WriteLine("Fel Operator..");
}
static void startMeny ()
{
Console.WriteLine("1. Registrering");
Console.WriteLine("2. Inloggning");
string val = Console.ReadLine();
if (val == "1")
{
registrering();
saveToFile(@"C:/Users/karol/OneDrive/Skrivbord/Programmering Läxor/Del 2/login.txt");
}
else if (val == "2")
{
inloggning();
}
}
}
}
Attachments
Last edited by a moderator: