F
Foxy Kav
Hi everyone, Im currently doing first year UNI, taking a programming
course in C++, for one project i have to create a simple array
manipulator... that i have done, but i cant figure out how to make the
ESC key quit the called function when ever the user inputs data. The
description was : ESC drops back to the main menu in case the user
gets locked up or wants to start over again, ESC should provide a fool
proof way to exit back to the main menu from anywhere in the program.
Here's my code:
#include <iostream>
#include <cstdlib>
#include <conio.h> // For getsch()
#include <cstring> // For strlen()
using namespace std;
#define MAXSIZE 200 // Defining maximum range of the array
stringarray, emptystring and copystringarray.
#define ESC 27
// Program functions.
void enter() ; // Enter string into array function.
void display(); // Display array contents function.
void erase() ; // Earse contents of array function.
void displaychar() ; // Display character at location function.
void empty() ; // Is the array empty function.
void display10char() ; // Display the first 10 characters of the array
function.
void displayrange() ; // Display characters from array within a given
range function.
void replacechar() ;// Charcter to replace function.
void reverse() ; // Reverse string function.
// Declaring global variables.
// Global variables for enter array.
char stringarray[MAXSIZE] ; // Array to hold the string.
// Global variables for earse array.
char emptystring[MAXSIZE] = "\0" ; // Empty array to copy to
stringarray.
// Global variables for display characater at location.
int length ; // The length in numbers of the array.
// Global variables for is the array empty function.
int test ; // Testing if character is a space.
// Global varaible for reverse string funtion.
char copystringarray[MAXSIZE] = "" ; // Copy of orginal array +
contents.
int main() { // Start main function.
// Declaring variables for main function.
char menuselect ; // Menu selction character.
for(; { // Start for lop for menu.
do { // Start do loop for menu.
cout << " String Manipulation: Lab 2 by Kylie Kavanagh 3052463
\n \n" ;
cout << " e) Enter data into the array \n" ;
cout << " d) Display current contents of array \n" ;
cout << " r) Erase current contents of the array. \n \n" ;
cout << " c) Display chracter at location specified \n" ;
cout << " i) Is the array empty? \n" ;
cout << " t) Display the first 10 characters of the array \n"
;
cout << " f) Display characters in range specified \n \n" ;
cout << " j) Replace specified characters with other specified
characters \n \n" ;
cout << " v) Display the string in reverse order \n" ;
cout << " q) Quit this program \n \n" ;
cout << " ESC - Go back to main menu \n" ;
cout << " \n This is the current array contents: " <<
stringarray << "\n" ;
cout << " \n Enter selection: " ;
menuselect = getch() ;
} while (menuselect < 'a' || menuselect > 'z' && menuselect !=
'q') ;
if(menuselect == 'q') break;
cin.ignore(); // Clearing cin buffer.
cout << "\n" ;
switch(menuselect) { // Start switch statement for the menu.
case 'e': // Enter data into the array.
enter() ; // Calling function enter().
break ;
case 'd': // Display current contents of array.
display() ; // Calling function display().
break ;
case 'r': // Earse current contents of array.
erase() ; // Calling function erase() .
break ;
case 'c': // Display character at location number:
displaychar() ; // Calling function displaychar().
break ;
case 'i': // Is input array empty?
empty() ; // Calling function empty().
break ;
case 't': // Display first 10 characters of input array.
display10char() ; // Calling function display10char().
break ;
case 'f': // Display characters in range.
displayrange() ; // Calling function displayrange().
break ;
case 'j': // Replace 'g' with 'h'.
replacechar() ; // Calling function replacechar().
break ;
case 'v': // Display string in reverse order.
reverse() ; // Calling function reverse().
break ;
} // End switch statement for menu.
} // End infinite for loop for menu.
return 0;
} // End main function.
void enter() { // Start enter string into array function.
cout << " Enter string into array: " ;
cin.ignore(); // Clearing cin buffer.
cin.getline(stringarray , MAXSIZE , '\n') ;
// if (cin.getline(stringarray) == ESC) return ;
cout << "\n You entered the string : \n " ;
cout << stringarray << ends;
cout << "\n \n" ;
system("PAUSE");
} // End enter string into array function.
void display() { // Start display contents of array.
cout << " Display contents of array : \n " ;
cout << stringarray << ends;
cout << "\n \n " ;
system("PAUSE");
} // End display contents of array.
void erase(){ // Start of erase contents of array function.
cout << " Current contents of array : \n " ;
cout << stringarray << ends;
cout << "\n \n" ;
strcpy(stringarray , emptystring);
cout << " Contents of array after erasure: " ;
cout << stringarray << ends;
cout << "\n \n" ;
system("PAUSE");
} // End of erase contents of array function.
void displaychar() { // Start display character at location function.
int location ; // Location number of character of array to display.
cout << stringarray << "\n" << ends ; // Displaying the array.
length = strlen(stringarray); // Calculkating the length of the
array.
cout << " \n Enter a location number within the length of the
array (between 1 and " << length << "): " ;
cin.ignore(); // Clearing cin buffer.
cin >> location ;
while(location >= length)
{ // Start while loop.
cout << " \n You did not enter a location number
within the length of the array (between 1 and " << length << ")" ;
cout << " \n Try again: " ;
cin.ignore(); // Clearing cin buffer.
cin >> location ;
} // End while loop.
cout << " \n The character at the location number " << location <<
" is: " << ends; ;
cout << stringarray[location -1] << ends ;
cout << "\n" ;
system("PAUSE");
} // End display character at location function.
void empty() { // Start is the array empty function.
length = strlen(stringarray) ; // Calculating the length of the
array.
test = isalnum(stringarray[0]) ;//test if srray is only a string
if (length > 1)
{ // Start if statement.
cout << " The string is not empty. \n The contents of the
string is: " << stringarray << "\n" ;
} else { // End if statement, start else staement.
if(test != 1)
{ // Start if statement.
cout << " The string is empty. \n There
is nothing in the string, as shown by displaying the string. \n
Contents of string :" << stringarray << "\n" ;
} else { // End if statement, start else
statement.
cout << " The string is not
empty. \n The contents of the string is: " << stringarray << "\n" ;
} // End else statement.
} // End else statement.
cout << "\n" ;
system("PAUSE");
} // End is the array empty function.
void display10char() { // Start display the first 10 characters of the
array function.
cout << " The first ten characters of you array is: " ;
for(int i = 0 ; i < 10 ; i++) cout << stringarray ; //
Displaying the character at i.
cout << "\n" ;
system("PAUSE");
} // End display the first 10 characters of the array function.
void displayrange() { // Start display characters from array within a
given range function.
int startlocation ; // Start location for show array.
int endlocation ; // End location for show array.
int i ; // Control variable for the for loop.
length = strlen(stringarray) ; // Calculating the length of the
array.
cout << " The string is " << length << " characters long. \n From 1
to " << length << " is your valid range. " ;
cout << " \n Enter the start of the range for the array to be shown:
" ;
cin.ignore();
cin >> startlocation ; // Enter start location number.
while (startlocation > length) // Testing if the start location is
within the array length.
{ // Start while loop.
cout << " \n The start location you entered was not in the
arrays range. \n The arrays range is 1 - " << length << " \n Re-enter
the start of the range for the array to be shown: " ;
cin.ignore(); // Clearing cin buffer.
cin >> startlocation ; // Enter start location number.
} // End while loop.
cout << " \n Enter the end of the range for the array to be shown: "
;
cin >> endlocation ; // Enter end location number.
while (endlocation > length) // Testing if the end location is
within the array length.
{ // Start while loop.
cout << " \n The end location you entered was not in the
arrays range. \n The arrays range is 1 - " << length << " \n Re-enter
the end of the range for the array to be shown: " ;
cin.ignore(); // Clearing cin buffer.
cin >> endlocation ; // Enter end location number.
} // End while loop.
while(startlocation > endlocation)
{ // Start while loop.
cout << " \n The start location must be a smaller number than
the end location within \n the valid range : 1 - " << length << " " ;
cout << " \n Enter the start of the range for the array to be
shown: " ;
cin >> startlocation ; // Enter start location number.
while (startlocation > length) // Testing if the start
location is within the array length.
{ // Start while loop.
cout << " \n The start location you
entered was not in the arrays range. \n The arrays range is 1 - " <<
length << " \n Re-enter the start of the range for the array to be
shown: " ;
cin.ignore(); // Clearing cin buffer.
cin >> startlocation ; // Enter start
location number.
} // End while loop.
cout << " \n Enter the end of the range for the array to be
shown: " ;
cin >> endlocation ; // Enter end location number.
while (endlocation > length) // Testing if the end
location is within the array length.
{ // Start while loop.
cout << " \n The end location you
entered was not in the arrays range. \n The arrays range is 1 - " <<
length << " \n Re-enter the end of the range for the array to be
shown: " ;
cin.ignore(); // Clearing cin buffer.
cin >> endlocation ; // Enter end
location number.
} // End while loop.
} // End while loop.
cout << " \n Your selected range was: " << startlocation << " to "
<< endlocation ;
cout << " \n The contents of this range is : " ;
for(i = (startlocation -1 ) ; i < endlocation ; i++) cout <<
stringarray ; // Displaying the character at i.
cout << "\n \n" ;
system("PAUSE");
} // End display characters from array within a given range function.
void replacechar() { // Charcter to replace function.
char chartoreplace ; // Charcter to replace.
char chartoreplacewith ; // Charcter to replace with.
int dectect ; // Control varaibel for sorting loop
cout << " This is you current string: " << stringarray ;
cout << " \n Enter the character you wish to replace (g): " ;
cin.ignore(); // Clearing cin buffer.
cin >> chartoreplace ;
cout << " \n Enter the chracter you wish to replace (g) with (h):
" ;
cin.ignore(); // Clearing cin buffer.
cin >> chartoreplacewith ;
cout << " Replace " << chartoreplace << " with " <<
chartoreplacewith << "\n" ;
length = strlen(stringarray); // Calculating the length of the
array.
cout << "\n This is your changed string: " ;
for(dectect = 0 ; dectect < length ; dectect++)
{ // Start for loop.
if (stringarray[dectect] == chartoreplace)
stringarray[dectect]= chartoreplacewith ;
cout << stringarray[dectect] ;
} // End for loop.
cout << "\n" ;
system("PAUSE");
} // Charcter to replace function.
void reverse() { // Start reverse string function.
char *startofarray ;// Pointer at the start of the string.
char *endofarray ; // Pointer at the end of the string.
char temporary ; // Temporary storage of array elemnet while swapping
around.
cout << " Current array contents: " << stringarray << "\n" ;
/*
strcpy(copystringarray, stringarray) ; // Copying string to keep
orignal string and reverse the copied string.
length = strlen(copystringarray) ; // Finding the length of the
copied string.
startofarray = copystringarray ; // Giving the pointer the
addresss of the start of the array.
endofarray = ©stringarray[length - 1] ; // Giving the pointer
the address of the end of the array, minus the null character.
while (startofarray < endofarray) // Compares start and end
pointer for the condittion expression.
{
temporary = *startofarray ;
*startofarray = *endofarray ;
*endofarray = temporary ;
startofarray++ ; // Moves the pointer up one in the
array index.
endofarray-- ; // Moves the pointer down one in the
array index.
} // End of while loop.
cout << "\n Reversed array: " << copystringarray << "\n" ;
cout << "\n" ;
*/
length = strlen(stringarray) ; // Calculating the length of the
array.
cout << " Your array backwards: " ;
for(int i = length -1 ; i >= 0 ; i--) cout << stringarray ;
// Displaying the character at i.
cout << "\n" ;
system("PAUSE");
} // End reverse string function.
course in C++, for one project i have to create a simple array
manipulator... that i have done, but i cant figure out how to make the
ESC key quit the called function when ever the user inputs data. The
description was : ESC drops back to the main menu in case the user
gets locked up or wants to start over again, ESC should provide a fool
proof way to exit back to the main menu from anywhere in the program.
Here's my code:
#include <iostream>
#include <cstdlib>
#include <conio.h> // For getsch()
#include <cstring> // For strlen()
using namespace std;
#define MAXSIZE 200 // Defining maximum range of the array
stringarray, emptystring and copystringarray.
#define ESC 27
// Program functions.
void enter() ; // Enter string into array function.
void display(); // Display array contents function.
void erase() ; // Earse contents of array function.
void displaychar() ; // Display character at location function.
void empty() ; // Is the array empty function.
void display10char() ; // Display the first 10 characters of the array
function.
void displayrange() ; // Display characters from array within a given
range function.
void replacechar() ;// Charcter to replace function.
void reverse() ; // Reverse string function.
// Declaring global variables.
// Global variables for enter array.
char stringarray[MAXSIZE] ; // Array to hold the string.
// Global variables for earse array.
char emptystring[MAXSIZE] = "\0" ; // Empty array to copy to
stringarray.
// Global variables for display characater at location.
int length ; // The length in numbers of the array.
// Global variables for is the array empty function.
int test ; // Testing if character is a space.
// Global varaible for reverse string funtion.
char copystringarray[MAXSIZE] = "" ; // Copy of orginal array +
contents.
int main() { // Start main function.
// Declaring variables for main function.
char menuselect ; // Menu selction character.
for(; { // Start for lop for menu.
do { // Start do loop for menu.
cout << " String Manipulation: Lab 2 by Kylie Kavanagh 3052463
\n \n" ;
cout << " e) Enter data into the array \n" ;
cout << " d) Display current contents of array \n" ;
cout << " r) Erase current contents of the array. \n \n" ;
cout << " c) Display chracter at location specified \n" ;
cout << " i) Is the array empty? \n" ;
cout << " t) Display the first 10 characters of the array \n"
;
cout << " f) Display characters in range specified \n \n" ;
cout << " j) Replace specified characters with other specified
characters \n \n" ;
cout << " v) Display the string in reverse order \n" ;
cout << " q) Quit this program \n \n" ;
cout << " ESC - Go back to main menu \n" ;
cout << " \n This is the current array contents: " <<
stringarray << "\n" ;
cout << " \n Enter selection: " ;
menuselect = getch() ;
} while (menuselect < 'a' || menuselect > 'z' && menuselect !=
'q') ;
if(menuselect == 'q') break;
cin.ignore(); // Clearing cin buffer.
cout << "\n" ;
switch(menuselect) { // Start switch statement for the menu.
case 'e': // Enter data into the array.
enter() ; // Calling function enter().
break ;
case 'd': // Display current contents of array.
display() ; // Calling function display().
break ;
case 'r': // Earse current contents of array.
erase() ; // Calling function erase() .
break ;
case 'c': // Display character at location number:
displaychar() ; // Calling function displaychar().
break ;
case 'i': // Is input array empty?
empty() ; // Calling function empty().
break ;
case 't': // Display first 10 characters of input array.
display10char() ; // Calling function display10char().
break ;
case 'f': // Display characters in range.
displayrange() ; // Calling function displayrange().
break ;
case 'j': // Replace 'g' with 'h'.
replacechar() ; // Calling function replacechar().
break ;
case 'v': // Display string in reverse order.
reverse() ; // Calling function reverse().
break ;
} // End switch statement for menu.
} // End infinite for loop for menu.
return 0;
} // End main function.
void enter() { // Start enter string into array function.
cout << " Enter string into array: " ;
cin.ignore(); // Clearing cin buffer.
cin.getline(stringarray , MAXSIZE , '\n') ;
// if (cin.getline(stringarray) == ESC) return ;
cout << "\n You entered the string : \n " ;
cout << stringarray << ends;
cout << "\n \n" ;
system("PAUSE");
} // End enter string into array function.
void display() { // Start display contents of array.
cout << " Display contents of array : \n " ;
cout << stringarray << ends;
cout << "\n \n " ;
system("PAUSE");
} // End display contents of array.
void erase(){ // Start of erase contents of array function.
cout << " Current contents of array : \n " ;
cout << stringarray << ends;
cout << "\n \n" ;
strcpy(stringarray , emptystring);
cout << " Contents of array after erasure: " ;
cout << stringarray << ends;
cout << "\n \n" ;
system("PAUSE");
} // End of erase contents of array function.
void displaychar() { // Start display character at location function.
int location ; // Location number of character of array to display.
cout << stringarray << "\n" << ends ; // Displaying the array.
length = strlen(stringarray); // Calculkating the length of the
array.
cout << " \n Enter a location number within the length of the
array (between 1 and " << length << "): " ;
cin.ignore(); // Clearing cin buffer.
cin >> location ;
while(location >= length)
{ // Start while loop.
cout << " \n You did not enter a location number
within the length of the array (between 1 and " << length << ")" ;
cout << " \n Try again: " ;
cin.ignore(); // Clearing cin buffer.
cin >> location ;
} // End while loop.
cout << " \n The character at the location number " << location <<
" is: " << ends; ;
cout << stringarray[location -1] << ends ;
cout << "\n" ;
system("PAUSE");
} // End display character at location function.
void empty() { // Start is the array empty function.
length = strlen(stringarray) ; // Calculating the length of the
array.
test = isalnum(stringarray[0]) ;//test if srray is only a string
if (length > 1)
{ // Start if statement.
cout << " The string is not empty. \n The contents of the
string is: " << stringarray << "\n" ;
} else { // End if statement, start else staement.
if(test != 1)
{ // Start if statement.
cout << " The string is empty. \n There
is nothing in the string, as shown by displaying the string. \n
Contents of string :" << stringarray << "\n" ;
} else { // End if statement, start else
statement.
cout << " The string is not
empty. \n The contents of the string is: " << stringarray << "\n" ;
} // End else statement.
} // End else statement.
cout << "\n" ;
system("PAUSE");
} // End is the array empty function.
void display10char() { // Start display the first 10 characters of the
array function.
cout << " The first ten characters of you array is: " ;
for(int i = 0 ; i < 10 ; i++) cout << stringarray ; //
Displaying the character at i.
cout << "\n" ;
system("PAUSE");
} // End display the first 10 characters of the array function.
void displayrange() { // Start display characters from array within a
given range function.
int startlocation ; // Start location for show array.
int endlocation ; // End location for show array.
int i ; // Control variable for the for loop.
length = strlen(stringarray) ; // Calculating the length of the
array.
cout << " The string is " << length << " characters long. \n From 1
to " << length << " is your valid range. " ;
cout << " \n Enter the start of the range for the array to be shown:
" ;
cin.ignore();
cin >> startlocation ; // Enter start location number.
while (startlocation > length) // Testing if the start location is
within the array length.
{ // Start while loop.
cout << " \n The start location you entered was not in the
arrays range. \n The arrays range is 1 - " << length << " \n Re-enter
the start of the range for the array to be shown: " ;
cin.ignore(); // Clearing cin buffer.
cin >> startlocation ; // Enter start location number.
} // End while loop.
cout << " \n Enter the end of the range for the array to be shown: "
;
cin >> endlocation ; // Enter end location number.
while (endlocation > length) // Testing if the end location is
within the array length.
{ // Start while loop.
cout << " \n The end location you entered was not in the
arrays range. \n The arrays range is 1 - " << length << " \n Re-enter
the end of the range for the array to be shown: " ;
cin.ignore(); // Clearing cin buffer.
cin >> endlocation ; // Enter end location number.
} // End while loop.
while(startlocation > endlocation)
{ // Start while loop.
cout << " \n The start location must be a smaller number than
the end location within \n the valid range : 1 - " << length << " " ;
cout << " \n Enter the start of the range for the array to be
shown: " ;
cin >> startlocation ; // Enter start location number.
while (startlocation > length) // Testing if the start
location is within the array length.
{ // Start while loop.
cout << " \n The start location you
entered was not in the arrays range. \n The arrays range is 1 - " <<
length << " \n Re-enter the start of the range for the array to be
shown: " ;
cin.ignore(); // Clearing cin buffer.
cin >> startlocation ; // Enter start
location number.
} // End while loop.
cout << " \n Enter the end of the range for the array to be
shown: " ;
cin >> endlocation ; // Enter end location number.
while (endlocation > length) // Testing if the end
location is within the array length.
{ // Start while loop.
cout << " \n The end location you
entered was not in the arrays range. \n The arrays range is 1 - " <<
length << " \n Re-enter the end of the range for the array to be
shown: " ;
cin.ignore(); // Clearing cin buffer.
cin >> endlocation ; // Enter end
location number.
} // End while loop.
} // End while loop.
cout << " \n Your selected range was: " << startlocation << " to "
<< endlocation ;
cout << " \n The contents of this range is : " ;
for(i = (startlocation -1 ) ; i < endlocation ; i++) cout <<
stringarray ; // Displaying the character at i.
cout << "\n \n" ;
system("PAUSE");
} // End display characters from array within a given range function.
void replacechar() { // Charcter to replace function.
char chartoreplace ; // Charcter to replace.
char chartoreplacewith ; // Charcter to replace with.
int dectect ; // Control varaibel for sorting loop
cout << " This is you current string: " << stringarray ;
cout << " \n Enter the character you wish to replace (g): " ;
cin.ignore(); // Clearing cin buffer.
cin >> chartoreplace ;
cout << " \n Enter the chracter you wish to replace (g) with (h):
" ;
cin.ignore(); // Clearing cin buffer.
cin >> chartoreplacewith ;
cout << " Replace " << chartoreplace << " with " <<
chartoreplacewith << "\n" ;
length = strlen(stringarray); // Calculating the length of the
array.
cout << "\n This is your changed string: " ;
for(dectect = 0 ; dectect < length ; dectect++)
{ // Start for loop.
if (stringarray[dectect] == chartoreplace)
stringarray[dectect]= chartoreplacewith ;
cout << stringarray[dectect] ;
} // End for loop.
cout << "\n" ;
system("PAUSE");
} // Charcter to replace function.
void reverse() { // Start reverse string function.
char *startofarray ;// Pointer at the start of the string.
char *endofarray ; // Pointer at the end of the string.
char temporary ; // Temporary storage of array elemnet while swapping
around.
cout << " Current array contents: " << stringarray << "\n" ;
/*
strcpy(copystringarray, stringarray) ; // Copying string to keep
orignal string and reverse the copied string.
length = strlen(copystringarray) ; // Finding the length of the
copied string.
startofarray = copystringarray ; // Giving the pointer the
addresss of the start of the array.
endofarray = ©stringarray[length - 1] ; // Giving the pointer
the address of the end of the array, minus the null character.
while (startofarray < endofarray) // Compares start and end
pointer for the condittion expression.
{
temporary = *startofarray ;
*startofarray = *endofarray ;
*endofarray = temporary ;
startofarray++ ; // Moves the pointer up one in the
array index.
endofarray-- ; // Moves the pointer down one in the
array index.
} // End of while loop.
cout << "\n Reversed array: " << copystringarray << "\n" ;
cout << "\n" ;
*/
length = strlen(stringarray) ; // Calculating the length of the
array.
cout << " Your array backwards: " ;
for(int i = length -1 ; i >= 0 ; i--) cout << stringarray ;
// Displaying the character at i.
cout << "\n" ;
system("PAUSE");
} // End reverse string function.