Jump to content
  • Advertisement

Archived

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

-silent-

using arrays and functions

This topic is 5887 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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!