3 Function Program, Any suggestions and advice?

Im am working on a program for class and I am a beginner C++ programmer. I am having trouble passing pointers and arrays.
I am going to post the assignment and then post my code! Any help would be great!Thanks!

Mr. Ed wants you to help him develop a program that has two string functions. The program that he has in mind will display a menu of two string functions: function one will scan for a specified character and substitute the specified character with another specified character; and function two will count the number of vowels ‘a’,’e’,’i',’o’,’u’ (upper and lower case) and non-vowels in the string and produce a short report.

Declare an array of 80 characters in main(). Also, prompt the user for a string in main() and store the string into the array of characters that you have declared.

Your main() program will display a menu by calling Function 1, based on the menu choice returned by Function 1, call the appropriate function based on user’s selection. For each function 2 and 3, prompt for user’s input in main() and pass those information as arguments into the function.

Your program will allow Mr. Ed to perform as many string operations as he wishes.
For the loop that controls the iteration, use the following condition: ‘U’ for continue and any other character to stop. For each user input, first check to see if the input is a lower case character (hint: use one of the standard string functions), if it is, call a standard string function to convert it to upper case before using it. This does not apply to character input for function 2 and 3 but it does apply to function 1.

You are to write four functions:
Function 1: (No argument, return a character value)
This function will display the menu choice: A – Substitute Character, B – Count vowels. Call this function at the beginning of your program to display the menu. In the function, also, prompt for user’s choice, check for invalid input and loop until correct input is received. Return the choice back to main().

Function 2: (Accept three arguments: a pointer to an array, and two characters; return no value)
This function will accept a pointer to an array containing the string, and two characters. It will use the pointer to traverse the string and scan for the first character and when it finds the character, it will substitute the first character with the second character. Prompt for the character to be scanned and the character to be substituted in main() and pass those into the function.

Function 3: (Accept two arguments: an array and an integer; return an integer value)
This function will accept an array containing the string and size of array. It will loop through the string and scan for the character: a,e,i,o, or u (upper and lower case). When it finds any of those characters, it will start keeping track of the number of times each character appeared in the string. It will also track non-vowels that are not a space or not a special character. In the function, output a report that lists the count for each vowel and all non-vowel alphabets. Also, sum up the count of all vowels and return the sum to main(). In main(), if the number is zero, output a message stating that “the string does not consist of any vowel”. Otherwise, display the total number of vowels found in the string.

Bonus (2 points): Instead of passing in array and integer, pass in a pointer to the beginning of array and use that pointer to access the content of string.



** Do not use global variable, a minimum of 5 points will be taken off.
** Give your function meaningful name. Naming your function: Function 1, Func One, or Func A or anything similar will result in 3 points deduction for each function.

Here is what I have so far.

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <cctype>
#include <cstring>
#include <iomanip>
#include <iostream>

using namespace std;

char menu_function(); //function that will run the menu to call the other functions.
void sub_function(char, char, char); //function that will be used to substitute charachters.
int vowel_function(char[], int); //function that will be used to count the vowels.

int main()
{
	char strg_array[80];
	char contin = ' ';
	char choice = ' ';
	char target = ' ';
	char new_val = ' ';
	int array_size = 80;
	int vowel_sum = 0;


	do
	{
		cout << "Please enter a string. ";
		cin >> strg_array;
		cout << endl << endl;

		choice = menu_function();

			if(islower(choice)) //this is supposed to test if the menu choice was entered in lower case
			{
				char(toupper(choice)); // this is supposed to change the entered menu choice to uppercase if it was originaly lowercase, 
									 // but this doesnt work
			}

		switch(choice)
		{
			case 'A':

				cout << " Please select a character that you wish to substitute. ";
				cin >> target;
				cout << endl << endl << "Please select a character that you wish to use in the other characters place. ";
				cin >> new_val;
				cout << endl << endl;

				sub_function(&strg_array, target, new_val);

				break;

			case 'B':
				
				vowel_sum = vowel_function(strg_array, array_size);

				if(vowel_sum == 0)
				{
					cout << " The string does not consist of any vowel.\n\n";
				}
				else
				{
					cout << " There were a total of " << vowel_sum << " vowel(s) in the string entered.\n\n";
				}

				break;
		}
	

		cout << "If you would like to continue and enter another string please press U,\nif you would like to exit the program enter any other character. ";
		cin >> contin;
		cout << endl << endl;

	}while((contin == 'u') || (contin == 'U'));

return 0;
}


char menu_function()
{
	char option = ' ';

	do
	{
		

		cout << "MENU\n";
		cout << "A - Subsitute Character\n";
		cout << "B - Count Vowels\n";
		cout << "Please select an option from the menu.";
		cin >> option;
		cout << endl << endl;

	}while((option != 'A') && (option != 'a') && (option != 'B') && (option != 'b'));

	return option;
}

void sub_function(char* ch_ptr, char tar, char n_val)
{
	while(*ch_ptr != '\0')
	{
		if(*ch_ptr == tar)
		{
			*ch_ptr = n_val;
		}
		++ch_ptr;
	}

	return;
}

int vowel_function(char ch_array[], int size)
{
	char* ch_ptr = ch_array;
	int a = 0;
	int e = 0;
	int i = 0;
	int o = 0;
	int u = 0;
	int length = 0;
	int sum = 0;
	int non_vowel = 0;
		
	while(*ch_ptr != '\0')
	{
		if((*ch_ptr == 'a') || (*ch_ptr == 'A'))
			++a;
		else if((*ch_ptr == 'e') || (*ch_ptr == 'E')) 
			++e;
		else if((*ch_ptr == 'i') || (*ch_ptr == 'I')) 
			++i;
		else if((*ch_ptr == 'o') || (*ch_ptr == 'O'))
			++o;
		else if((*ch_ptr == 'u') || (*ch_ptr == 'U'))
			++u;
		++ch_ptr;
	}

	sum = a + e + i + o + u;

	length = strlen(ch_array);

	non_vowel = length - sum;

	cout << " There were " << a << " A(s) in your string.\n";
	cout << " There were " << e << " E(s) in your string.\n";
	cout << " There were " << i << " I(s) in your string.\n";
	cout << " There were " << o << " O(s) in your string.\n";
	cout << " There were " << u << " U(s) in your string.\n";
	cout << " There were " << non_vowel << " non_vowel alphebetic characters in your string.\n\n";

	return sum;
}

1. give your functions more meaningful names... such as show_menu e.g....
2. toupper RETURNS the character as an uppercase letter
3. if the user enters a non-concatenated string such as something like this "hello world" your string will only contain "hello"... (use cin.getline instead)...
4.check for islower in the show_menu-function and return the character uppercase...
5. dont pass &array into your sub-function... read upon the array-chapters again...
6. done use endless if-else-control structures... use the switch-case...
try this code..

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <cctype>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <conio.h>

using namespace std;

char menu_function(); //function that will run the menu to call the other functions.
void sub_function(char[], char, char); //function that will be used to substitute charachters.
int vowel_function(char[], int); //function that will be used to count the vowels.

int main()
{
	char strg_array[80];
	char contin = ' ';
	char choice = ' ';
	char target = ' ';
	char new_val = ' ';
	int array_size = 80;
	int vowel_sum = 0;


	do
	{
        system("cls");
		cout << "Please enter a string. ";
		cin.getline(strg_array,80);
		cout << endl << endl;

		choice = menu_function();

	/*	
    	if(islower(choice)) //this is supposed to test if the menu choice was entered in lower case
			{
				char(toupper(choice)); // this is supposed to change the entered menu choice to uppercase if it was originaly lowercase, 
									 // but this doesnt work
			}
*/

		switch(choice)
		{
			case 'A':
            case 'a':

				cout << " Please select a character that you wish to substitute. ";
				cin >> target;
				cout << endl << endl << "Please select a character that you wish to use in the other characters place. ";
				cin >> new_val;
				cout << endl << endl;

				sub_function(strg_array, target, new_val);

				break;

			case 'B':
            case 'b':
				
				vowel_sum = vowel_function(strg_array, array_size);

				if(vowel_sum == 0)
				{
					cout << " The string does not consist of any vowel.\n\n";
				}
				else
				{
					cout << " There were a total of " << vowel_sum << " vowel(s) in the string entered.\n\n";
				}

				break;
		}
	

		cout << "If you would like to continue and enter another string please press Y,\nif you would like to exit the program enter any other character. ";
	    contin = getche();
		cout << endl << endl;

	}while((contin == 'Y') || (contin == 'y'));

return 0;
}


char menu_function()
{
	char option = ' ';

	do
	{
		

		cout << "MENU\n";
		cout << "A - Subsitute Character\n";
		cout << "B - Count Vowels\n";
		cout << "Please select an option from the menu.";
		cin >> option;
		cout << endl << endl;

	}while((option != 'A') && (option != 'a') && (option != 'B') && (option != 'b'));

	return option;
}

void sub_function(char* ch_ptr, char tar, char n_val)
{
	while(*ch_ptr != '\0')
	{
		if(*ch_ptr == tar)
		{
			*ch_ptr = n_val;
		}
		++ch_ptr;
	}
	
    return;

}

int vowel_function(char ch_array[], int size)
{
	char* ch_ptr = ch_array;
	int a = 0;
	int e = 0;
	int i = 0;
	int o = 0;
	int u = 0;
	int length = 0;
	int sum = 0;
	int non_vowel = 0;
		
	while(*ch_ptr != '\0')
	{
		if((*ch_ptr == 'a') || (*ch_ptr == 'A'))
			++a;
		else if((*ch_ptr == 'e') || (*ch_ptr == 'E')) 
			++e;
		else if((*ch_ptr == 'i') || (*ch_ptr == 'I')) 
			++i;
		else if((*ch_ptr == 'o') || (*ch_ptr == 'O'))
			++o;
		else if((*ch_ptr == 'u') || (*ch_ptr == 'U'))
			++u;
		++ch_ptr;
	}

	sum = a + e + i + o + u;

	length = strlen(ch_array);

	non_vowel = length - sum;

	cout << " There were " << a << " A(s) in your string.\n";
	cout << " There were " << e << " E(s) in your string.\n";
	cout << " There were " << i << " I(s) in your string.\n";
	cout << " There were " << o << " O(s) in your string.\n";
	cout << " There were " << u << " U(s) in your string.\n";
	cout << " There were " << non_vowel << " non_vowel alphebetic characters in your string.\n\n";

	return sum;
}
 
Topic archived. No new replies allowed.