aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab6/Functions.cpp
blob: b6683eca4db28e1a231070a14740a256a19db652 (plain) (blame)
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
163
//=============================================================
//  CODE FOR PROBLEM #1 ON PAGE 282 (LEARN BY DOING) IS BELOW
//=============================================================

//#include "Header.h"
//
//void readData(string club_name[][31], string club_pres[][31], int num_stud[], float money[]) //Takes in user entered data and writes to arrays
//{
//	for (int i = 0; i < 10; i++)
//	{
//		cout << "\n\tEnter club " << i + 1 << " name: ";
//		getline(cin >> ws,club_name[i][0]);
//
//		cout << "\n\tEnter club " << i + 1 << " president: ";
//		getline(cin >> ws,club_pres[i][0]);
//
//		cout << "\n\tEnter club " << i + 1 << " number of students: ";
//		cin >> num_stud[i];
//		
//		money[i] = 75 * num_stud[i];
//
//		cout << "\n\n~~~~~~~~~~~~~~~~~~~~" << endl;
//	}
//	
//}
//
//void displayData(string club_name[][31], string club_pres[][31], int num_stud[], float money[]) // Displays the data from the arrays in an orderly fashion
//{
//	cout << "\n\nClub Name                       Club President                  # of Students     Money";
//	cout << "\n--------------------------------------------------------------------------------------------------\n";
//
//	for (int i = 0; i < 10; i++)
//	{
//		cout << left << setw(32) << club_name[i][0]
//			<< left << setw(32) << club_pres[i][0]
//			<< left << setw(18) << num_stud[i]
//			<< "$" << left << money[i] << endl;
//	}
//}


//====================================================================
//  CODE FOR PROBLEM #1 ON PAGE 292 (PROGRAMMING EXERCISES) IS BELOW
//====================================================================

#include "Header.h"

void displayMenu(int& menu_choice) //Displays menu choices and takes in user choice
{
	do
	{
		cout << "\n\t\t==============================";
		cout << "\n\t\t  What would you like to do?";
		cout << "\n\t\t==============================";

		cout << "\n\n\t1) Test if a string is a palindrome";
		cout << "\n\t2) Test if a string is all alphabetic characters";
		cout << "\n\t3) Count the number of times a specific character appears in a string";
		cout << "\n\t4) Exit";

		cout << "\n\n\t\tMenu choice #: ";
		cin >> menu_choice;

		if (menu_choice < 1 || menu_choice > 4)
		{
			cout << "\n\nError: Invalid menu choice\n";
		}
	} while (menu_choice < 1 || menu_choice > 4);
}

void processMenuChoice(int menu_choice) //Process menu choice, calls relevant function
{
	switch (menu_choice) {

	case 1:
	{
		isPalindrome();
		break;
	}

	case 2:
	{
		isAlphaStr();
		break;
	}

	case 3:
	{
		countChar();
		break;
	}

	case 4:
	{
		cout << "\n\t\t\t~ Goodbye! ~\n\n";
		break;
	}
	}
	
}

void isPalindrome() //Checks whether or not a user input string is a palindrome
{
	char string1[41];
	char stringR[41];
	int same = 2;

	cout << "\n\t\t---------------------------------------------------------";
	cout << "\n\t\t  Enter a string (up to 40 characters, spaces are okay)";
	cout << "\n\t\t---------------------------------------------------------";

	cin.ignore(numeric_limits<streamsize>::max(), '\n'); //****Ignores everything in the input buffer up to this point

	cout << "\n\tEnter string: ";
	cin.getline(string1, 41);

	strcpy_s(stringR, string1);

	_strrev(stringR);


	same = strcmp(string1, stringR);

	if (same == 0)
	{
		cout << "\n\tThe string is a palindrome!\n";
	}
	else if (same == 1)
	{
		cout << "\n\tThe string is not a palindrome.\n";
	}
}

void isAlphaStr() //Checks to see if a user input string contains all alphabetic characters or not, displays result
{
	char string2[31];
	int strLength = 0;
	int flag = 0;

	cout << "\n\t\t---------------------------------------------------";
	cout << "\n\t\t  Enter a string (up to 30 characters, no spaces)";
	cout << "\n\t\t---------------------------------------------------";

	cin.ignore(numeric_limits<streamsize>::max(), '\n'); //****Ignores everything in the input buffer up to this point

	cout << "\n\tEnter string: ";
	cin.getline(string2, 31);

	strLength = strlen(string2);

	for (int i = 0; i < strlength - 1; i++)
	{
		if (isalpha(string2))
		{

		}
	}
}

void countChar() //Takes in a user input string and a character, counts the frequency of that character within the string, displays result
{

}