-1

I am new to C# and trying to create a GPA calculator using a sentinel controlled loop. To end the loop, I want the user to enter an 'x', but it is throwing an exception. I'm pretty sure it is because 'x' is not a double type, but I am not sure how I can make it work. I was using a number to exit before but it kept being added to the gradeTotal. Any suggestions would be great! Thanks!

Code:

class Program
{
    static void Main(string[] args)
    {


        double gradeTotal = 0;
        int[] score = new int[100];
        string inValue;
        int scoreCnt = 0;
        Console.WriteLine("When entering grades, use a 0-4 scale. Remember; 
        A = 4, B = 3, C = 2, D = 1, F = 0");
        Console.WriteLine("Enter grade {0}: ((X to exit)) ", scoreCnt + 1);
        inValue = Console.ReadLine();
        gradeTotal += double.Parse(inValue);//This may be a problem area
        while (inValue != "x")
        {
            if (int.TryParse(inValue, out score[scoreCnt]) == false)
                Console.WriteLine("Invalid data -" + "0 stored in array");
            ++scoreCnt;
            Console.WriteLine("Enter Score{0}: ((X to exit)) ", scoreCnt + 
            1);
            inValue = Console.ReadLine();
            gradeTotal += double.Parse(inValue);//This is a problem area
        }
        Console.WriteLine("The number of scores: " + scoreCnt);
        Console.WriteLine("Your GPA is: " + gradeTotal);//Obviously not the 
        //right calculation, just trying to figure it out
        Console.ReadLine();
    }
}
  • 1
    What exception are you getting? – Andrew Diamond Aug 15 '17 at 18:18
  • 1
    That will happen if any non-numeric key is pressed. Use [Int.TryParse()](https://stackoverflow.com/questions/4804968/how-can-i-validate-console-input-as-integers) instead, that will tell you if its a valid integer. (no need for a double as you are only looking for 0 to 4) – Alex K. Aug 15 '17 at 18:18
  • Please pick duplicate of your choice from results of the search - https://www.bing.com/search?q=c%23+formatexception+double – Alexei Levenkov Aug 15 '17 at 18:21
  • Even when I use int.TryParse() I get "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format." – sandracodes Aug 15 '17 at 18:24
  • Possible duplicate of [C# testing to see if a string is an integer?](https://stackoverflow.com/questions/1752499/c-sharp-testing-to-see-if-a-string-is-an-integer) – Loren_ Aug 15 '17 at 19:23

2 Answers2

0

Least effort

Instead of

gradeTotal += double.Parse(inValue);//This is a problem area

Try

if (inValue == "X") break;
gradeTotal += double.Parse(inValue);

More robust

double d;
var ok = double.TryParse(inValue, out d);
if (!ok) break;
gradeTotal += d;
John Wu
  • 50,556
  • 8
  • 44
  • 80
0

You have zero validation on the inValue before trying to parse it. That's the problem. How you resolve this is up to you. Here's a couple suggestions:

  • wrap the code in a try...catch...

    try {

     grandTotal += double.Parse(inValue);
    

    } catch (Exception e) {

     Console.WriteLine("Invalid input!");
    

    }

  • Use Regular Expressions to validate user input and return error if not a number (System.Text.RegularExpressions.Regex)

daddygames
  • 1,880
  • 1
  • 11
  • 18