CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 34
  1. #1
    Join Date
    Oct 2016
    Posts
    38

    Post For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    Code:
    int i = 0;
    //  int j = i + 1;
    int charCount = 0;
    
    for(i=0; i<word.Length;i++)
    {
        for ( int j= 1; i < word.Length; j++)
        {
            if(word.Substring(i)==word.Substring(j))
            {
                charCount++;
            }
                   
        }
    }
    
    if (charCount!=0)
        MessageBox.Show("Word Accepted");
    else
        MessageBox.Show("Word Accepted");
    Last edited by 2kaud; October 27th, 2016 at 03:52 PM.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.

    and your c# question is? Do you really want it to display 'Word Accepted' under any circumstance? - both parts of the if statement display the same message!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Oct 2016
    Posts
    38

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    Sorry, just noticed that if its not equal to 0, it should not accept word into listbox

  4. #4
    Join Date
    Oct 2016
    Posts
    38

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    Quote Originally Posted by neymark View Post
    Sorry, just noticed that if its not equal to 0, it should not accept word into listbox
    Any idea of how i can get it to work?

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    you haven't said what is the problem. Have you debugged the program using the debugger - tracing through the code to see where it deviates from what expected?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Oct 2016
    Posts
    38

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    Quote Originally Posted by 2kaud View Post
    you haven't said what is the problem. Have you debugged the program using the debugger - tracing through the code to see where it deviates from what expected?
    The problem is that It always displays 'word accepted' even if i enter a word with no reocurring letters?

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    Code:
    for ( int j= 1; i < word.Length; j++)
    Don't you mean j < word.Length?

    What happens when i is 1 and j is 1? The test is always equals! Shouldn't the test exclude the case j == i?
    Code:
    if(j != i && word.Substring(i)==word.Substring(j))
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Oct 2016
    Posts
    38

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    Quote Originally Posted by 2kaud View Post
    Code:
    for ( int j= 1; i < word.Length; j++)
    Don't you mean j < word.Length?

    What happens when i is 1 and j is 1? The test is always equals! Shouldn't the test exclude the case j == i?
    Code:
    if(j != i && word.Substring(i)==word.Substring(j))
    So i should take out the j==1 ?and replace it with?

  9. #9
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    So i should take out the j==1 ?and replace it with?
    Change the if test as per my post #7
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Oct 2016
    Posts
    38

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    Quote Originally Posted by 2kaud View Post
    Change the if test as per my post #7
    I tried that, its still saying word accepted if i only have no reoccuring letters.
    could it possible be the if statement below the for loops?

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    Quote Originally Posted by neymark View Post
    The problem is that It always displays 'word accepted' even if i enter a word with no reocurring letters?
    It always says the 'word accepted' because that's what you programmed it to do. You've written 'Word Accepted' twice.

    Code:
    if (charCount!=0)
        MessageBox.Show("Word Accepted");   // 1st time
    else
        MessageBox.Show("Word Accepted");  // 2nd time - oops

  12. #12
    Join Date
    Oct 2016
    Posts
    38

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    i have it changed and it still is doing it.

  13. #13
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    Can't give you c# code, but for c++ the code below works OK.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int recur(const string& word)
    {
    	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)
    		cout << word << " accepted" << endl;
    	else
    		cout << word << " not accepted" << endl;
    
    	return charCount;
    }
    
    int main()
    {
    	recur("qwerty");
    	recur("qwetqy");
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    Also note that using two loops has a complexity of O(n*n). By using an array and just one loop this complexity can be reduced to just O(n). Consider (again c++ - sorry)
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int recur(const string& word)
    {
    	int cnt[256] = { 0 };
    	int charCount = 0;
    
    	for (int i = 0; i < word.length(); i++)
    		charCount += cnt[word[i]]++;
    
    	if (charCount)
    		cout << word << " accepted" << endl;
    	else
    		cout << word << " not accepted" << endl;
    
    	return charCount;
    }
    
    int main()
    {
    	recur("qwerty");
    	recur("qwetqy");
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    Oct 2016
    Posts
    38

    Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters

    Cheers i think ive got it working. Would you have any idea how i would go about a method accepting the word aslong as it it must start with vowel?

Page 1 of 3 123 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured