6 Replies - 1560 Views - Last Post: 13 April 2012 - 10:10 AM Rate Topic: -----

#1 CODER101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-April 12

C# coding error don't know where

Posted 12 April 2012 - 09:35 PM

i have been working on this for the past 5 days and i can't seem to find my error. this is my code please help me fix it.tnxs.When a random number is generated say 4 then it should display the text for i=4 and it does problem is i want it to display a messege box when random number is selected and the correct answer is pressed.

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 WindowsFormsApplication4
{
    public partial class Form4 : Form
    {
        public bool buttonclicked = true;
        public int i;
        int timeLeft;
        public Form4()
        {
            InitializeComponent();
        }

        private void Timelabel2_Click(object sender, EventArgs e)
        {

        }

        private void Form4_Load(object sender, EventArgs e)
        {
            // Start the timer.
            timeLeft = 30;
            Timelabel2.Text = "30 seconds";
            timer1.Start();
           
            for (int k = 1; k < 16; k++)
            {
                Random random = new Random();
                i = random.Next(1, 16);  //Random number generating function.
                if (i == 1)
                {
                    question.Text = "'Strike and spare' are common terms in which sport?";
                    answer1.Text = "Golf";
                    answer2.Text = "Bowling";
                    answer3.Text = "Boxing";
                    answer4.Text = "Football";
                        
                }

                if (i == 2)
                {
                    question.Text = " What is the color of the sheep used to describe a person who doesn't fit into a family?";
                    answer1.Text = "Black";
                    answer2.Text = "Blue";
                    answer3.Text = "Red";
                    answer4.Text = "Green";

                }
                if (i == 3)
                {
                    question.Text = "  Someone that is special to you is called the what of your eye?";
                    answer1.Text = "Pear";
                    answer2.Text = "Orange";
                    answer3.Text = "Apple";
                    answer4.Text = "Grape";

                }
                if (i == 4)
                {
                    question.Text = "Which letter signifies a failing grade?";
                    answer1.Text = "E";
                    answer2.Text = "F";
                    answer3.Text = "U";
                    answer4.Text = "D";

                }
                if (i == 5)
                {
                    question.Text = "Which actor won the Academey Awardfor his role as Forest Gump?";
                    answer1.Text = "Alec Baldwin";
                    answer2.Text = "Tom Hanks";
                    answer3.Text = "Tom Cruise";
                    answer4.Text = "Denzel Washington";

                }
                if (i == 6)
                {
                    question.Text = "  What was the house that Hansel and Gretel found made of?";
                    answer1.Text = "Chocolate";
                    answer2.Text = "Candy";
                    answer3.Text = "Ginger Bread";
                    answer4.Text = "Concrete";

                }
                if (i == 7)
                {

                    question.Text = "What might someone call a psychiatrist?";
                    answer1.Text = "Shrew";
                    answer2.Text = "Shoe";
                    answer3.Text = "Miracle Worker";
                    answer4.Text = "Shrink";

                }
                if (i == 8)
                {
                    question.Text = "At which event would an artist win an album of the year award?";
                    answer1.Text = "Grammy Awards";
                    answer2.Text = "Emmy Awards";
                    answer3.Text = "Tony Awards";
                    answer4.Text = "Academy Awards";

                }
                if (i == 9)
                {
                    question.Text = "Which artist recorded the 1982 album 'Thriller' ?";
                    answer1.Text = "Elton John";
                    answer2.Text = "Madonna";
                    answer3.Text = "Michael Jackson";
                    answer4.Text = "Paul McCartney";

                }
                if (i == 10)
                {
                    question.Text = "In baseball,how many strikes is a batter allowed?";
                    answer1.Text = "one";
                    answer2.Text = "four";
                    answer3.Text = "two";
                    answer4.Text = "three";

                }
                if (i == 11)
                {
                    question.Text = "Which game show would have contestants guess popular responses to survey questions?";
                    answer1.Text = "Jeoparady";
                    answer2.Text = "The Price is Right";
                    answer3.Text = "Wheel of fortune";
                    answer4.Text = "Family Fued";

                }
                if (i == 12)
                {
                    question.Text = "In the children's nursery rhyme,who did the 'Three blind Mice'all run after?";
                    answer1.Text = "J.K.Rowling";
                    answer2.Text = "The Farmer's Wife";
                    answer3.Text = "The Farmer";
                    answer4.Text = "The Blind Mice";

                }
                if (i == 13)
                {
                    question.Text = "What vegetable gives 'Popeye' superhuman strength?";
                    answer1.Text = "Carrot";
                    answer2.Text = "Spinach";
                    answer3.Text = "Cabbage";
                    answer4.Text = "Lettuce";

                }
                if (i == 14)
                {
                    question.Text = "The hand held pointing device for computers is named after what rodent?";
                    answer1.Text = "Rat";
                    answer2.Text = "Hamster";
                    answer3.Text = "Mouse";
                    answer4.Text = "Guinniepig";

                }
                if (i == 15)
                {
                    question.Text = "Someone who suddenly stops smoking is said to quit how?";
                    answer1.Text = "Hot Turkey";
                    answer2.Text = "Hot Chicken";
                    answer3.Text = "Cold Turkey";
                    answer4.Text = "Cold Chicken";
                }

            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timeLeft > 0)
            {
                // Display the new time left
                // by updating the Time Left label.
                timeLeft = timeLeft - 1;
                Timelabel2.Text = timeLeft + " seconds";
            }
            else
            {
                // If the user ran out of time, stop the timer
                timer1.Stop();


            }
        }

        private void question_Click(object sender, EventArgs e)
        {
           
            }   

        private void button1_Click(object sender, EventArgs e)
        {
           
            [b]if (i == 1)
            { 
                buttonclicked= true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }
            }
            if (i == 2)
            {
                 buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");

                }

            }

            if (i == 15)//don't know how to fix it plz help me correct it.
            {

                buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }
            }
        }

