Hi everyone,
I am having a problem with a problem I am trying to write. It seems to compile ok, but then when I run it, I get a Segmentation Fault. i am using arrays and I am attempting to return a pointer to a char array and i am not sure if I am doing it correctly. Any help with the below code to figure out why I am receiving this error is greatly appreciated. Thank you very much in advance.
Nick

#include <iostream>
using namespace std;

char* soundex(const char word[]); // prototype
// possibly need '&'
char trade(char);

int main()
{
   char inp[40];

   for (int i=0; i<10; i++)
   {
       cout << "Enter a word: ";
       cin >> inp[40];

       cout << soundex(inp) << "is here" << endl;
   }

   return 0;
}

// function soundex
// input: a const char[] named word
// output: none
// return: a char array or pointer to the soundex code for that word
char* soundex(const char word[])
{
   static char rval[6];

   if (trade(word[0]) != 'Z')
       rval[0] = word[0];
   else
       rval[0] = 'Z';

   int i = 1;

   while (trade(word[i]) != '\n' || i < 5)
   {
       char tmp = trade(word[i]);
       if (trade(rval[i-1]) != tmp)
           rval[i] = tmp;
       else
           rval[i] = '0';

       i++;
   }
   // rval[6] will automatically be changed to \n at the end.

   rval[5] = '\n';

   return rval;

}

//function trade
// input: char - a letter from the word
// output: none
// return: the soundex translation of the letter
// This is just the function that holds the switch statement
char trade(char c)
{
   char ret = '0';
   switch(c)
   {
       case 'b': case 'p': case 'f': case 'v':
       case 'B': case 'P': case 'F': case 'V':
           ret = '1';
           break;

       case 'c': case 's': case 'k': case 'g':
       case 'j': case 'q': case 'x': case 'z':
       case 'C': case 'S': case 'K': case 'G':
       case 'J': case 'Q': case 'X': case 'Z':
           ret =  '2';
           break;

       case 'd': case 't': case 'D': case 'T':
           ret = '3';
           break;

       case 'l': case 'L':
           ret = '4';
               break;

       case 'm': case 'n': case 'M': case 'N':
           ret = '5';
           break;

       case 'r': case 'R':
           ret = '6';
           break;

       case '\n':
           ret = '\n';
           break;

       default:
           ret = 'Z';
               break;
   }

   return ret;
}

trade(word) != '\n' || i < 5)

(don't you want && i< 5 )

Thank you for your help. I made that change and I believe I tinkered a little bit more. with it. Now what I get for an output is:

Enter a word: [U]n[/U]

is here

after I enter in even just a single character, i get a blank line (i think it's a '\n' but i'm not sure) and then it says "is here".
any ideas on why it is saying this? I have been running some different things and changing small things and I think that I am having a problem passing a char from the char array to the trade() function and it was having a problem running the switch statement. Thank you for any more help in advance.
Nick

#include <iostream>
using namespace std;

char* soundex(const char word[]); // prototype
// possibly need '&'
char trade(char);

int main()
{
   char inp[40];

   for(int k=0; k<40; k++)
       inp[k] = '\n';

   for (int i=0; i<10; i++)
   {
       cout << "Enter a word: ";
       cin >> inp[40];

   //    cout << trade(inp[0]) << endl;
       cout << soundex(inp) << "is here" << endl;
   }

   return 0;
}

// function soundex
// input: a const char[] named word
// output: none
// return: a char array or pointer to the soundex code for that word
char* soundex(const char word[])
{
   static char rval[6];

   if (trade(word[0]) != 'Z') // this is always coming up false
       rval[0] = word[0];
   else
       rval[0] = 'Z'; // everytime, this is what comes up.

   int i = 1;

   while (/*trade(word[i]) != '\n' && */i < 5)
   {
       if (trade(word[i] == '\n'))
           i = 5;
       else
       {
           char tmp = trade(word[i]);
           if (trade(rval[i-1]) != tmp)
               rval[i] = tmp;
           else
               rval[i] = '0';

           i++;
       }
   }
   // rval[6] will automatically be changed to \n at the end.

   rval[5] = '\n';

   return rval;

}

//function trade
// input: char - a letter from the word
// output: none
// return: the soundex translation of the letter
// This is just the function that holds the switch statement
char trade(char c)
{
   char ret = '0';
   switch(c)
   {
       case 'b': case 'p': case 'f': case 'v':
       case 'B': case 'P': case 'F': case 'V':
           ret = '1';
           break;

       case 'c': case 's': case 'k': case 'g':
       case 'j': case 'q': case 'x': case 'z':
       case 'C': case 'S': case 'K': case 'G':
       case 'J': case 'Q': case 'X': case 'Z':
           ret =  '2';
           break;

       case 'd': case 't': case 'D': case 'T':
           ret = '3';
           break;

       case 'l': case 'L':
           ret = '4';
               break;

       case 'm': case 'n': case 'M': case 'N':
           ret = '5';
           break;

       case 'r': case 'R':
           ret = '6';
           break;

       case '\n':
           ret = '\n';
           break;

       default:
           ret = 'Z';
           break;
   }

   return ret;
}

> cin >> inp[40];
The next problem is you're inputting only a single char, but its off the end of your array.

cin >> inp;
Would input a single word into your array.

> inp[k] = '\n';
This loop does nothing useful.

> if (trade(word == '\n'))
This performs a comparison, and then passes the boolean result 0 or 1 to the trade function.
Is this what you wanted?

Thank you for your help. Those were some overlooked errors I made. Thank you for pointing them out. I got the output looking a little more like it should now, but it's still not right. I need to make some changes and I think I can do that on my own, but I will post if I need any more help. Thank you very much again for your help!
Nick

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.