Archived

This topic is now archived and is closed to further replies.

using arrays and functions

This topic is 5578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need help passing an array to a function, and then getting corresponding soundex code. I am having problems passing the array to the function, and getting the array from the user. here is what I have done so far (with errors, which you guys will figure out right away). The main program asks the user for a word (which should be put in an array), and then gives the corresponding code. It does this 10 times.
      
// This program accepts 10 words from the user and outputs the corresponding soundex code


#include <iostream.h>
#include <ctype.h>

// code function takes in the word from the user and returns the soundex code for that word

// input:  a char array of the word from the user

// output: nothing

// return: a char array of the soundex code for that word

char* code(char *array);

// the main function takes a word from the user and calls the code function. It does this 10 times

int main()
{
	// a char variable for the word input by the user

	char word[30];

	for (int i = 1; i < 11; ++i)
	{
		cout << "Please enter word number " << i << ": ";
		cin >> word;
		cout << endl;

		cout << "The soundex code for " << word << " is: " << code(word) << endl;

	}
	return 0;
}

char* code(char *arr)
{
	// create the return array and set it to the default soundex code

	char rval[]= new char [30];

	// create an array for the initial conversion

	char sound[30] = {0};
	
	for (int i = 0; arr[i] != '\0'; ++i)
	{
		switch (tolower(i))
		{
		case 'b':
			sound[i] = 1;
			break;
		case 'p':
			sound[i] = 1;
			break;
		case 'f':
			sound[i] = 1;
			break;
		case 'v':
			sound[i] = 1;
			break;
		case 'c':
			sound[i] = 2;
			break;
		case 's':
			sound[i] = 2;
			break;
		case 'k':
			sound[i] = 2;
			break;
		case 'g':
			sound[i] = 2;
			break;
		case 'j':
			sound[i] = 2;
			break;
		case 'q':
			sound[i] = 2;
			break;
		case 'x':
			sound[i] = 2;
			break;
		case 'z':
			sound[i] = 2;
			break;
		case 'd':
			sound[i] = 3;
			break;
		case 't':
			sound[i] = 3;
			break;
		case 'l':
			sound[i] = 4;
			break;
		case 'm':
			sound[i] = 5;
			break;
		case 'n':
			sound[i] = 5;
			break;
		case 'r':
			sound[i] = 6;
			break;
		default:
			sound[i] = '-';
			break;
		}

	}

	if (isdigit(arr[0]))
		rval[0] = 'Z';
	else
		rval[0] = toupper(arr[0]);
	
	for (int j = 1; sound[j] != '\0'; ++j)
	{
		if (sound[j] == '-')
			// break

		else if (sound[j] == sound[j-1])
			// break

		else 
			for (int k = 1; rval[k] != '\0'; ++k)
			{
				rval[k] = sound[j];
			}
	}

	

	return rval;
}      
Once I get this to work I will work on the function to get the soundex. I am getting errors right now about converting a char of one size to another char. Thanks for your help. [edited by - -silent- on September 10, 2002 6:34:47 PM] [edited by - -silent- on September 10, 2002 7:09:04 PM] [edited by - -silent- on September 10, 2002 7:11:24 PM] [edited by - -silent- on September 10, 2002 9:11:49 PM]

Share this post


Link to post
Share on other sites
1. Use the [ source ] tags when you put up code.
2. char wordarr[] = word; This is just plain wrong. ''word'' is a run-time value. You can''t initialize the array with anything that is not known at compile-time
3. You should use std::string to handle strings.
4. If you really insist on using arrays, it''s easier to pass pointers around instead of complete arrays.

Cédric

Share this post


Link to post
Share on other sites
I'm still having problems. It's saying that:

'return' : cannot convert from 'char [6]' to 'char'

I'm sorry, this is probably pretty easy, but I am still new to C++.

edit: yeah, i'm supposed to use arrays so that i can modify one in the function.

Thanks for your help.

