Hello,

I have a program I am working on that is working, except for the output for the avg. I know NaN can occur when the denominator is a zero, but mine are not zeros. Here is the relevant code.

double avg6 = (grade6tot/cont6Count);    // grade_tot's are declared as double,                            
double avg7 = (grade7tot/cont7Count);   //  cont_count's are declared as int.
double avg8 = (grade8tot/cont8Count);
Console.WriteLine("Grade Level    # of Contribs    Total Amount    Average");
Console.WriteLine("6th Grade          {0}            {1,7:c}          {2,5:c}   ", cont6Count, grade6tot, avg6);
Console.WriteLine("7th Grade          {0}            {1,7:c}          {2,5:c}   ", cont7Count, grade7tot, avg7);
Console.WriteLine("8th Grade          {0}            {1,7:c}          {2,5:c}   ", cont8Count, grade8tot, avg8);

Thanks a bunch.

Recommended Answers

All 4 Replies

It is working perfect with me actually what's the problem you are mentioning? I have tried taking input from user it also is working. Is this the way you are giving input to this program?

double grade6tot, grade7tot, grade8tot;
            int cont6Count, cont7Count, cont8Count;
            Console.WriteLine("Enter Total amount for 6th Grade:");
            grade6tot = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter No. of contribs for 6th Grade:");
            cont6Count = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Total amount for 7th Grade:");
            grade7tot = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter No. of contribs for 7th Grade:");
            cont7Count = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Total amount for 8th Grade:");
            grade8tot = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter No. of contribs for 8th Grade:");
            cont8Count = Convert.ToInt32(Console.ReadLine());

Here is my entire code.

namespace Contributions
{
    class contributionProgram
    {
        static void Main(string[] args)
        {
            int grade = 0;
            int winGrade = 0;
            int cont6Count = 0;
            int cont7Count = 0;
            int cont8Count = 0;
            double contrib = 0;
            double grade6tot = 0;
            double grade7tot = 0;
            double grade8tot = 0;
            double totContrib = 0;
            double avg6 = (grade6tot/cont6Count);
            double avg7 = (grade7tot/cont7Count);
            double avg8 = (grade8tot/cont8Count);
           

            while(grade != 999)
            {
            Console.WriteLine("Please enter your grade (6, 7, or 8)");
            Console.WriteLine("(Enter 999 to quit):");
            grade = Convert.ToInt32(Console.ReadLine());
            if (grade != 999)
            {
                Console.WriteLine("Please enter your contribution: ");
                contrib = Convert.ToDouble(Console.ReadLine());
            }

                switch(grade)
                {   
                    case 6:
                        grade = 6;
                        grade6tot = grade6tot + contrib;
                        cont6Count += 1;
                        break;
                    case 7:
                        grade = 7;
                        grade7tot = grade7tot + contrib;
                        cont7Count += 1;
                        break;
                   case 8:
                        grade = 8;
                        grade8tot = grade8tot + contrib;
                        cont8Count += 1;
                        break;
                   case 999:
                        grade = 999;
                        if (grade6tot > totContrib)
                        {
                            totContrib = grade6tot;
                            winGrade = 6;
                        }

                        if (grade7tot > totContrib)
                        {
                            totContrib = grade7tot;
                            winGrade = 7;
                        }
                        if (grade8tot > totContrib)
                        {
                            totContrib = grade8tot;
                            winGrade = 8;
                        }
                        Console.WriteLine("Grade Level    # of Contribs    Total Amount    Average");
                        Console.WriteLine("6th Grade          {0}            {1,7:c}          {2,5:c}   ", cont6Count, grade6tot, avg6);
                        Console.WriteLine("7th Grade          {0}            {1,7:c}          {2,5:c}   ", cont7Count, grade7tot, avg7);
                        Console.WriteLine("8th Grade          {0}            {1,7:c}          {2,5:c}   ", cont8Count, grade8tot, avg8);                      
                        Console.WriteLine("Congratulations {0}th graders!", winGrade);
                        Console.WriteLine("Your winning contribution was {0:c}", totContrib);
                        break;

                   default:
                    Console.WriteLine("Invalid grade. Try again:  ");
                        break;
                }  //end switch


            }  //end while
        }

    }
}

My output is something like this:
Grade Level # of contrib Total Amount Average
6th Grade 3 $83.00 NaN
etc...

Why am I getting NaN for the division?

because you are calculating your average before taking input from user place this code

double avg6 = (grade6tot/cont6Count);
                         double avg7 = (grade7tot/cont7Count);
                         double avg8 = (grade8tot/cont8Count);

just before

Console.WriteLine("Grade Level    # of Contribs    Total Amount    Average");
                        Console.WriteLine("6th Grade          {0}            {1,7:c}          {2,5:c}   ", cont6Count, grade6tot, avg6);
                        Console.WriteLine("7th Grade          {0}            {1,7:c}          {2,5:c}   ", cont7Count, grade7tot, avg7);
                        Console.WriteLine("8th Grade          {0}            {1,7:c}          {2,5:c}   ", cont8Count, grade8tot, avg8);                      
                        Console.WriteLine("Congratulations {0}th graders!", winGrade);
                        Console.WriteLine("Your winning contribution was {0:c}", totContrib);

Ahhh, Thank you. and thank you for being so quick to answer! I appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.