error as the word 'apple' is not being accepted to listbox although its true to IF
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: error as the word 'apple' is not being accepted to listbox although its true to IF

  1. #1
    Join Date
    Oct 2016
    Posts
    38

    error as the word 'apple' is not being accepted to listbox although its true to IF

    Code:
    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 WordHandlerAssignment
    {
        public partial class frmWordHandler : Form
        {
            static string word;
            static string[] wordBank = new string[4];
            static int i = 0;
    
            public frmWordHandler()
            {
                InitializeComponent();
            }
    
            private void btnBackToMain_Click(object sender, EventArgs e)
            {
                {
                    frmSplashScreen mainForm = new frmSplashScreen();
                    mainForm.Show();
                    this.Hide();
                }
            }
    
            private void frmWordHandler_Load(object sender, EventArgs e)
            {
    
            }
    
            private void btnStoreWord_Click(object sender, EventArgs e)
            {
                if (lstStoreWords.Items.Count ==5)
                {
                    MessageBox.Show("Time to play");
                }
                word = txtStoreWords.Text.ToUpper();
    
                lengthCheck(word);
                vowelStart(word);
                recur(word);
    
                if ((lengthCheck(word) == true)&& (recur(word) == false) && (vowelStart(word) == true))
                {
    
                    wordBank[i] = txtStoreWords.Text;
                    lstStoreWords.Items.Add(word.ToString());
                    i++;
                    MessageBox.Show("Word Accepted");
    
                }
    
          
    
    
    
                if (lstStoreWords.Items.Count == 4)
                {
                    MessageBox.Show("Time to play");
                }
    
    
            }
    
            private bool recur(string word)
            { //start of recur mthod
                word = txtStoreWords.Text.ToUpper();
                int i = 0;
                int charCount = 0;
                
                for (i = 0; i < word.Length; i++)
                {
                    for (int j = 1; j < word.Length; j++)
                    {
                        if (j != i && word[i] == word[j])
    
                        {
                            charCount++;
                        }
                    }
                }
    
                if (charCount == 0)
                {
                    //MessageBox.Show("Word Not Accepted, has no reoccurence");
                    txtStoreWords.ResetText();
                    txtStoreWords.Focus();
                    return false;
                }
                
                
                else
                {
                   // MessageBox.Show("Word Accepted, has reoccurence");
                    txtStoreWords.ResetText();
                    txtStoreWords.Focus();
                    return true;
                }
                
            }//end of recur method
    
    
            private bool vowelStart(string word)
    
            {
    
                char vowel = word[0];
                
    
                switch (vowel.ToString().ToUpper())
                { //start of switch
                    case "A":
                    case "E":
                    case "I":
                    case "O":
                    case "U":
                        return true;
                        break;
    
                    default:
                       // MessageBox.Show("The word must start with vowel!");
                        return false;
                        break;
                        
    
                }//end of switch
    
                
    
            }
    
    
            private bool lengthCheck(string word)
            {
                word = txtStoreWords.Text.ToUpper();
    
                if (word.Length > 4 && word.Length < 9)
                {
    
                    return true;
    
    
                }
                else
                {
    
                    return false;
                }
                
            }
    
    
        } // store button end of method
    
    
    }
    Last edited by 2kaud; November 8th, 2016 at 11:53 AM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    4,752

    Re: error as the word 'apple' is not being accepted to listbox although its true to I

    When posting code, please use code tags. Select the formatted code and click '#'.

    Code:
    lengthCheck(word);
                vowelStart(word);
                recur(word);
    Why have these function calls here when you also use them in the if statement following?

    Code:
    if ((lengthCheck(word) == true)&& (recur(word) == false) && (vowelStart(word) == true))
    apple will be rejected as recur("apple") is true and the condition is testing for false.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
    C, C++ Compiler: Microsoft VS2015

  3. #3
    Join Date
    Oct 2016
    Posts
    38

    Re: error as the word 'apple' is not being accepted to listbox although its true to I

    Quote Originally Posted by 2kaud View Post
    When posting code, please use code tags. Select the formatted code and click '#'.

    Code:
    lengthCheck(word);
                vowelStart(word);
                recur(word);
    Why have these function calls here when you also use them in the if statement following?

    Code:
    if ((lengthCheck(word) == true)&& (recur(word) == false) && (vowelStart(word) == true))
    apple will be rejected as recur("apple") is true and the condition is testing for false.
    I have tried it with a true and even debugged the whole program when using apple, but it still does not accept. i have no idea why?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    4,752

    Re: error as the word 'apple' is not being accepted to listbox although its true to I

    I have tried it with a true and even debugged the whole program when using apple, but it still does not accept. i have no idea why?
    If you have used the debugger to debug the code and traced through it then you should see why the word is not accepted??

    Code:
    private bool lengthCheck(string word)
            {
                word = txtStoreWords.Text.ToUpper();
    As you are passing word as a parameter, why are you setting it to something else within the function? Same for function recur() ?

    For vowelStart(), the parameter word is already uppercase, so there is no need to convert vowel to uppercase and then to a string. The switch can be for vowel
    Code:
    switch (vowel)
                { //start of switch
                    case 'A':
                    case 'E':
                    case 'I':
                    case 'O':
                    case 'U':
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
    C, C++ Compiler: Microsoft VS2015

  5. #5
    Join Date
    Oct 2016
    Posts
    38

    Re: error as the word 'apple' is not being accepted to listbox although its true to I

    Thanks! I have to enter 5 words into the listbox which are stored in the array wordBank[]. Would you have any idea how i would start or go about using a random num generator to randomly choose a word from the array of words i have entered then how i would compare a guess i enter to the word? thanks

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    4,752

    Re: error as the word 'apple' is not being accepted to listbox although its true to I

    Quote Originally Posted by neymark View Post
    Thanks! I have to enter 5 words into the listbox which are stored in the array wordBank[]. Would you have any idea how i would start or go about using a random num generator to randomly choose a word from the array of words i have entered then how i would compare a guess i enter to the word? thanks
    Sorry, not in c#. I'm mainly c++

    but see http://stackoverflow.com/questions/2...nt-number-in-c
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
    C, C++ Compiler: Microsoft VS2015

  7. #7
    Join Date
    Oct 2016
    Posts
    38

    Re: error as the word 'apple' is not being accepted to listbox although its true to I

    Quote Originally Posted by 2kaud View Post
    Sorry, not in c#. I'm mainly c++

    but see http://stackoverflow.com/questions/2...nt-number-in-c
    Any chance you could show me in c++ so I could have an idea? cheers.
    Code:
    //private void btnGuess_Click(object sender, EventArgs e)
           
     //{
            //    int tries = 0;
            //    tries++;
    
            //    Random ranNum = new Random();
            //    int randomWord = ranNum.Next(0, 5);
            //    string wordGuess = wordBank[randomWord];
    
            //    for (int i = 0; i > wordGuess.Length; i++)
            //    {
    
            //        if (found == false)
            //        {
            //            if (tries > 3)
            //            {
            //                MessageBox.Show("Only 3 attempts allowed");
            //                lblGuess.ResetText();
            //                lblAttempt.ResetText();
            //            }
    Last edited by 2kaud; November 9th, 2016 at 02:30 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center