11 Replies - 352 Views - Last Post: 26 September 2013 - 06:42 AM Rate Topic: -----

#1 Uthiana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-September 13

Flag Quiz (Need Help Please!)

Posted 24 September 2013 - 10:15 PM

Hi guys,

So for this semester we were given a series of programs to code, i have successfully completed most of it expect 3, but at this point of time i would really appreciate any help with the flag quiz.

With this program we were asked to write code so that a picture of a flag would pop up and you would have to type in the country's name and then press the check button to check the answer and then for it to add points to your score, after checking the answer you would then have to press the next button to generate a new random flag.

So far what I have gathered from it is that I'd have to create an array of images as well as declare a random.

Im really confused about how to put the flags images into an array. Any help would be appreciated, i do not expect anyone to do my assignment for me. thanks for any help in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Flag Quiz (Need Help Please!)

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 486
  • Joined: 13-June 12

Re: Flag Quiz (Need Help Please!)

Posted 24 September 2013 - 10:53 PM

Are you going to have them locally stored as files? You could use the Image.FromFile() method. However, I'd recommend adding them as resources and embedding them in your program, so they are not required to be on the users' computer (plus filenames won't give away the answers!).

For your array, if you're wondering, it should probably be of type Image.
Was This Post Helpful? 0
  • +
  • -

#3 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: Flag Quiz (Need Help Please!)

Posted 25 September 2013 - 03:13 AM

I'd create a special class for the answer/picture pair. With a method that checks the answer and returns a true/false if the answer is correct or not. The actual answer would remain private. You could also make a constructor that accepts the image as a parameter. You could embed a string/image pair in the resources, in that way that the first string corresponds to the first image. The strings are the answers to the quiz.
Some pseudo-code that might get you going:

class Flag
{

constructor (image that you need to pass)
{
set the private variable that holds the image to the one that's passed to the constructor;
}

public bool check (the entered answer)
{
see if the entered answer corresponds to the correct answer in the resources;
}

}



Suggested Reading:

How to embed and access resources by using Visual C#

This post has been edited by RexGrammer: 25 September 2013 - 03:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 995
  • View blog
  • Posts: 2,386
  • Joined: 04-October 09

Re: Flag Quiz (Need Help Please!)

Posted 25 September 2013 - 09:16 AM

View PostRexGrammer, on 25 September 2013 - 03:13 AM, said:


There is an error on that page, step 7 should read "Repeat steps 5 and 6 for the other file." not "Repeat steps 4 and 5 for the other file." :)
Was This Post Helpful? 0
  • +
  • -

#5 Uthiana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-September 13

Re: Flag Quiz (Need Help Please!)

Posted 25 September 2013 - 08:49 PM

Thanks for all the replies! :)/>/> really helped,
I put all the flag images into the resources and im not sure how to make it match to the captical cities,
i put all the capital cites into an array as well.

So far i have this for my check button:
//declared my string of capitals
 string[] capital = new string[10] { "Canberra", "Beijing", "New Delhi", "Jakarta", "Toyko", "Kuala Lumpar", "Islamabad", "Singapore", "Bangkok", "Hanoi" };
            string input;
            Image[] images = new Image[10];
            images[0] = Properties.Resources.Australia;
            images[1] = Properties.Resources.China;
            images[2] = Properties.Resources.India;
            images[3] = Properties.Resources.Indonesia;
            images[4] = Properties.Resources.Japan;
            images[5] = Properties.Resources.Malaysia;
            images[6] = Properties.Resources.Pakistan;
            images[7] = Properties.Resources.Singapore;
            images[8] = Properties.Resources.Thailand;
            images[9] = Properties.Resources.Vietnam;
            input = (txtInput.Text).ToString();
           
            int score = 0;
            if (picFlag.Image == images[i])
            {
                if (input == capital[i])
                {
                    score++;
                    lblScore.Text = score.ToString();

                }
            }



when i press the check button its not doing anything.

This post has been edited by Skydiver: 25 September 2013 - 09:25 PM
Reason for edit:: Fixed code tags.

Was This Post Helpful? 0
  • +
  • -

#6 Uthiana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-September 13

Re: Flag Quiz (Need Help Please!)

Posted 25 September 2013 - 08:59 PM

