Hello:)
I am asking the user to input a numerical grade into one textbox and then I want to show the corrosponding grade in the textbox below when they push a 'calculate' button. I have put it the code below and I am puzzled because I have listed 6 conditionals based on grade - but the only thing that will output is the data validation bit "Invalid 1-100 please". This outputs whatever number I put in! So doing my utmost not to get into a demented state I am trying to work this out logically and the only difference between the code that is kind enough to output, and the bit that isn't seems to be the brackets that hold the numeric conditions - but I have no errors - it took me a day to get it to output at all, and now I'm derailed :icon_eek: Could someone put me back on track please.

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;

namespace Task1._2_gradeConverter
{
/** 
*Program created October 2010
*This program allows the user to enter a numerical grade from 0 to 100, click the convert 
*or enter botton, and be astonished when the application calculates the letter score that
*corrosponds with the numerical *data. It then displays the result in a label on the form.
**/
    public partial class Form1 : Form
    {
        int numGrade = 0;
        //string letGrade; do I need a string for the letter Grade variable ???
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
         
            
            if((numGrade>87) && (numGrade>101))
            {
                textBox2.Text = numGrade.ToString("A");//Output A in Letter Grade textbox
            }

            else if((numGrade>79) && (numGrade>88))
            {
                textBox2.Text = numGrade.ToString("B");//Output B in Letter Grade textbox
            }  
 
            else if((numGrade>66) && (numGrade>80))
            {
                textBox2.Text = numGrade.ToString("C");//Output C in Letter Grade textbox
            } 
 
            else if((numGrade>59) && (numGrade>68))
            {
                textBox2.Text = numGrade.ToString("D");//Output D in Letter Grade textbox
            }

            else if ((numGrade > 62) && (numGrade > 0))
            {
                textBox2.Text = numGrade.ToString("F");//Output F in Letter Grade textbox
            }

            else
            {
                textBox2.Text = numGrade.ToString("Invalid 1-100 please");
            }
     

        }

        private void label2_Click(object sender, EventArgs e)
        {
            //What about the Exit command- how on earth do I get this to close the program?????
        }
    }
}

Recommended Answers

All 6 Replies

if((numGrade>87) && (numGrade>101))Look carefully at this statement from line 36.
This will only be true if numGrade is > 102!
Hope this helps.

commented: Nice hint +1
commented: Thanks for waking me up! +1

Well i see a few things that seem to be missing,

1) What ddanbe said, but on all of them, to see if its between should be checking for > and <

2) I dont see where you set numGrade to anything other then 0.
at the top of your button1_Click method i would expect to see like
numGrade = textbox.Text;

*just an observation, but don't need to use numGrade.ToString("F") to set the text of a textbox. just use textbox1.text = "F";

I don't want to give too much detail, cause this just has that, its home work feel ;)

commented: Great prompting thank you +1
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;

namespace Task1._2_gradeConverter
{
/** 
*Program created October 2010
*This program allows the user to enter a numerical grade from 0 to 100, click the convert 
*or enter botton, and be astonished when the application calculates the letter score that
*corrosponds with the numerical *data. It then displays the result in a label on the form.
**/
    public partial class Form1 : Form
    {
        int numGrade = 0;
        //string letGrade; do I need a string for the letter Grade variable ???
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
         numGrade=textBox1.text.toString();

            if((numGrade>87) && (numGrade<=100))
            {
                textBox2.Text = "A"; //Output A in Letter Grade textbox
            }

            else if((numGrade>79) && (numGrade<=87))
            {
                textBox2.Text = "B";//Output B in Letter Grade textbox
            }  

            else if((numGrade>66) && (numGrade<=79))
            {
                textBox2.Text = "C";//Output C in Letter Grade textbox
            } 

            else if((numGrade>59) && (numGrade<=66))
            {
                textBox2.Text = "D";//Output D in Letter Grade textbox
            }

            else if ((numGrade <=59) && (numGrade >= 0))
            {
                textBox2.Text = "F";//Output F in Letter Grade textbox
            }

            else
            {
                textBox2.Text = numGrade.ToString("Invalid 1-100 please");
            }


        }

           }
}
commented: Very clear - thank you +1

if((numGrade>87) && (numGrade>101))Look carefully at this statement from line 36.
This will only be true if numGrade is > 102!
Hope this helps.

Yes it helped enourmously thank you - after I'd finished kicking myself round the room (metaphorically of course)- especially seeing as I wrote them out correctly on paper- honest. Stops my ego getting too big this coding:) Thanks again.

Well i see a few things that seem to be missing,

1) What ddanbe said, but on all of them, to see if its between should be checking for > and <

2) I dont see where you set numGrade to anything other then 0.
at the top of your button1_Click method i would expect to see like
numGrade = textbox.Text;

*just an observation, but don't need to use numGrade.ToString("F") to set the text of a textbox. just use textbox1.text = "F";

I don't want to give too much detail, cause this just has that, its home work feel ;)

Hi thanks for that. I'm quarter of a century too late for homework, but it is for an assignment, so you were on the right lines and I appreciate the approach as I am trying to work it out for myself:). Although I did some Java last year I've only just statred with C# and Visual Studio.
Following your prompt I got it to work with

int numGrade = int.Parse(numGradBox.Text);

in the end, and following your advice managed to cut the code down enormously so even I can work out what is happening;) Thank You.

start quote:

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;

namespace Task1._2_gradeConverter
{
/** 
*Program created October 2010
*This program allows the user to enter a numerical grade from 0 to 100, click the convert 
*or enter botton, and be astonished when the application calculates the letter score that
*corrosponds with the numerical *data. It then displays the result in a label on the form.
**/
    public partial class Form1 : Form
    {
        int numGrade = 0;
        //string letGrade; do I need a string for the letter Grade variable ???
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
         numGrade=textBox1.text.toString();

            if((numGrade>87) && (numGrade<=100))
            {
                textBox2.Text = "A"; //Output A in Letter Grade textbox
            }

            else if((numGrade>79) && (numGrade<=87))
            {
                textBox2.Text = "B";//Output B in Letter Grade textbox
            }  

            else if((numGrade>66) && (numGrade<=79))
            {
                textBox2.Text = "C";//Output C in Letter Grade textbox
            } 

            else if((numGrade>59) && (numGrade<=66))
            {
                textBox2.Text = "D";//Output D in Letter Grade textbox
            }

            else if ((numGrade <=59) && (numGrade >= 0))
            {
                textBox2.Text = "F";//Output F in Letter Grade textbox
            }

            else
            {
                textBox2.Text = numGrade.ToString("Invalid 1-100 please");
            }


        }

           }
} 

end quote.

Thank you - I'd just got it before I read your reply - but this has reinforced where I should be returning - ie textBox2, and has made things clearer. Very much appreciated. I now have it working on the convert button and when I press enter. The exit button even works - so I'm very happy:) Thanks very much.

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.