If you can run other undesirable lines [closed]

-4

I want to make a program that allows you to manipulate and manage information about

CDs that is stored in a text file. The file should save for each CD, author / group name, CD name, year of edit, publisher name, total time (in minutes) and number of tracks.

The if does not work because?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string line;
            int valor;
            string input;
            string[] names = new string[6];
            Console.WriteLine("Escolha:\n1 - adicionar\n2 - Visualizar\n 3 - Sair");
            input = Console.ReadLine();
            valor = Int32.Parse(input);
            if (valor == 1)
                Console.WriteLine("Escreva o nome do autor/grupo, nome do CD, ano de edição, nome da editora, total de tempo e número de faixas: ");
                for (int i = 0; i < 6; i++)
                {
                    names[i] = Console.ReadLine();
                }


                StreamWriter SW = new StreamWriter(@"C:\Users\gabri\Desktop\trabalho.txt");

                for (int i = 0; i < 6; i++)
                {
                    SW.WriteLine(names[i]);
                }

                SW.Close();
            if (valor == 2)

                Console.WriteLine("FICHEIRO .txt: \n");

                StreamReader reader = File.OpenText(@"C:\Users\gabri\Desktop\trabalho.txt");
                while ((line = reader.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
                Console.ReadKey();
            if (valor == 3)
                Console.WriteLine("Adeus");
        }
    }
}
    
asked by anonymous 14.10.2016 / 18:29

1 answer

2

The code has several problems, the main one being what Bacco said in comments. Since there are no keys in the command block the if will only selectively execute the first line after if , the rest will always run. Lesson to be learned: Always use keys, even if you only need one line and avoid confusion.

There were some minor issues, and some more serious ones such as accepting invalid values and letting streams' memory leak.

using static System.Console;
using System.IO;

namespace ConsoleApplication1 {
    public class Program {
        public static void Main(string[] args) {
            string[] names = new string[6];
            WriteLine("Escolha:\n1 - adicionar\n2 - Visualizar\n 3 - Sair");
            var valor = 0;
            if (!int.TryParse(ReadLine(), out valor)) {
                WriteLine("Opção inválida");
            }
            if (valor == 1) {
                WriteLine("Escreva o nome do autor/grupo, nome do CD, ano de edição, nome da editora, total de tempo e número de faixas: ");
                for (int i = 0; i < 6; i++) {
                    names[i] = ReadLine();
                }
                using (var sw = new StreamWriter(@"C:\Users\gabri\Desktop\trabalho.txt")) {
                    for (int i = 0; i < 6; i++) {
                        sw.WriteLine(names[i]);
                    }
                }
            }
            if (valor == 2) {
                WriteLine("FICHEIRO .txt: \n");
                using (var reader = File.OpenText(@"C:\Users\gabri\Desktop\trabalho.txt")) {
                    string line;
                    while ((line = reader.ReadLine()) != null) {
                        WriteLine(line);
                    }
                }
                ReadKey();
            }
            if (valor == 3) {
                WriteLine("Adeus");
            }
        }
    }
}

See almost running on dotNetFiddle (you are not allowed to access filesystem ).

    
15.10.2016 / 17:50