[edited by - -silent- on September 10, 2002 6:53:42 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
an array name without square brackets is a pointer to the first location in the array


   wordarr = word   

whatever you do to wordarr will be done to word since you''ve just assigned wordarr the address of word,
if you want to copy, use strcpy(char *, char *);

Share this post


Link to post
Share on other sites
well what i want to do here is this:

(1) Get a word from the user, and place in array

(2) Pass the array to the function

(3) Create another array that will change the word to the code values

(4) Create one last array for the return value after applying the soundex code rules.


obviously i dont fully understand arrays and pointers. so i need some help creating an array for getting the word from the user, and then passing it to the function. right now i am trying to use pointers, but still no luck.


all of your help is greatly appreciated. i am learning a lot.

Share this post


Link to post
Share on other sites
quote:
Once I get this to work I will work on the function to get the soundex. I am getting errors right now about converting a char of one size to another char.


You will not be able to make it work because you are doing a few things the wrong way. Let's take this one step at a time...


    
char code(char arr[])
{
// create the return array and set it to the default soundex code

char rval[]="Z0000";

return rval;
}


The array named "rval" will be created when the "code" function begins executing and destroyed immediately after the "code" function returns. Therefore, it is a really bad idea to return it. You cannot be completely sure that the array will be valid.

The solution to this problem is that you should either modify "arr" and return that or allocate a new array on the free store and return that. You allocate arrays on the free store using the C++ operator new.

It is not a good idea to declare function arguments with subscripts. It is much better to declare those arguments as pointers because they can deal with both arrays allocated on the free store and those allocated on the stack (which is what you are familiar with).

Here is the code for your function:


  
char* code (char* arr)
{
// create the return array


char rval = new char [30];

// set it to the default soundex code


// <look up string functions>


return rval;
}


You will have to look up the C strXXX functions such as strcpy, strcmp, etc. to figure out how to use them. I haven't touched them in a long, long time...

Remember, at some point you must delete the memory that you have allocated with new. To be able to do this, you must save the return value from your "code" function into a pointer in your other function, so that you can call delete on it.

[edited by - null_pointer on September 10, 2002 7:31:03 PM]

Share this post


Link to post
Share on other sites
thanks for that last post. i'll work on that now.

here is the code that i have worked on so far:


         
// This program accepts 10 words from the user and outputs the corresponding soundex code


#include <iostream.h>
#include <ctype.h>

// code function takes in the word from the user and returns the soundex code for that word

// input: a char array of the word from the user

// output: nothing

// return: a char array of the soundex code for that word

char* code(char *array);

// the main function takes a word from the user and calls the code function. It does this 10 times

int main()
{
// a char variable for the word input by the user

char word[30];

for (int i = 1; i < 11; ++i)
{
cout << "Please enter word number " << i << ": ";
cin >> word;
cout << endl;

cout << "The soundex code for " << word << " is: " << code(word) << endl;

}
return 0;
}

char* code(char *arr)
{
// create the return array and set it to the default soundex code

char rval[]= new char [30];

// create an array for the initial conversion

char sound[30] = {0};

for (int i = 0; arr[i] != '\0'; ++i)
{
switch (tolower(i))
{
case 'b':
sound[i] = 1;
break;
case 'p':
sound[i] = 1;
break;
case 'f':
sound[i] = 1;
break;
case 'v':
sound[i] = 1;
break;
case 'c':
sound[i] = 2;
break;
case 's':
sound[i] = 2;
break;
case 'k':
sound[i] = 2;
break;
case 'g':
sound[i] = 2;
break;
case 'j':
sound[i] = 2;
break;
case 'q':
sound[i] = 2;
break;
case 'x':
sound[i] = 2;
break;
case 'z':
sound[i] = 2;
break;
case 'd':
sound[i] = 3;
break;
case 't':
sound[i] = 3;
break;
case 'l':
sound[i] = 4;
break;
case 'm':
sound[i] = 5;
break;
case 'n':
sound[i] = 5;
break;
case 'r':
sound[i] = 6;
break;
default:
sound[i] = '-';
break;
}

}

if (isdigit(arr[0]))
rval[0] = 'Z';
else
rval[0] = toupper(arr[0]);

for (int j = 1; sound[j] != '\0'; ++j)
{
if (sound[j] == '-')
// break

else if (sound[j] == sound[j-1])
// break

else
for (int k = 1; rval[k] != '\0'; ++k)
{
rval[k] = sound[j];
}
}



return rval;
}



grrrrr! I hate not understanding all of this.

[edited by - -silent- on September 10, 2002 7:46:45 PM]

[edited by - -silent- on September 10, 2002 7:47:30 PM]

[edited by - -silent- on September 10, 2002 8:19:58 PM]

[edited by - -silent- on September 10, 2002 9:12:26 PM]

Share this post


Link to post
Share on other sites
Hmm...it appears that I edited my previous post after you responded, so please reread my previous post. As for the difference between plain char''s, char arrays, and pointers, I will try to explain.

A pointer is just a variable that holds an address in memory where another variable (presumably, a char) is stored. A char* contains the location of a char. A char array, on the other hand, is a group of char''s arranged contiguously (i.e., one-after-the-other) in memory. This is why char arrays are just perfect for strings:

char array : [0] [1] [2] [3] [4] [5] [6]
char string : "0123456"


That example is a little different from real life, but it will give you an idea. Let''s take a look at your code:


  
01 : int main()
02 : {
03 : // a char variable for the word input by the user

04 : char word[30];
05 :
06 : for (int i = 1; i < 11; ++i)
07 : {
08 : cout << "Please enter word number " << i << ": ";
09 : cin >> word;
10 : cout << endl;
11 :
12 : // create an array for the word

13 : //char wordarr[] = word;

14 :
15 : cout << "The soundex code for " << word << " is: " << code(wordarr) << endl;
16 : }
17 : return 0;
18 : }


For convenience I have numbered the lines. In line 4, we tell the compiler to create an array of 30 char''s and we also give it a name. The compiler manages the memory automatically, so we never have to worry about deallocating it. And when the array is first created, it is filled with just some random values, so we cannot count on what it contains. "word" also functions as a pointer to the first element in the array.

In line 8, you allocate two char arrays and pass them to cout. The compiler will store these directly into the program so that they do not use up much memory.

In line 9, you tell cin to write however many characters the user types into the character array named word. If the user types more than 30 characters, you have a problem because there are only 30 characters in the array. If this happens, Visual C++ will probably detect it and issue you a warning.

In line 13, I have no idea what you are doing. (Don''t get angry, I just don''t run into this much.) I *think* you are simply making another pointer to the array that word contains, but I am not sure.

In line 15, you allocate space for another array and pass it to cout. Next, you pass the pointer named word to cout so that it can output the string that word points to. Then you create another constant array and output that. Finally, you call your function named code and pass it a pointer to the array named word.

Since you pass a pointer to the function named code, the function named code can just modify the word array through that pointer. You do not need to create another array. If you want to know how to create another array, see my previous post. If you need more information on the strXXX functions please see your compiler help file and post more questions here if necessary.

Good luck,

- null_pointer

Share this post


Link to post
Share on other sites
wow, thanks for all that information. that helps sooo much!! on line 13 (i think thats the line) i was trying to create another array with the same values as the input word. that way i could use that one, and not have to change the original.

again, thank you for your help, and ill see what i can do with it!



ok, one thing i noticed that is a problem. a soundex code has only 5 values (1 letter and 4 numbers). that is what rval was going to be. so when i called the function, in came the word, and out went the soundex code.

will it work if i change the new char array size to 6?

[edited by - -silent- on September 10, 2002 8:11:44 PM]

Share this post


Link to post
Share on other sites
in the function itself, i want to make another array (called sound) that is the same size as the word array. this array will contain the code conversions.
how do i make it the same size as the word array (since i dont know how long it is.)

also, after i made some changes, i am getting this error on line 40:

C2440: 'initializing' : cannot convert from 'char *' to 'char []

one other problem i am having is with the isdigit and toupper/lower functions. i am getting the same error as above.

i updated my code above with what i have so far.




[edited by - -silent- on September 10, 2002 8:22:10 PM]

Share this post


Link to post
Share on other sites
quote:
will it work if i change the new char array size to 6?


Yes, it should work just fine.


quote:
in the function itself, i want to make another array (called sound) that is the same size as the word array. this array will contain the code conversions.

how do i make it the same size as the word array (since i dont know how long it is.)


An array does not store its size, just a pointer to the first element. To work around this, strings in C++ store a hidden char value called a null character. I see that you are already using this in your for (; loop.

Basically, most functions just iterate over the string one element at a time until they find the null character, and then they just break from the loop. If you keep a count of how many times the loop executed, then you will know how long the string is. The standard strlen function can do this for you.


quote:
also, after i made some changes, i am getting this error on line 40:

error C2440: ''initializing'' : cannot convert from ''char *'' to ''char []


Well...since you probably have other code in your file that doesn''t tell me very much. I think that the problem occurs here because of how you declare the function parameter:

char* code(char *arr[]) 


You should remove the subscripts because they are not necessary.

Share this post


Link to post
Share on other sites
quote:
one other problem i am having is with the isdigit and toupper/lower functions. i am getting the same error as above.


Well, looking in the docs I do not see any problems with how you are using those functions. Let me know if removing the subscripts (see previous post) changes anything. Sometimes you will get a lot of errors just because the compiler does not understand the line(s) before it (due to earlier errors).

Share this post


Link to post
Share on other sites
well, removing the subscripts gave me less errors. that good, but i am still getting this one:

error C2664: 'code' : cannot convert parameter 1 from 'char [30]' to 'char *[]'

i know i am making a stupid mistake somewhere.

here is all of the code that i have:


    
// This program accepts 10 words from the user and outputs the corresponding soundex code


#include <iostream.h>
#include <ctype.h>

// code function takes in the word from the user and returns the soundex code for that word

// input: a char array of the word from the user

// output: nothing

// return: a char array of the soundex code for that word

char* code(char *array);

// the main function takes a word from the user and calls the code function. It does this 10 times

int main()
{
// a char variable for the word input by the user

char word[30];

for (int i = 1; i < 11; ++i)
{
cout << "Please enter word number " << i << ": ";
cin >> word;
cout << endl;

cout << "The soundex code for " << word << " is: " << code(word) << endl;

}
return 0;
}

char* code(char *arr)
{
// create the return array and set it to the default soundex code

char rval[]= new char [30];

// create an array for the initial conversion

char sound[30] = {0};

for (int i = 0; arr[i] != '\0'; ++i)
{
switch (tolower(i))
{
case 'b':
sound[i] = 1;
break;
case 'p':
sound[i] = 1;
break;
case 'f':
sound[i] = 1;
break;
case 'v':
sound[i] = 1;
break;
case 'c':
sound[i] = 2;
break;
case 's':
sound[i] = 2;
break;
case 'k':
sound[i] = 2;
break;
case 'g':
sound[i] = 2;
break;
case 'j':
sound[i] = 2;
break;
case 'q':
sound[i] = 2;
break;
case 'x':
sound[i] = 2;
break;
case 'z':
sound[i] = 2;
break;
case 'd':
sound[i] = 3;
break;
case 't':
sound[i] = 3;
break;
case 'l':
sound[i] = 4;
break;
case 'm':
sound[i] = 5;
break;
case 'n':
sound[i] = 5;
break;
case 'r':
sound[i] = 6;
break;
default:
sound[i] = '-';
break;
}

}

if (isdigit(arr[0]))
rval[0] = 'Z';
else
rval[0] = toupper(arr[0]);

for (int j = 1; sound[j] != '\0'; ++j)
{
if (sound[j] == '-')
// break

else if (sound[j] == sound[j-1])
// break

else
for (int k = 1; rval[k] != '\0'; ++k)
{
rval[k] = sound[j];
}
}



return rval;
}


i really appreciate all of your help.

[edited by - -silent- on September 10, 2002 9:12:56 PM]

Share this post


Link to post
Share on other sites
yeah, it is for school. i have been working on it all week. as you can see, i am having problems with arrays and pointers. but, i have learned more tonight then the whole time ive been in class this semester.

Share this post


Link to post
Share on other sites
incidently, when you create sound[30], all it has is garbage in it, no null chars, yet. you have to put them in yourself.

Perhaps:

char sound[30];
ZeroMemory(sound,30);

Share this post


Link to post
Share on other sites
thanks ^^ (duh! on my part)
compiler doesnt recognize "ZeroMemory," so i did: char sound[30] = {0};



if i remove the brackets from the prototype does it know that i am passing an array?

*it did give me 2 less errors though. down to 3 and 1 warning





[edited by - -silent- on September 10, 2002 8:56:14 PM]

[edited by - -silent- on September 10, 2002 8:58:07 PM]

Share this post


Link to post
Share on other sites
what are your assumptions about the user input?

will they be entering 5 chars?
more?
are less allowed and handled, or does it need to be at least 5?

I find this "soundex" as intriquing as confusing. Is that supposed to be something real?

--You may want to consider passing in an array to code() to receive the output rather than returning it. Then you can allocate a normal array before calling code and print it out after, thus avoiding dynamic memory which is really needed if the function is creating the array itself.

Share this post


Link to post
Share on other sites
a name of an array IS a pointer, really, to the first element,
so when you call code(arr) you are passing a pointer to the start of the array anyways.

then you can index the pointer as normal.

Share this post


Link to post
Share on other sites
ZeroMemory might need , I forget.
I think
memcpy(sound,0,30)
might work, but you can always set the chars to 0 individually,
just make sure you have one at the end since your loop checks for one, otherwise infinite loop.

BTW is this a console app, I''m assuming, under VC++?

Share this post


Link to post
Share on other sites
well, i guess more information would be helpful huh?

as for the input, the user can enter any length of word, just not over 25 or so characters.


the soundex code is a code of 1 letter and 4 numbers that can be made from any word. first, each letter of the word is converted according to the following code:

letter number
b,p,f,v 1
c,s,k,g,j,q,x,z 2
d,t 3
l 4
m,n 5
r 6
all others nothing

so the code for the word Sullivan would be:
2-44-1-5

the first letter is the first letter of the code.

next, any "nothings" are skipped over. also, if a number is the same as the one before it, it is also skipped. so we get:
2415

lastly, since the code is 1 letter and 4 numbers, we get:
S4150


one other example: tomorrow
would be: T5600

Share this post


Link to post
Share on other sites