Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
non incrementing loop Posted by Les2012 on 4 Dec 2012 at 9:22 AM
Good Morning:
I have been working on this function for two days and I cannot get it to run correctly. It becomes a runaway. 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:

/* 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.

Report
Re: non incrementing loop Posted by Les2012 on 5 Dec 2012 at 8:04 AM
HEllo:

Ok, I fixed the runaway problem but the function does not increment the
correct array elements. Does anyone have any suggestions? Here is the new code:
void getCharacterFrequency (char inputString[], unsigned int frequency[])
{
  char ch;
  int index1 = 0;
  int index2 = 0;
  const int SIZE = 26;
     
  while (index1 < SIZE)   
  {
	  frequency[index1] = 0;  // This is to initilize the frequency array values to zero
	  index1++;
  }
  
  ch = inputString[index2];
  while (ch != '\0')
	{
		if (ch >= 65 && ch <= 90)
		{
			frequency[ch - 65]++;
		}
	    else if (ch >= 95 && ch <= 122)
		{
		    frequency[ch - 95]++;
		}
		ch = inputString[++index2];
    }
  
}




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.