0

Here is my simple and newbie program written in C#. I have just tried it for integers but when I tried to input naturally it throws an execption. I wonder how to warn user if any string input occurs without throwing the exception. ( PS: I am an absoulute beginner).

float quiz, mid, final,avg;
Console.WriteLine("Enter quiz score:");
quiz= float.Parse(Console.ReadLine());
Console.WriteLine("Enter mid-term score:");
mid = float.Parse(Console.ReadLine());
Console.WriteLine("Enter final score:");
final = float.Parse(Console.ReadLine());

avg=(quiz+mid+final)/3;

while (avg<=100 && quiz<=100&& mid <=100 && final<=100 )
{

    if (avg >= 90)
        Console.WriteLine("Grade A");

    else if (avg >= 70 && avg < 90)
        Console.WriteLine("Grade B");

    else if (avg >= 50 && avg < 70)
        Console.WriteLine("Grade C");

    else if (avg < 50)
        Console.WriteLine("Grade F");
    else
        Console.Write("invalid operation!!");
    break;
}
while (avg > 100)
{
    Console.WriteLine("Please enter the right scores!");
    //Console.ReadLine();
    break;
}
while (mid > 100)
{
    Console.WriteLine("Please enter the right scores!");
    //Console.ReadLine();
    break;
}
while (final > 100)
{
    Console.WriteLine("Please enter the right scores!");
    //Console.ReadLine();
    break;
}

while (quiz > 100)
{
    Console.WriteLine("Please enter the right scores!");
    //Console.ReadLine();
    break;
}
Console.ReadLine();
3
  • Why not try to convert the strings to integers first? Commented Jun 29, 2015 at 12:28
  • Actually there must not be any string input according to flow logic.I unsurely think that there is nothing to convert from string into integers OR I prolly do not understand what you mean. Could you explain it? :) Commented Jun 29, 2015 at 13:12
  • ahhh old newbie days :) Commented Feb 27, 2019 at 12:23

2 Answers 2

1

You could try this:

bool isNumeric;
int i = -1;
string str = Console.ReadLine();

isNumeric = int.TryParse(str, out i);
if(isNumeric == false)
    Console.WriteLine("Not an integer");

Taken from here

Sign up to request clarification or add additional context in comments.

Comments

0

I have just implemented the "isNumeric" solution and encountered a problem with integer definition cuz I use float type variables. I re-arranged and modified my codes. Here is the last working version :

bool isNumeric;
        float i;
        string str;
        string name = "";
        float quiz=0;
        float mid=0;
        float final=0;
        float avg=0;


        Console.WriteLine("Please enter your name:");
        name = Console.ReadLine();

        while (true)
        {
            Console.WriteLine("Enter quiz score:");



            str = Console.ReadLine();
            isNumeric = float.TryParse(str, out i);



            if (isNumeric)
            {

                quiz = float.Parse(str);
                if (quiz>100)
                {
                    Console.WriteLine("Please enter the right scores!");
                }
                else
                {
                    break;
                }

            }
            else
            {

                Console.WriteLine("Not an integer");


            }

        }

        while (true)
        {
            Console.WriteLine("Enter mid-term score:");
            str = Console.ReadLine();
            isNumeric = float.TryParse(str, out i);
            if (isNumeric == false)
            {
                Console.WriteLine("Not an integer");
            }
            else
            {
                mid = float.Parse(str);
                if(mid>100)
                {
                    Console.WriteLine("please input the right score");
                }
                else
                {
                    break;
                    }


            }
        }

        while (true)
        {
            Console.WriteLine("Enter final score:");
            str = Console.ReadLine();
            isNumeric = float.TryParse(str, out i);
            if (isNumeric == false)
            {
                Console.WriteLine("Not an integer");
            }
            else
            {
                final = float.Parse(str);
                if (final > 100)
                {
                    Console.WriteLine("Please enter the right scores!");
                }
                else
                {
                    break;
                }
            }
        }

        avg = (quiz + mid + final) / 3;

        while (avg <= 100 && quiz <= 100 && mid <= 100 && final <= 100)
        {

            if (avg >= 90)
                Console.WriteLine(name+" 's Grade is A");

            else if (avg >= 70 && avg < 90)
                Console.WriteLine(name+ " 's Grade is B");

            else if (avg >= 50 && avg < 70)
                Console.WriteLine(name+ "Grade C");

            else if (avg < 50)
                Console.WriteLine("Grade F");
            else
                Console.Write("invalid operation!!");
            break;
        }


        Console.ReadLine(); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.