Jump to content

C# help!

780blzit

I'm trying to make a small program where you enter the correct word for the definition that is displayed.

 

Example:

 

"Statement of the meaning of a term."

 

answer: Definition

 

Now I've sorted the correct definitions in an array, and I've sorted the answers that the user put in in another array. 

<pre class="_prettyXprint _linenums:0">using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace Glosövning{    public partial class Form1 : Form    {        static string[] gloser = new string[19];        static string[] svar = new string[19];        static string[] rätt = new string[19];        static int k = 0;        static int s = 0;        public Form1()        {            InitializeComponent();            gloser[0] = "Öka med 1, 'i++' till exempel.";            gloser[1] = "Minska med 1, 'i--' till exempel.";            gloser[2] = "Reservera minne.";            gloser[3] = "Ge variabler ett värde.\n'i = 5' till exempel.";            gloser[4] = "Används för att kombinera\nflera olika villkor.";            gloser[5] = "Större än, mindre än.\n <, >, <=, >= till exmepel.";            gloser[6] = "Tillfälligtvis gör\nom en datatyp till en annan.";            gloser[7] = "Vilken sorts information det är,\nint för heltal, double för decimaltal.";            gloser[8] = "För stora program lätta att överblicka.\nGörs för att slippa upprepa sig själv.";            gloser[9] = "Används för att hoppa in i\ntexten eller för att få raka kolumner.";            gloser[10] = "En parantes i en parantes,\nloop i en loop.";            gloser[11] = "Överlagrade metoder,\nflera metoder med samma namn.";            gloser[12] = "Lägger ihop stränger/text.";            gloser[13] = "Källkod görs om till\nmaskinkod som datorn kan exekvera.";            gloser[14] = "Ställer upp koden på\nett snyggt sätt så det\nblir lättare att se.";            gloser[15] = "Annat ord för upprepning,\nt.ex. upprepa loopar.";            gloser[16] = "När något uppropar sig\nsjälv. T.ex. en metod.";            gloser[17] = "Struntar i decimaler, avrundar";            gloser[18] = "Restvärde ur en heltalsdivision";            rätt[0] = "inkremera";            rätt[1] = "dekremera";            rätt[2] = "allokera";            rätt[3] = "tilldela";            rätt[4] = "logiska operatorer";            rätt[5] = "villkors operatorer";            rätt[6] = "typomvandling";            rätt[7] = "datatyp";            rätt[8] = "metod";            rätt[9] = "escapetecken";            rätt[10] = "nästlad";            rätt[11] = "polymorphism";            rätt[12] = "konkatenera";            rätt[13] = "kompilera";            rätt[14] = "indentera";            rätt[15] = "iteration";            rätt[16] = "rekursion";            rätt[17] = "trunkera";            rätt[18] = "modulus";        }        private void button1_Click(object sender, EventArgs e)        {        beginning:            if (k > 0 && k < 19)            {                textBox1.Text = svar[k];            }            if (k >= 0 && k < 19)            {                label2.Text = gloser[k];;            }            k++;            if (k <= 19)            {                label3.Text = k + "/19";                button1.Text = "Nästa";            }            if (k == 19)                button1.Text = "Resultat";            if (k == 20)            {                label2.Text = "Du hade " + s + " antal rätt.";                button1.Text = "Börja om";            }            if (k == 21)            {                Array.Clear(svar, 0, 19);                         k = 0;                goto beginning;            }        }        private void textBox1_TextChanged(object sender, EventArgs e)        {        }        private void label2_Click(object sender, EventArgs e)        {                              }        private void label3_Click(object sender, EventArgs e)        {        }        private void label4_Click(object sender, EventArgs e)        {        }    }} </pre> 

I want to compare the answers with the correct definitions.

 

I've tried:

 

"if(svar[k] == rätt[0])

    s++;"

Link to comment
Share on other sites

Link to post
Share on other sites

