473,382 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Passing arrarys from a function to another function.

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.
Jul 22 '05 #1
1 2506
"Foxy Kav" <fo*****@hotmail.com> wrote in message
news:32**************************@posting.google.c 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)

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: harry | last post by:
I have an object that's passed in to a function as a parameter i.e public boolean getProjectTitle(ProjectHeader_DTO obj) {...} If I then call a method on this object inside the function i.e...
3
by: Simon Foster | last post by:
I have some code which attempts to convert Python arrays (tuples of tuples of tuples...) etc. into C arrays with equivalent contents. The prototype code is shown below. My only question is, is...
7
by: Tim Clacy | last post by:
I know arrays of references are not allowed, but have a couple of questions: 1) Why aren't arrays of references allowed :-) ? 2) What's a good practical alternative? As an example, Intel's...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
4
by: Pushkar Pradhan | last post by:
I want a function to execute another function, which I pass to it, sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style. double exec_basecase(void (*func)(double *a, double...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
1
by: User1014 | last post by:
Since you can pass a function to a ... erm...... function.... how to you use the result of a function as the argument for another function instead of passing the actual function to it. i.e. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.