Passing arrarys from a function to another function.

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.
 
J

John Carson

Foxy Kav said:
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.

You can move

char stringarray[MAXSIZE] ;
char emptystring[MAXSIZE] = "\0" ;

into main and change you functions so they take a character array variable
as an argument, e.g., you would have

void enter(char stringarray[]) {

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");

}

which you would call with:

enter(stringarray);

If you also need emptystring, you would give the function two arguments.

I don't think this change improves matters any in this simple program, but
the technique of passing arguments to functions must be learned.

It is worth pointing out that, within the empty(...) function, the function
argument will not be the actual array variable from main. Rather, the
function calling mechanism copies the address of the array to a local
variable and it is this local variable that you receive as
the function argument. To emphasise this, there is some merit in giving
it --- and every use of it within the function --- a different name, e.g.,

void enter(char lstringarray[]) {

cout << " Enter string into array: " ;
cin.ignore(); // Clearing cin buffer.
cin.getline(lstringarray , MAXSIZE , '\n') ;
// if (cin.getline(lstringarray) == ESC) return ;
cout << "\n You entered the string : \n " ;
cout << lstringarray << ends;
cout << "\n \n" ;

display(arrayaddress)

system("PAUSE");

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top