S
S
Shoyo2021-08-02 10:10:05
C++ / C#
Shoyo, 2021-08-02 10:10:05

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

1 answer(s)
R
rundll32, 2021-08-02
@Shoyo

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 question

Ask a Question

731 491 924 answers to any question