F
Foxy Kav
Hi everyone, im a first year UNI student doing a programming subject
and im stuck on how to get rid of my global variables, char
stringarray[MAXSIZE] and char emptystring[MAXSIZE]. I was wondering if
anyone could show me a example of how to do this. I was told to pass
the arrays from function to function, but i do not know how ....
Thanxs if you can help.
My code (First two functions only - there's more):
#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.
// Program functions.
void enter() ; // Enter string into array.
void display(); // Display array contents.
void erase() ; // Earse contents of array.
void displaychar() ; // Display character at location.
void empty() ; // Is the array empty.
void display10char() ; // Display the first 10 characters of the
array.
void displayrange() ; // Display characters from array within a given
range.
void replacechar() ;// Charcter to replace.
void reverse() ; // Reverse string.
// 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.
int main() { // Start main function.
char menuselect ; // Menu selction character.
for(; { // Start for lop for menu.
do { // Start do loop for menu.
cout << " String Manipulation: \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: " ;
cin >> menuselect ;
} while (menuselect < 'a' || menuselect > 'z' && menuselect !=
'q') ;
if(menuselect == 'q') break;
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" ;
display(arrayaddress)
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.
and im stuck on how to get rid of my global variables, char
stringarray[MAXSIZE] and char emptystring[MAXSIZE]. I was wondering if
anyone could show me a example of how to do this. I was told to pass
the arrays from function to function, but i do not know how ....
Thanxs if you can help.
My code (First two functions only - there's more):
#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.
// Program functions.
void enter() ; // Enter string into array.
void display(); // Display array contents.
void erase() ; // Earse contents of array.
void displaychar() ; // Display character at location.
void empty() ; // Is the array empty.
void display10char() ; // Display the first 10 characters of the
array.
void displayrange() ; // Display characters from array within a given
range.
void replacechar() ;// Charcter to replace.
void reverse() ; // Reverse string.
// 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.
int main() { // Start main function.
char menuselect ; // Menu selction character.
for(; { // Start for lop for menu.
do { // Start do loop for menu.
cout << " String Manipulation: \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: " ;
cin >> menuselect ;
} while (menuselect < 'a' || menuselect > 'z' && menuselect !=
'q') ;
if(menuselect == 'q') break;
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" ;
display(arrayaddress)
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.