if(svar[k].equals(rätt[0])

 

I'm a little bit rusty, but "equals" should work.

Link to comment
Share on other sites

Link to post
Share on other sites

if(svar[k].equals(rätt[0])

 

I'm a little bit rusty, but "equals" should work.

 

I tried it but it doesn't work. :( 

Link to comment
Share on other sites

Link to post
Share on other sites

I tried it but it doesn't work. :(

try running it in debug mode, then you can see the values of the variables

 

are you sure you don't want to compare svar[k] with ratt[k] ? do you actually intend to compare svar[k] with ratt[0]?

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

try running it in debug mode, then you can see the values of the variables

 

are you sure you don't want to compare svar[k] with ratt[k] ? do you actually intend to compare svar[k] with ratt[0]?

ooh my bad! I definitely meant svar[k] with rätt[k]!! Sorry! 

Link to comment
Share on other sites

Link to post
Share on other sites

ooh my bad! I definitely meant svar[k] with rätt[k]!! Sorry! 

is the same bug present in your code as well?

do you still have the problem?

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

is the same bug present in your code as well?

do you still have the problem?

 

Yes.

Link to comment
Share on other sites

Link to post
Share on other sites

Yes.

confusing answer is confusing

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

confusing answer is confusing

 

I'm sorry, I'm quite new to C# sooooo..

 

I thought your answers were the same?

 

I still do have a problem, and you told me to run it in debug mode. Which I did, I don't know where you get to see the values of the variables?

 

 

Sorry, english is obviously not my mother language.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm sorry, I'm quite new to C# sooooo..

 

I thought your answers were the same?

 

I still do have a problem, and you told me to run it in debug mode. Which I did, I don't know where you get to see the values of the variables?

 

 

Sorry, english is obviously not my mother language.

if you are doing it in visual studio then create a brake point by selecting the line

if(svar[k] == ratt[k])

 

and then press F8 it should create a red dot in front of the line

then press F5 and your code will be paused there

then you can move your cursor over

svar[k]

and

ratt[k]

 

by holding your Mouse pointer above them, small boxes that show values should appear

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

if you are doing it in visual studio then create a brake point by selecting the line

if(svar[k] == ratt[k])

and then press F8 it should create a red dot in front of the line

then press F5 and your code will be paused there

then you can move your cursor over

svar[k]

and

ratt[k]

by holding your Mouse pointer above them, small boxes that show values should appear

 

It's showing me svar[19] and rätt[19] ?

Link to comment
Share on other sites

Link to post
Share on other sites

It's showing me svar[19] and rätt[19] ?

so is your "k" = 19? If so then that would be your problem as you don't have a value for ratt[19]

 

can you paste code where you assign "k" value and "svar[]" value?

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

so is your "k" = 19? If so then that would be your problem as you don't have a value for ratt[19]

 

can you paste code where you assign "k" value and "svar[]" value?

 

 
            if (svar[k] == rätt[k] && k < 19)
                s++;
Link to comment
Share on other sites

Link to post
Share on other sites

 

 
            if (svar[k] == rätt[k] && k < 19)
                s++;

 

I would like to see something that looks like:

svar[0]="inkremera";svar[1]="inkremera";..... int k=0;while(k<19){   if(svar[k]==ratt[k])   {     s++;   }   k++;}

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

I would like to see something that looks like:

svar[0]="inkremera";svar[1]="inkremera";..... int k=0;while(k<19){   if(svar[k]==ratt[k])   {     s++;   }   k++;}

 

aaalright, that seems to somewhat do it. 

 

Honestly, I'm so confused right now with my own code I don't know what I'm doing anymore haha..

 

I'm going to bed soon but if you want to download it and have a look at it yourself you can: https://www.dropbox.com/s/0kgfzxve809rs6z/glos%C3%B6vning123.rar?dl=0

 

Not sure if I'm allowed to post links or not but ohwell, thank you so much for your help anyways!

Link to comment
Share on other sites

Link to post
Share on other sites

Honestly, I'm so confused right now with my own code I don't know what I'm doing anymore haha..

I'm going to bed soon but if you want to download it and have a look at it yourself you can: https://www.dropbox.com/s/0kgfzxve809rs6z/glos%C3%B6vning123.rar?dl=0

 I'm sorry... I think you will have to rewrite the logic of your "if" statements, most of them make no sense

the part with

if(k<519)

is just mind boggling...

 

and I never found where you assign any value to "svar"

 

it's like what are you even doing? that's crazy! you're being insane!

 

sorry, you will have to rewrite pretty much all your "if" statements

can you explain what you want the code to do with sentences?

maybe draw a block-diagram?

 

on a high level I have a feeling you want to display the definition to the user and ask him to enter a term, then you would like to check the number of correct answers.

is that correct?

I don't understand why do you have those aditional states like

if(k>21){ ....}

 

like where do those come from? because there's only 19 definitions and it feels like your "k" value should be from 0 to 18, why do you expect it to go to 21, and 519??? are those all typos?

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm sorry, I'm quite new to C# sooooo..

 

I thought your answers were the same?

 

I still do have a problem, and you told me to run it in debug mode. Which I did, I don't know where you get to see the values of the variables?

 

 

Sorry, english is obviously not my mother language.

 

so it's your father language  ​/jk

 

 

aaalright, that seems to somewhat do it. 

 

Honestly, I'm so confused right now with my own code I don't know what I'm doing anymore haha..

 

I'm going to bed soon but if you want to download it and have a look at it yourself you can: https://www.dropbox.com/s/0kgfzxve809rs6z/glos%C3%B6vning123.rar?dl=0

 

Not sure if I'm allowed to post links or not but ohwell, thank you so much for your help anyways!

Go over what you've already done and add comments by doing "//" or "///". Write comments so that you now which part does what and you don't end up confused when something doesn't work.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

I read what you are trying to do and there is an easier way to do it. If you still having problem let me know.

Link to comment
Share on other sites

Link to post
Share on other sites

Instead of using a string array (string[]) try using a List ... lists are much easier to iterate and have lots of great built in methods to help you traverse them.

Even better in your case the "Dictionary" here is your friend!!

Psuedo Code:

Dictionary<string, string> terms = new Dictionary();terms.Add("test", "test");

Have a look at this great resource http://www.dotnetperls.com/dictionary



 

// Gigabyte 990FXA-UD3 // AMD FX-8320 CPU @ 4.3 Ghz (7-21.5 Multiplier) 200.90mhz FSB CPU-Z Validated // Kraken X40 AIO - 2x140mm Push-Pull // 4GB Corsair Vengeance LP - 8GB Avexir Core Series Red 1760Mhz // Sapphire R9 Fury Nitro 1130mhz/4GB 1025mhz (Effective) GPU-Z Validation // Corsair SP2500 2.1 & Microlab Solo 9C Speakers // Corsair K90 Silver - Cherry MX Red & Blue LEDs // EVGA SuperNova 850w G2

Link to comment
Share on other sites

Link to post
Share on other sites

As @Mkfish and @AluminiumTech eluded to, you need to have a think about what you are doing because right now it's going very badly wrong and here's why:

  • goto keyword...You should NEVER use the goto keyword!
  • Inconsistent initialization
  • Static variables... just no... don't
  • Far too many if elses
  • Use of magic numbers
  • Incorrect data structure choice for the context
  • Explicit newline tokens
  • String concatenation where interpolation could be used

Seriously when you look at that code do your instincts not tell you anything at all?

 

Instead of trying to fix the symptoms as the majority of replies so far seem to be concerned with I am going to attack the problem directly: Who the hell taught you to write crap like that?

 

Sorry if that seems overly harsh or criticising but I am interested in helping you to help yourself with this. You should have arrived at the conclusion that something was deeply wrong by yourself by now and stopped to ask the question 'is there an easier way than trying to hammer a square peg into a round hole'. For instance, fighting against the language by trying to use two arrays as a lookup table.

 

Moving forwards then, perhaps consider throwing what you have away and starting again - yes simply delete it. Then define your data model and decide on where it's going to live. Only after that point move onto the implementation... and we can talk about that.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

/*!\FILE:       miniDictionary.
* DESCRIPTION:  small search and found program for 
*               words and definition.
* PROGRAMMER:   Brian Hinds.
* DATE:         2016-31-02.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace MiniDictionary
{
    public partial class MiniDictionary : Form
    {
        /*! Create hash table for the mini dictionary. */
        private Hashtable miniDictionary = new Hashtable();
        /*! Variable for the search word. */
        private string search;


        public MiniDictionary()
        {
            InitializeComponent();
        }

        /*! Executes the method that loads dictionary data base. */
        private void MiniDictionary_Load(object sender, EventArgs e)
        {
            loadMiniDictionary();
        }
        /*!\METHOD:         loadDictionary.
          * DESCRIPTION:    Loads the search and definition for the 
          *                 mini dictionary from a text file.
          * PARAMETER:      none.
          * RETURN:         none.
          */
        public void loadMiniDictionary()
        {
            /*! Reads the lines of string from the text file. */
            List<string> readLines = File.ReadAllLines(@"dictionaryData.txt").ToList();
            /*! Scan through each line of string that is read. */
            foreach (string line in readLines)
            {
                /*! parses the string, Word and Definition, separated by a | character. */
                string key = line.Split('|')[0];
                string val = line.Split('|')[1];

                /*! Check if the word and meaning already exist in mini dictionary. */
                if (!miniDictionary.ContainsKey(key))
                {
                    miniDictionary.Add(key, val);
                }
            }
        }
        /*!\METHOD:         btnSearch_Click.
          * DESCRIPTION:    Searches the dictionary for the definition 
          * PARAMETER:      none.
          * RETURN:         none.
          */
        private void btnSearch_Click(object sender, EventArgs e)
        {
            /*! Checks if search window is empty. */
            if (!string.IsNullOrEmpty(searchTB.Text))
            {
                /*! Store the word to be search. */
                search = searchTB.Text;
                /*! Checks if the word is in the dictionary. */
                if (miniDictionary.ContainsKey(search))
                {
                    /*! Displays the matching definition to the searched word. */
                    resultsTB.Text = Convert.ToString(miniDictionary[search]);
                }
                else
                {
                    /*! Displays this message if not found. */
                    resultsTB.Text = "Word not found!";
                }
            }
            else
            {
                /*! Displays this message if the search window is empty. */
                resultsTB.Text = "Enter a word to search!";
            }
        }

        /*!\METHOD:         btnReset_Click.
          * DESCRIPTION:    Resets the value on the form. 
          * PARAMETER:      none.
          * RETURN:         none.
          */
        private void btnReset_Click(object sender, EventArgs e)
        {
            /*! Clears both the search and results windows.. */
            resultsTB.Clear();
            searchTB.Clear();
        }
    }
}

This is the best and less bothersome solution to your program. Sorry about the date on the file, for some reason i cant edit it on this.

dictionaryData.txt

miniD.png

Source.txt

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×