error: no return statement in function returning non-void [-Werror=return-type]| |
just saying...
Here what you should also do:
Input:
output:
desired output:
right?
So... how can we fix this... as I said, your function really does a little too much.
But then, here is your main function indented properly:
1 2 3 4 5 6 7 8 9 10 11
|
int main()
{
char arr[99]; int len;
cout<<"Enter the string.\n";
cin.getline(arr,98); // leave room for '\0'
// did we really want to call swamp for each character in our input?
// probally not. We just wanted to count the characters:
for(len=0; arr[len]!='\0'; len++) //; add semicolon here
swamp(arr,len);
return(0);
}
|
see? Alright we fixed that. But there's still a weird space inbetween our output. Interestingly, it is always after the last letter (and then gets put upfront once of corse).
Is this maybe a bug in cin::getline? Probably not.
So we look at the swamp code. Do you know what 'len' is? Do you know how it is related to the indexing of your array? If len is 5, which indices can we use on our array to access the characters?
So, when lets look at first loop:
if x is 5 (length)
k:
0 1 2 3 4 5
|
But since we start counting at zero, we can only go to 4 (makes a total of 5 iterations).
(unless you want to rinse and repeat the process)
Next loop:
This seems right, since we only have the indices 0, 1, 2, 3, 4.
Or does it? Let's look at what is inside the loop:
whoops. So this is where our famous space is coming from. During the last iteration we go outside of our bounds, and assign the '\0' to the last character in the array. Let's fix that and only iterate until we reached the one-before-last character. (there are two ways to make the loop work)
Last loop, which prints our array:
We can't really see anything wrong with this, but we know it prints characters which don't belong to our word itself.
I hope you will be able to fix your code.