locked
int / int = 0% ?!

    Question

  • I'm developing a quiz program where the user matches random cities to their destination using a series of buttons.  I've gotten everything to work properly except my percent display result is acting funny and I'm having trouble figuring out why.  Everything works properly if the user keeps getting the answers correct, but as soon as an incorrect answer is entered then the percent drops from 100.00% to 0.00%. 

    The problem line is 118 in the CalculationsAndDisplay() method.  It should be pretty simple, but I'm just not seeing it.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace SSBinset_2._1
    {
      public partial class frmSSBin2_1 : Form
      {
        public frmSSBin2_1()
        {
          InitializeComponent();
        }
    
        decimal percentCorrect = 0m;
        int arrayPosition = 0;
        int cityCount = 176;
        int guessesCorrect = 0;
        int guessesIncorrect = 0;
        int totalAttempts = 0;
    
        //citylist is the answer sheet. citylistrandom is the question sheet
        string[,] answerArray = new string[176, 2];
        string[] cityListRandom = new string[176];
    
        private void frmSSBin2_1_Load(object sender, EventArgs e)
        {
          //http://msdn.microsoft.com/en-us/library/aa720461%28v=vs.71%29.aspx
          try
          {
            int i = 0;
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("C:\\myinput.txt"))
            {
              String line;
              // Read and display lines from the file until the end of 
              // the file is reached.
              while ((line = sr.ReadLine()) != null)
              {
                //define the cell contents
                //MessageBox.Show(line);
                int split = line.IndexOf('\t');
                string city = line.Substring(0, split);
                string destination = line.Substring(split + 1);
                //MessageBox.Show(city + '\n' + destination);
    
                //Populate the array 
                answerArray[i, 0] = city;
                answerArray[i, 1] = destination;
                cityListRandom[i] = city;
                
                i++;
              }
            }
            //select the first city for the test
            cityListRandom = RandomStringArrayTool.RandomizeStrings(cityListRandom);
            txtCity.Text = cityListRandom[0];
    
            CalculationsAndDisplay();
          }
          catch (Exception ex)
          {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            //Console.WriteLine(ex.Message);
            MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "Exception");
          }
    
        }
    
        private void Button_Click(object sender, EventArgs e)
        {
          Button buttonClicked = sender as Button;
          string destinationGuess = buttonClicked.Text;
          
          //compare the city in txtCity.Text to the answer array and get t
          string correctDestination = "";
          for (int i = 0; i <= cityCount; i++)
          {
            if (txtCity.Text == answerArray[i, 0])
            {
              correctDestination = answerArray[i, 1];
              break;
            }
          }
    
          if (destinationGuess == correctDestination)
          {
            guessesCorrect++;
          }
          else
          {
            guessesIncorrect++;
          }
    
          //reset the array position counter if end or cityListRandom array is reached
          if (arrayPosition == cityCount)
            arrayPosition = 0;
          else
            arrayPosition++;
    
          txtCity.Text = cityListRandom[arrayPosition];
          totalAttempts++;
    
          CalculationsAndDisplay();
        }
    
        private void CalculationsAndDisplay()
        {
          try
          {
            percentCorrect = guessesCorrect / totalAttempts;
            
            txtCorrect.Text = guessesCorrect.ToString();
            txtIncorrect.Text = guessesIncorrect.ToString();
            txtPercCorrect.Text = percentCorrect.ToString("p");
            txtTotal.Text = totalAttempts.ToString();
          }
          //shouldonly be needed on form load
          catch (DivideByZeroException)
          {
            txtCorrect.Text = guessesCorrect.ToString();
            txtIncorrect.Text = guessesIncorrect.ToString();
            txtPercCorrect.Text = "0.00 %";
            txtTotal.Text = totalAttempts.ToString();
          }
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
          this.Close();
        }
      }
    }
    
    

    Maybe I've just been staring at this thing for too long, but can anyone see what is going wrong? 

    Sunday, January 09, 2011 2:46 AM

Answers


  • Change one line of code to this:
     
            percentCorrect = (int) guessesCorrect / (decimal) totalAttempts;
    Int divided by Int is always int, and in your case, zero or one.

    --
    Mike
    • Marked as answer by Pvt. Stash Sunday, January 09, 2011 1:46 PM
    Sunday, January 09, 2011 2:58 AM

All replies


  • Change one line of code to this:
     
            percentCorrect = (int) guessesCorrect / (decimal) totalAttempts;
    Int divided by Int is always int, and in your case, zero or one.

    --
    Mike
    • Marked as answer by Pvt. Stash Sunday, January 09, 2011 1:46 PM
    Sunday, January 09, 2011 2:58 AM

  • Sorry, both casts should have been (decimal).

    --
    Mike
    Sunday, January 09, 2011 2:59 AM

  • Change one line of code to this:
     
            percentCorrect = (int) guessesCorrect / (decimal) totalAttempts;
    Int divided by Int is always int, and in your case, zero or one.

    --
    Mike
    Thanks a bunch.  I just woke up this morning and went over my code again and came up with the same answer you did.  The code works now after I convert them to decimals for the calculation. 


    Sunday, January 09, 2011 1:47 PM