Passing arrarys from a function to another function.

Discussion in 'C++' started by Foxy Kav, Apr 25, 2004.

  1. Foxy Kav

    Foxy Kav Guest

    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.
     
    Foxy Kav, Apr 25, 2004
    #1
    1. Advertising

  2. Foxy Kav

    John Carson Guest

    "Foxy Kav" <> wrote in message
    news:
    > 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)
     
    John Carson, Apr 25, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    627
    John Carson
    Jul 30, 2003
  2. Tim Clacy

    Arrarys of reference

    Tim Clacy, Oct 31, 2003, in forum: C++
    Replies:
    7
    Views:
    426
    Troll_King
    Nov 2, 2003
  3. Pushkar Pradhan

    passing ptr to function to another function

    Pushkar Pradhan, Oct 4, 2003, in forum: C Programming
    Replies:
    4
    Views:
    379
    Mike Wahler
    Oct 4, 2003
  4. Simon Foster
    Replies:
    3
    Views:
    429
    Lonnie Princehouse
    Dec 18, 2003
  5. Robert Cohen
    Replies:
    3
    Views:
    291
    Andrew Durstewitz
    Jul 15, 2003
Loading...

Share This Page