-2

How can I edit this code, if the user enter wrong char? Like " g " or " H " or anything else, repeat this step again, and don't go to the next step [ I mean ] if I loop for 10 loops, if I enter wrong char it will loop for 9 just

 char grade;      // one grade
 int aCount = 0, // number of As
     bCount = 0, // number of Bs
     cCount = 0, // number of Cs
        dCount = 0, // number of Ds
        fCount = 0; // number of Fs

    for ( int i = 1; i <= 10; i++ )
    {
       Console.Write( "Enter a letter grade: " );
       grade = Char.Parse( Console.ReadLine() );
       switch ( grade )
       {
          case 'A': // grade is uppercase A
          case 'a':  // or lowercase a
             ++aCount;
             break;

          case 'B': // grade is uppercase B
          case 'b':  // or lowercase b
             ++bCount;
             break;

          case 'C':  // grade is uppercase C
          case 'c': // or lowercase c
             ++cCount;
             break;

          case 'D': // grade is uppercase D
          case 'd':  // or lowercase d
             ++dCount;
             break;

          case 'F':  // grade is uppercase F
          case 'f': // or lowercase f
             ++fCount;
             break;
          default:    // processes all other characters
             Console.WriteLine( 
                "Incorrect letter grade entered." +
                "\nEnter a new grade" );
             break;
       } // end switch
    } // end for
    Console.WriteLine( 
       "\nTotals for each letter grade are:\nA: {0}" +
       "\nB: {1}\nC: {2}\nD: {3}\nF: {4}", aCount, bCount,
       cCount, dCount, fCount );
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Ahmed Ibrahim
  • 380
  • 5
  • 22

2 Answers2

0

You could make this a lot more dynamic and easier to read. Plus using the dictionary makes it a lot easier to add another grade without modifying so much code.

However, you can solve your problem by keeping track of an acceptedGradeCount for every grade you accept and running the loop 10 times using that to compare against. Which is what you should do in your code.

You can also use char.ToLower to convert a character to lower case so that you don't need to compare against upper as well.

    //Dictionary of grades with default counts of 0 per grade
    var dict = new Dictionary<char, int>()
    {
        {'a', 0},
        {'b', 0},
        {'c', 0},
        {'d', 0},
        {'f', 0},
    };
    var acceptedGradeCount = 0;
    //While accepted grade count is less than 10
    while (acceptedGradeCount < 10)
    {
        Console.WriteLine("Enter a letter grade: ");
        //Read in the character and convert it to lower case
        var input = char.ToLower(Convert.ToChar(Console.ReadLine()));
        //Determine if the character is a valid grade by seeing if it exists in the dictionary
        if (dict.ContainsKey(input))
        {
            //Add 1 to the dictionary count value for that grade
            dict[input]++;
            acceptedGradeCount++;
        }
        else
        {
            Console.WriteLine("Incorrect letter grade entered. {0}Enter a new grade", Environment.NewLine);
        }
    }

    //Get results string
    var builder = new StringBuilder("Totals for each letter grade are:");
    foreach (KeyValuePair<char, int> keyValuePair in dict)
    {
        builder.Append(string.Format("{0}: {1} ", keyValuePair.Key, keyValuePair.Value));
    }
    //Print Results
    Console.WriteLine(builder.ToString());
    Console.ReadLine();

To stay with your current code, you could add acceptedGradeCount and increment it in each accepted grade. Use a while loop instead of a for also.

    char grade;      // one grade
    int aCount = 0, // number of As
        bCount = 0, // number of Bs
        cCount = 0, // number of Cs
           dCount = 0, // number of Ds
           fCount = 0; // number of Fs

    var acceptedGradeCount = 0;
    while(acceptedGradeCount < 10)
    {
        Console.Write("Enter a letter grade: ");
        grade = char.ToLower(Char.Parse(Console.ReadLine()));
        switch (grade)
        {
            case 'a':  // or lowercase a
                ++aCount;
                acceptedGradeCount++;
                break;

            case 'b':  // or lowercase b
                ++bCount;
                acceptedGradeCount++;
                break;

            case 'c': // or lowercase c
                ++cCount;
                acceptedGradeCount++;
                break;

            case 'd':  // or lowercase d
                ++dCount;
                acceptedGradeCount++;
                break;

            case 'f': // or lowercase f
                ++fCount;
                acceptedGradeCount++;
                break;
            default:    // processes all other characters
                Console.WriteLine(
                   "Incorrect letter grade entered." +
                   "\nEnter a new grade");
                break;
        } // end switch
    } // end for
    Console.WriteLine(
       "\nTotals for each letter grade are:\nA: {0}" +
       "\nB: {1}\nC: {2}\nD: {3}\nF: {4}", aCount, bCount,
       cCount, dCount, fCount);
austin wernli
  • 1,801
  • 12
  • 15
  • hardly i can not understand such this code ! , i still very begainer in c# ! :( – Ahmed Ibrahim Mar 12 '15 at 19:13
  • 1
    I posted your code as well with modifications needed lol. However, it hurts to see painful code that uses a ton of repetition to complete a task, which is why i rewrote for my own sanity – austin wernli Mar 12 '15 at 19:14
  • The second code block should do what you want it to though @AhmedIbrahim – austin wernli Mar 12 '15 at 19:29
  • wernl : yes its working very well , but trying to understand it ,, after modification :D Can you help me , and tell me what is `var` mean ? :D is it int or string or char ? any kind of datetype ? – Ahmed Ibrahim Mar 12 '15 at 19:34
  • var is just an implicit type. You can use int if you want as well, they are both functionally equivalent when used in this context. You can read up more on var at https://msdn.microsoft.com/en-us/library/bb383973.aspx – austin wernli Mar 12 '15 at 19:38
-2

Didn't read the question before answering previously. Appologies. Check this

static void Main(string[] args)
    {
        char grade;      // one grade
        int aCount = 0, // number of As
            bCount = 0, // number of Bs
            cCount = 0, // number of Cs
               dCount = 0, // number of Ds
               fCount = 0; // number of Fs


        AskForChar(ref aCount, ref bCount, ref cCount, ref dCount, ref fCount); // end switch

    // end for
    Console.WriteLine(
       "\nTotals for each letter grade are:\nA: {0}" +
       "\nB: {1}\nC: {2}\nD: {3}\nF: {4}", aCount, bCount,
       cCount, dCount, fCount);
    Console.ReadLine();
}

private static void AskForChar(ref int aCount, ref int bCount, ref int cCount, ref int dCount, ref int fCount)
{
    for (int i = 1; i <= 10; i++)
    {
        char grade;
        Console.Write("Enter a letter grade: ");
        grade = Char.Parse(Console.ReadLine());
        switch (grade)
        {
            case 'A': // grade is uppercase A
            case 'a':  // or lowercase a
                ++aCount;
                break;

            case 'B': // grade is uppercase B
            case 'b':  // or lowercase b
                ++bCount;
                break;

            case 'C':  // grade is uppercase C
            case 'c': // or lowercase c
                ++cCount;
                break;

            case 'D': // grade is uppercase D
            case 'd':  // or lowercase d
                ++dCount;
                break;

            case 'F':  // grade is uppercase F
            case 'f': // or lowercase f
                ++fCount;
                break;
            default:    // processes all other characters
                Console.WriteLine(
                   "Incorrect letter grade entered." +
                   "\nEnter a new grade");
                return;
                break;
        }
    }

}
Gaurav Sharma
  • 586
  • 3
  • 10