Trouble with pass by reference

Syntax help with Pass by reference

Page 1 of 1

3 Replies - 545 Views - Last Post: 23 October 2009 - 08:12 PM Rate Topic: -----

#1 ifeatu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-September 09

Trouble with pass by reference

Post icon  Posted 23 October 2009 - 06:57 PM

I need help with passing by reference, I keep getting corrupt output when I try to do the following:

int countClump (string inputString, string &longest)
{
for (int p = 0; p < biggestClump; p++) {
				longest[p] = charRepeat;
				}
				cout << "The string is : " << longest << endl;
}
int main ()
{
biggest = countClump(word,longest);
return;
}


Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with pass by reference

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5959
  • View blog
  • Posts: 23,229
  • Joined: 23-August 08

Re: Trouble with pass by reference

Posted 23 October 2009 - 07:01 PM

What are the values of biggestClump and charRepeat inside your function? Where do they come from? What are their types? What is your expected output vs. your "corrupted" output?
Was This Post Helpful? 0
  • +
  • -

#3 ifeatu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-September 09

Re: Trouble with pass by reference

Posted 23 October 2009 - 07:13 PM

View PostJackOfAllTrades, on 23 Oct, 2009 - 06:01 PM, said:

What are the values of biggestClump and charRepeat inside your function? Where do they come from? What are their types? What is your expected output vs. your "corrupted" output?


Here is my code:

int countClump (string inputString, string &longest)
{
	bool inClump;
	inClump = 0;
	char charRepeat;
	int i, numClumps, currentClumpSize, biggestClump, length;
	numClumps = biggestClump = length = 0;
	currentClumpSize = 1;

	for (i = 0; i < inputString.length(); i++) { //while not eo string
	 	   if ( i == 0) { continue; } //Should prevent buffer underrun
		if (inputString[i] == inputString[i+1] ) { // when two chars match
			if (inClump == 0 ) { // if this is the first time I have encountered a repeat
				numClumps++;
				currentClumpSize++;
				inClump = 1;
			}
			else { //if I have already encountered a repeat
				currentClumpSize++;
			}
		}
		else {  // If this is not a repeat letter
			inClump = 0;
			if (currentClumpSize > biggestClump) { //Compares the current clump's size to the last, largest clump.
			cout << "The value of currentClumpSize is : " << currentClumpSize << endl;
				biggestClump = currentClumpSize;
				cout << "The letter that has the most repeats is: " << inputString[i] << endl;
				charRepeat = inputString[i];
				}
		currentClumpSize = 1;
		}
	}
	for (int p = 0; p < biggestClump; p++) {
		longest[p] = charRepeat;
		}
		cout << "The string is : " << longest << endl;
		
return biggestClump;
}



here is my output
$ ./a.exe
Type in a word or 'q' to quit: mmmmiiiiiiiiiiiiiiiiiiifft
The value of currentClumpSize is : 3
The letter that has the most repeats is: m
The value of currentClumpSize is : 19
The letter that has the most repeats is: i
The string is :
	290 [main] a 3064 _cygtls::handle_exceptions: Error while dumping state (pro
bably corrupted stack)
Segmentation fault (core dumped)

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5959
  • View blog
  • Posts: 23,229
  • Joined: 23-August 08

Re: Trouble with pass by reference

Posted 23 October 2009 - 08:12 PM

for (i = 0; i < inputString.length(); i++) { //while not eo string
            if ( i == 0) { continue; } //Should prevent buffer underrun
        if (inputString[i] == inputString[i+1] ) 

I think you're going to overrun the end of your string there. Could be causing your seg fault.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1