Debugging My Homework and I'm stumped

main executes up until you make a menu selection (line 54), then seems to get stuck in an infinite loop. I'm not sure if the issue is in my switch statement (57-88) or in the code of the methods it calls.

Is it maybe some sort of issue with the pointers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//Sarah Wilderman
//COP 2222
//Week 2 - Chapter 10
//
//
//"Vowels and Consonants"
/*
Write a function that accepts a pointer to a C-String as its argument. The function
should count the number of vowels appearing in the string and return the number.

Write another function that accepts a pointer to a C-string as its argument. This function
should count the number of consonants appearing in the string and return the number.

Demonstrate these two functions in a program as defined in the text on page 608.
*/

//Inclusions
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

//Prototype vowel function
int findVowels(char *);
int findConsonants(char *);

//Drumroll, please, here comes the main function!
int main()
{
	//Declare a constant variable, STRINGLENGTH, that will allow a maximum of 80 characters plus the null terminator.
	const int STRINGLENGTH = 81;
	//Declare a variable to store the entered string and restrict it's length to 80 characters plus the null terminator.
	char inputString[STRINGLENGTH];
	//Declare storage variables for vowels and consonants.
	int numVow, numCons;
	char option;

	//Prompt user for input and store it.
	cout << "Please enter a word or sentence. (Max 80 characters) \n";
	cin.getline(inputString, STRINGLENGTH);

	do 
	{
		//Display the menu
		cout << "A) Count the number of vowels in the string\n";
		cout << "B) Count the number of consonants in the string\n";
		cout << "C) Count both the vowels and consonants in the string\n";
		cout << "D) Enter another string\n";
		cout << "E) Exit the program\n";

		//Prompt the user for a menu selection
		cout << "\nPlease enter your selection: \n";
		//Store selection in a variable.
		option = cin.get();

		//Open a switch statement
		switch (option)
		{
			case 'A':
			case 'a':
				numVow = findVowels(inputString);
				cout << "There are " << numVow << " vowels!";
				break;
			case 'B':
			case 'b':
				numCons = findConsonants(inputString);
				cout << "There are " << numCons << "consonants!";
				break;
			case 'C':
			case 'c':
				numVow = findVowels(inputString);
				cout << "There are " << numVow << " vowels!";
				numCons = findConsonants(inputString);
				cout << "There are " << numCons << "consonants!";
				break;
			case 'D' :
			case 'd' :
				cout << "Enter a new String, please";
				cin.getline(inputString, 81);
				break;
			case 'E':
			case 'e':
				break;
			default:
				cout << "Please enter a valid option.";
				continue;
		}


	} while(option != 'E' || option != 'e');	

	system("PAUSE");
	return 0;
}

//Defining function findVowels
int findVowels(char *userString)
{
	//Declare and initialize a storage variable to zero.
	int vowels = 0;

	//Step through the character string passed into the function
	while (*userString != '\0')
	{
		//If the character is  any of those listed (vowels), add them to the value of the vowels variable
		if (*userString == 'a' || *userString == 'A' || *userString == 'e' || *userString == 'E' || *userString == 'i' || *userString == 'I' ||
			*userString == 'o' || *userString == 'O' || *userString == 'u' || *userString == 'U')
		{
			vowels++;
		}
	}
	//Return the value counted to point-of-call.
	return vowels;

}

//Defining function findConsonants
int findConsonants(char *userString)
{
	//Declare and initialize a storage variable to zero
	int consonants = 0;

	//Step through the C-String provided
	while (*userString != '\0')
	{
		//I stole this from the previous method - if the character is NOT any of those listed, add it to the value of the consonants variable.
		if (*userString != 'a' || *userString != 'A' || *userString != 'e' || *userString != 'E' || *userString != 'i' || *userString != 'I' ||
			*userString != 'o' || *userString != 'O' || *userString != 'u' || *userString != 'U')
		{
			consonants++;
		}
	}
	//Return the value counted to point-of-call
	cout << consonants;
	return consonants;
}
Q: What is the condition that controls the loops in findVowels() and findConsonants()?
A: *userString != '\0'

Q: What does change the value of *userString within the loop?
In findVowels(...) and findConsonants(...) the while loop goes forever or not at all because userString isn't changed and hence has always the same value.

You need to increment userString like e.g. so:
1
2
for(; *userString != '\0'; ++userString)
	{
Nevermind! I found the issue (and a few others besides) and now I've gotten it to run very well.

Now to figure out how to close this thread <<;;
There's no such thing as "closing a thread". The thread will remain open for posting until a certain length of time has passed, at which point it gets archived.

But if you want to indicate that your problem is solved, the convention is to change the thread icon to the green tick.
Topic archived. No new replies allowed.