If allows you to execute other unwanted lines

Asked

Viewed 92 times

-4

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

Cds that are stored in a text file. The file must save for each CD, the name of the author/group, CD name, year of edition, publisher name, total time (in minutes) and number of tracks.

The if doesn’t 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");
        }
    }
}
  • 2

    What’s the matter?

  • Is not running if

  • Explains the behavior you want and what’s going on

  • 1

    I tested it here and it works exactly as expected. The immediately following lines of if (those that have Console.Writeline) are conditioned by the values correctly (after all, when if has no { }, it only holds for the next line). If you expected something different, better [Dit] the question and explain exactly your problem, so that the question can be reopened. By the way, enjoy and tidy up the post if you can, it’s easier to read if you don’t mix different languages, upper and lower, and don’t repeat sentences.

1 answer

3


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

There were some small problems, and others more serious as the fact of accepting invalid values typing and letting memory of the streams.

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" working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference. (is not allowed to access filesystem).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.