I also tried
for (int i = 0; i < 20; i++)
            {
                if (picFlag.Image == images[i])
                {
                    if (input == capital[i])
                    {
                        score++;
                        lblScore.Text = score.ToString();

                    }
                }
            }


but its still not working, would it be this line:
if (picFlag.Image == images[i])

This post has been edited by Skydiver: 25 September 2013 - 09:25 PM
Reason for edit:: Fixed code tags.

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3165
  • View blog
  • Posts: 9,560
  • Joined: 05-May 12

Re: Flag Quiz (Need Help Please!)

Posted 25 September 2013 - 09:24 PM

I appreciate the attempt at putting in code tags, but please look closely on the formatting. It is open square bracket, the word "code", and then close square bracket for the beginning of the code. For the end of the code, it is open square bracket, the string "/code", and the close square bracket.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3165
  • View blog
  • Posts: 9,560
  • Joined: 05-May 12

Re: Flag Quiz (Need Help Please!)

Posted 25 September 2013 - 09:29 PM

How are you setting picFlag.Image? Are you using the same resources?
Was This Post Helpful? 0
  • +
  • -

#9 Uthiana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-September 13

Re: Flag Quiz (Need Help Please!)

Posted 25 September 2013 - 09:41 PM

View PostSkydiver, on 25 September 2013 - 09:24 PM, said:

I appreciate the attempt at putting in code tags, but please look closely on the formatting. It is open square bracket, the word "code", and then close square bracket for the beginning of the code. For the end of the code, it is open square bracket, the string "/code", and the close square bracket.


Oops sorry about that :/

And yes im using the same resources for the picflag.image ( the resources in the array )
Was This Post Helpful? 0
  • +
  • -

#10 Michael26  Icon User is offline

  • Radar love
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,419
  • Joined: 08-April 09

Re: Flag Quiz (Need Help Please!)

Posted 26 September 2013 - 12:38 AM

I would suggest using Dictionary<Tkey,Tvalue>
You could use Dictionay<string, Image> where string would be for example Canberra, and Image Australia, that way you will always know correct answer by comparing key and value of your dictionary class
Was This Post Helpful? 1
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3165
  • View blog
  • Posts: 9,560
  • Joined: 05-May 12

Re: Flag Quiz (Need Help Please!)

Posted 26 September 2013 - 05:31 AM

At this point, instead of guessing, step through your code with a debugger. Set a breakpoint at the if statement and inspect the variables that you are comparing.

And yes, the dictionary would be better to keep associated information together. But if the comparisons aren't working correctly, the lookups into the dictionary equally won't work.
Was This Post Helpful? 0
  • +
  • -

#12 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 256
  • View blog
  • Posts: 529
  • Joined: 07-July 10

Re: Flag Quiz (Need Help Please!)

Posted 26 September 2013 - 06:42 AM

When do you put the images inside the array? I suspect you are loading your images a second time in your event handler.
private void DisplayRandomImage()
{
   picFlag.Image = Properties.Resources.China; // new Image object created
}

// ps: this code should not be in the event handler but off by itself
private void check_click(object sender, EventArgs args)
{
   ...
   Image[] images = new Image[10];
   ...
   images[1] = Properties.Resources.China; // new Image object created
   ...
           
   // Uses the reference == operator to determine if they are the same
   if (picFlag.Image == images[i])
   {
      ...
   }
}

Forgive me if some of this seems rudimentary. I'm not sure how much you've covered in your studies or who else will stumble on this post with similar issues.

According to this MSDN page about the Resourcemanager:

Quote

Internally the get property uses the ResourceManager class to create a new instance of the object.

What this means is that a new space in memory is carved out to hold the object.

Imagine your computer only has 5 memory locations for these examples, numbered 1-5 in order:
[_][_][_][_][_]

Calling == will do one of three things, in this order:

If both types are the same (or can be implicitly cast to the same) value types:
Spoiler


If one type is a reference type and a matching overloaded == operator can be found:
Spoiler


None of the above:
Spoiler


I think you are using the 3rd comparison type. Although it is the same file, the variables represent different spaces in memory. Hence, they are not "equal".

You can solve this by creating the collection outside this method. Use the ResourceManager only once to fill this collection. Don't use the ResourceManager again. Display the image by pulling it out of the collection. Then your comparison check will work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1