-3

I am trying to figure out how to count how many times someone enters one of three letters so I can then display the count.

I am trying to do this in c# visual studio.

for example if someone has to register into a sport where R = running, C = cricket or B = boxing they enter a letter corresponding with the sport they are registering into, then the next person enters a letter for their sport and so on. how do i track the letters entered and display how many times each one has been entered.

thanks for any help I cant seem to find what im looking for anywhere,

  • 1
    Enter via what (a Windows form , website page etc)? How are the letters stored? What have you tried so far? Do you have any code examples? Just need a bit more info for someone to help here I feel, cheers. – d219 Aug 21 '20 at 10:59
  • just entering it through visual studio, – timelapse_pirate Aug 21 '20 at 11:06
  • My first thought is that you are solving this the wrong way. Maybe use checkbox for each sport, then users may select their sport without typing. With this, you will avoid problems like this. – Roar S. Aug 21 '20 at 11:07
  • 1
    Im not sure weather I have to convert the letters to an integer or is there a way to specify the R, C, B as a variable and then have it determine which it reads and then add one to a count. I will try add some attempted code, my bad. – timelapse_pirate Aug 21 '20 at 11:08
  • unfortunately I have to be able to count how many based on the letter the user enters, that does sound like a neater solution, im not that advanced though – timelapse_pirate Aug 21 '20 at 11:09
  • FYI: Visual Studio is a development environment that you can use to develop all sorts of applications (desktop, mobile, web and more) - so what is it that you are building? – Hans Kesting Aug 21 '20 at 11:30
  • There is no way to declare a variable in such a way that it automatically knows to count all inputs of a particular letter. You will have to provide some general input, then check the value for the letters *you* are interested in – Hans Kesting Aug 21 '20 at 11:32
  • 1
    Is this a real application or homework? A real one needs to be more robust than homework, which can get by with just storing values in static variables that get lost when the app restarts – Hans Kesting Aug 21 '20 at 11:34
  • Just homework, so the variable getting lost isn't an issue. I just wasn't sure what to declare the variable as and what to convert it into from the string input. Im starting to see where im going wrong. Is there any kind of guide online that would explain basic principles of c# i couldnt find one through a Google search for some reason thanks for all the help people – timelapse_pirate Aug 21 '20 at 22:49

1 Answers1

1

There are many ways to achieve this task, Here is a starting point in one way.
You can create a method (AddCounts()) that accepts a letter as a parameter and advances the count into a globally declared dictionary.

 static void Main(string[] args)
 {
     for (int i = 0; i < 5; i++)
     {
         Console.WriteLine("Enter one Letter and press enter");
         var letter = Console.ReadLine();
         if (letter.Length > 0)
         {   
             AddCounts(letter);
         }
     }

   PrintOutput();
   Console.ReadLine();
 }


 static Dictionary<string, int> LettersCountDictionary = new Dictionary<string, int>();
 public static void AddCounts(string letter)
 {
     if (LettersCountDictionary.ContainsKey(letter))
     {
         LettersCountDictionary[letter]++;
     }
     else
     {
         LettersCountDictionary.Add(letter, 1);
     }
 }

 private static void PrintOutput()
 {
     foreach (KeyValuePair<string, int> itm in LettersCountDictionary)
     {
         Console.WriteLine("Letter: {0} Count: {1}", itm.Key, itm.Value);
     }
 }
d219
  • 2,707
  • 5
  • 31
  • 36
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52
  • 2
    The question is a XY question, and you shouldn't provide a possible solution for this. It will only send him further away into unknown territory. – Roar S. Aug 21 '20 at 11:24
  • I was debating whether to just leave a comment with general direction only, in the end, it turned to be a code example of general principles. – Jonathan Applebaum Aug 21 '20 at 11:37