[/b]        private void button3_Click(object sender, EventArgs e)
        {

            if (i == 3)
            {
               buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }
            }

            if (i == 6)
            {
                 buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }

            }

            if (i == 9)
            {
                 buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }

            }

            if (i == 14)
            {
                   buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (i == 4)
            {

                 buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }
            }
            if (i == 5)
            {

                 buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }
            }

            if (i == 10)
            {
                buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }
            }

            if (i == 12)
            {
                buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }

            }
            if (i == 13)
            {
                 buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }

            }

        }

        private void button4_Click(object sender, EventArgs e)
        {

            if (i == 7)
            {
                buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }


            }
            if (i == 8)
            {

                 buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }
            }

            if (i == 11)
            {
                buttonclicked = true;

                if (buttonclicked == true)
                {
                    MessageBox.Show("The button was clicked");
                }

            }

        }

        private void button3_Click_1(object sender, EventArgs e)
        {

        }

        private void button4_Click_1(object sender, EventArgs e)
        {

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            Form5 myNewForm = new Form5();

            myNewForm.ShowDialog();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            Form6 myNEWFORM = new Form6();
            myNEWFORM.ShowDialog();
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: C# coding error don't know where

#2 DimitriV  Icon User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: C# coding error don't know where

Posted 12 April 2012 - 10:08 PM

Please. What are you trying to do? What's the issue with your code?
Was This Post Helpful? 0
  • +
  • -

#3 CODER101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-April 12

Re: C# coding error don't know where

Posted 13 April 2012 - 06:21 AM

after the random generator generates a number say 3 and displays the question and answers asociated with the number. the user will have to choose an answer. i have programmed it in such a way that makes the button the numbers are under the correct answer ie.the button is the correct answer for each of the numbers below. i want the computer to display for the user the message box when the button for the correct answer is pressed.i have also assigned labels answer1
answer2
answer3
answer4 to carry the answers of the questions :whatsthat:
Was This Post Helpful? 0
  • +
  • -

#4 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: C# coding error don't know where

Posted 13 April 2012 - 07:08 AM

I would refactor your code, rethink your code logic, there is no need as far as I can tell to have such a massive chain of IF statements.

If you used Dictionary Collection you could save yourself a lot of lines and just loop through the collection. They work as a key value pair, so by using the Key (for example the question number) you can look up the Value (question answer).

There would be no need to make it so "answer 3" is the answer to "question 3", like you say you want to do now.

This post has been edited by negligible: 13 April 2012 - 07:10 AM

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6530
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: C# coding error don't know where

Posted 13 April 2012 - 08:29 AM

While refactoring would help... Understanding more about coding and objects would be your biggest gain. The idea that you are hard-coding the questions and answers is just horrifying.

90% or more of the problems we see here on homework assignments is because students don't stop and plan nor block out their code into logical segments.
Read this article to get started on the right foot:
This is the 'Help me with my homework' article you wanted.
The last topic in that article is how to design a multiple choice quiz program.


Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

Was This Post Helpful? 2
  • +
  • -

#6 SuperBeast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-April 12

Re: C# coding error don't know where

Posted 13 April 2012 - 09:40 AM

View PosttlhIn`toq, on 13 April 2012 - 08:29 AM, said:

While refactoring would help... Understanding more about coding and objects would be your biggest gain. The idea that you are hard-coding the questions and answers is just horrifying.

90% or more of the problems we see here on homework assignments is because students don't stop and plan nor block out their code into logical segments.
Read this article to get started on the right foot....


I'm reading this article right now, and all I can say is thanks. I've never really sat down and planned out any of my projects. This article is great! You should do more!
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6530
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: C# coding error don't know where

Posted 13 April 2012 - 10:10 AM

Glad it helped!

View PostSuperBeast, on 13 April 2012 - 10:40 AM, said:

You should do more!


As it says on my DIC blog:
Other tutorials I've written that might help you

What does this error message mean?
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Separating data from GUI - PLUS - serializing the data to XML
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1