Thread: segmentation fault when processing a string

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    2

    segmentation fault when processing a string

    : This program is aimed at printing all the occurences of a particular letter in a sentence when that particular character is typed.(if anybody knows hangaroo game, this program is using the same concept with it)
    :
    : I tried using static to define my temp variable in printpart() functn.
    : It worked half way, cos the static storage temp inside the printpart() function is complainimg of segmentation fault when like 9 characters are printed.
    :
    :
    : #include <iostream>
    : #include <string>
    : #include <cctype>
    : #include <cstdlib>
    :
    : using namespace std;
    :
    : int ARR[6] ;
    : int KOUNT = 0;
    : int WRONG = 0;
    :
    : void printpart(string,char);
    : bool check(char, string);
    : string replace(string);
    : char asterik(char);
    :
    : int main()
    : {int n;
    : char ch;
    : string sentence = "This code prints sentence character by character";
    : cout << "\t" << sentence << endl;
    :
    : /*To check if choice is right and print all the occurence of the
    : * the number chosen in the sentence*/
    :
    : do
    : {cin >> ch;
    : if(check(ch,sentence))
    : {system("clear");
    : printpart(sentence,ch);
    : }
    : else wrong++;
    : }
    : while(wrong != 4);
    :
    : return 0;
    : }
    :
    :
    :
    : void printpart(string sentence,char ch)
    : { int len = sentence.length();
    : static string temp = replace(sentence);
    : int i = 0;
    : while(i < len)
    : {if (ch == sentence[i])
    : temp[i] = ch; //when the number of assigned character is nine it complains of segmentation fault.
    : i++;
    : }
    : cout << "\t" << temp << endl;
    : cout << sentence;
    : }
    :
    : Does anybody know how to go about the question?
    :
    : This is an instance of what I'm saying.
    :
    : string word = "this a feast of life";
    : i want to make it in such a way that if i type 'h'
    : it'll display like this:
    : *h** * ***** ** **** //character printed is 1
    : type f
    : *h** * f**** *f **f* // character printed is 4
    : type a
    : *h** a f*ast *f **f* // character printed is 8
    type e
    it will give segmentation err.

    so the problem is whenever the number of characters(non asterik) printed is 9 or above it will crash ,give segmentation err.
    )

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    code tags

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Can you show us the code for your replace( ) function?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  3. segmentation fault??
    By snappleapple in forum C Programming
    Replies: 9
    Last Post: 04-27-2007, 11:56 PM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM