Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Passing arrarys from a function to another function.

Reply
Thread Tools

Passing arrarys from a function to another function.

 
 
Foxy Kav
Guest
Posts: n/a
 
      04-25-2004
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.
 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      04-25-2004
"Foxy Kav" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om
> 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");

}

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing what a function returns to another function Bart Nessux Python 5 02-08-2004 07:55 PM
Converting arrarys in Python to arrays in C Simon Foster Python 3 12-18-2003 01:33 AM
Arrarys of reference Tim Clacy C++ 7 11-02-2003 11:58 AM
passing ptr to function to another function Pushkar Pradhan C Programming 4 10-04-2003 09:57 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments