Answer the question
In order to leave comments, you need to log in
How to make a C# phone book?
The user enters his number into the console. The program writes it to a txt file. If the file already has this number, then the program should generate an error. If there are less than 12 characters in the number, then the program should give an error
Answer the question
In order to leave comments, you need to log in
You can do something like this. You need to include the System.IO namespace.
string input = Console.ReadLine().Trin();
if (input.Length < 12) {
Console.WriteLine("Ошибка!"); // Можно заменить на throw new Exception("Текст ошибки");
} else {
string numbers = File.ReadAllLines(@"путь_к_файлу.txt");
if (numbers.Contains(input)) {
Console.WriteLine("Номер уже существует!");
} else {
File.AppendAllText(@"путь_к_файлу.txt", input + Environment.NewLine);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question