User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 401,727 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,155 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 1407 | Replies: 4 | Solved
Reply
Join Date: Mar 2005
Location: Nebraska, U.S.
Posts: 187
Reputation: stupidenator is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Segmentation Fault

  #1  
Jan 21st, 2006
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;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2005
Posts: 461
Reputation: winbatch is on a distinguished road 
Rep Power: 4
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Segmentation Fault

  #2  
Jan 21st, 2006
trade(word[i]) != '\n' || i < 5)

(don't you want && i< 5 )
Reply With Quote  
Join Date: Mar 2005
Location: Nebraska, U.S.
Posts: 187
Reputation: stupidenator is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: Segmentation Fault

  #3  
Jan 21st, 2006
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: n

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;
}
Reply With Quote  
Join Date: Dec 2005
Posts: 3,393
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 21
Solved Threads: 385
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Segmentation Fault

  #4  
Jan 22nd, 2006
> 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[i] == '\n'))
This performs a comparison, and then passes the boolean result 0 or 1 to the trade function.
Is this what you wanted?
Reply With Quote  
Join Date: Mar 2005
Location: Nebraska, U.S.
Posts: 187
Reputation: stupidenator is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: Segmentation Fault

  #5  
Jan 22nd, 2006
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:20 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC