Thread: Non incrementing loop.

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    14

    Non incrementing loop.

    Good Morning:
    I have been working on this function for two days and I cannot get it to increment properly. Can someone please help me? Here are the instructions for the function:
    Write a function named analyzeString. This function is passed a null terminated string as the first parameter. The function uses 3 reference parameters to return the number of vowels, the number of consonants, and the number of separator characters. Assume a separator character is a space, a tab, or a newline. The function declaration is as follows:
    void analyzeString (char inputString [], int & numVowels, int & numConsonants, int & numSeparators);
    Here is the code that I have so far:

    Code:
    /* This program will test the 
    "void analyzeString ( char inputString [], int & numVowels, 
    int & numConsonants, int & numSeparators)" function*/
    #include <iostream>
    using namespace std;
     
    void analyzeString ( char inputString [], int & numVowels,
           int & numConsonants, int & numSeparators);
     void main()
     {
        const int SIZE = 100;
          char inputString [SIZE] = {'l', 'D', '\t', ' ', 's','P','\0'};
          int numVowels, numConsonants, numSeparators;
          numVowels = numConsonants = numSeparators = 0;
     
           analyzeString (inputString, numVowels, 
                      numConsonants, numSeparators);
      cout << numVowels <<'\n';
      cout <<numConsonants <<'\n';
      cout <<numSeparators <<'\n';
     }
     
     
     * This function will count the number of vowels, 
    consonants, and separator characters in a string */
     
    #include <iostream>
    using namespace std;
     
    void analyzeString ( char inputString [], int & numVowels, int & numConsonants, int & numSeparators)
    {
          char ch;
          ch = inputString[0];
          numVowels = 0;
          numConsonants = 0;
          numSeparators = 0;
          int increment = 0;
     
          cout << "in funct \n";
          while ( ch != '\0')
          {
                if ( ch == 65 || ch == 69 || ch == 73 || ch == 79 || ch == 85 || //check for vowels
                   ch == 97 || ch == 101 || ch == 105 || ch == 111 || ch == 117)
                   numVowels++;
     
                if (( ch != 65 && ch != 69 && ch != 73 && ch != 79 && ch != 85 &&  //check for consonants
                   ch != 97 && ch != 101 && ch != 105 && ch != 111 && ch != 117)
                   && (ch >= 65 && ch <= 90 || ch >= 97 && ch <= 122))
                   numConsonants++;
     
                else
                   numSeparators++;
          } inputString [increment++];
         
    }
    The loop does not increment, it is a runaway.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's probably because
    Code:
    inputString [increment++];
    is outside the while loop. Also, you never reassign ch so even if increment was increasing the loop would still be infinite. Suggested fix: write
    Code:
    ch = inputString[++increment];
    as the last line of the while loop.

    Also, there's no need to use numbers to represent character literals. You can use "if ( ch == 'A' || ch == 'E' ..." or even "if ( strchr("AEIOU", ch) != NULL ) ...".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yes, definitely remove all those nasty magic numbers in there. Use toupper where it helps.

    You should also write your string in the shorter more common form:
    Code:
    char inputString [SIZE] = "lD\t sP\0";
    Oh, and your main should return int, not void, plus your comment on line 24 is missing the slash.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    14
    Thank you to everyone. I got this one working, now I only have four more to go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop not incrementing correctly
    By bobknows in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2012, 12:03 AM
  2. Replies: 3
    Last Post: 10-07-2012, 09:56 AM
  3. Replies: 3
    Last Post: 10-04-2012, 09:56 AM
  4. Incrementing Twice?
    By Kemaratu in forum C Programming
    Replies: 3
    Last Post: 09-27-2009, 11:13 PM
  5. Incrementing by 2 not 1
    By Eckey in forum C++ Programming
    Replies: 6
    Last Post: 10-14-2004, 03:16 PM