Passing by reference

So I want to take the word I make from the array and turn it into it's proper version of the word through pass by reference but my pass by reference wont work. Anybody have any pointers?



#include <iostream>
#include <cstring>

using namespace std;

//My name is Petar Crljenica, class CS161

void welcome(); //Welcomes the user and explains program
void read_word(char word[]); //The word that the user inputs for use in the program
void proper_name(char & word[]); //The proper name returned to the user




const int SIZE = 20; //Highest amount of letters allowed per word.

int main()
{
char word[SIZE];

welcome();
read_word(word);
proper_name(word);

cin.get();
return 0;
}

void welcome()
{
cout <<"Welcome to the word operations program! " <<endl;
cout <<"This program allows you to decide if you want to make a word proper, an adverb, plural, and strip any punctuation." <<endl;
cout <<"Begin whenever ready." <<endl;
}

void read_word(char word[])
{
cin.get(word,SIZE,'\n');
}

void proper_name(char & word[]) //Puts the name in it's proper form
{
char selection;

if(word[0] >= 65 && word[0] <= 90)
cout <<"It is already proper. " <<endl;
else
{
cout <<"Do you want to make the word proper? Y for yes, any character for no: " <<endl;
cin >> selection;
cin.ignore();
if('Y' == selection || 'y' == selection)
word[0] = toupper(word[0]); //Capatalize's first word
else
cout <<"The word shall not be made proper." <<endl;
cout <<"Your word now looks like: " << word;
}
}

Your worries are in vain! Luckily arrays by default are passed by reference, there is no need to pass an array by reference. It does this because the proper way to writing an array of Chars is char * text, a pointer to char.
So then it would work if I just switched the void from the beginning to a char?
Luckily arrays by default are passed by reference,


Arrays are not passed by reference by default. The name of an array decays to a pointer when fed to a function and information about the type is lost, which is why you often see a size parameter passed at the same time. That's not true when an array is passed by reference.

OP, you don't have any need to pass an array by reference.

void proper_name(char word[]) would work fine for you, but for completion's sake:

void proper_name(char (&word)[SIZE])

would've been the way to pass by reference.
Topic archived. No new replies allowed.