(recursive functions) whats wrong with my program?? =(

hi!! can someone help me out?
im trying to make a program using a Recursive function.
in the main(). my program asks the user to enter 2 strings (2 words) and then sends them to the recursive function and there the function outputs the longest word of the two.
so im having trouble with my NULL and '/o' (since i made a string) and when i ask the user to input the second word my program closes!!! im getting really frustrated! =(
PLEASE help!!

#include <iostream>
#include <string.h>
using namespace std;
//this function is supposed to get both strings and decide which one is longer
char * longer(int * st1, int * st2, int i){
if(st1[i]=='/o' || st2[i]=='/0'){
if(st2[i]=='/0')
cout<<st1;
else if (st1[i]=='/0')
cout<<st2;
else
cout<<st2;
return 0;
}
else
longer(st1,st2,++i);

}
int main(){
int st1[15];
int st2[15];
int i=0;
cout<<"enter 2 strings:"<<endl;
cout<<"st1"<<endl;
cin>>st1[15];
cout<<"str2"<<endl;
//when the program gets here it simply stops working! honestly dont understand why!!
cin>>st2[15];
longer(st1,st2,i);

return 0;
}
Last edited on
The first thing I've noticed, is that you have used the letter o instead of the number 0
/o should be /0

EDIT:
Also, when you cin, you want the variable without the element/index;
cin>>st1[15];
should be:
cin>>st1;

EDIT 2:
You're asking for char( string ) and assigning in to int's

You've included <string.h> and don't use it.
Also, string.h is depreciated, use:
#include <string>

EDIT 3:
You declare i in main and pass it to the function, but never change it. My bad, you do! lol.
Last edited on
true!! THANKS haha beginners mistakes!
but i still have a problem with my cin>>st2[15]!
for some reason as soon as the program hits that it stops working (before even touching the function!)
st2[15] is not part of your array. It is off the end of your array. It is memory that is not yours and writing to it trashes some other data.

When you make this:
int st2[15];
the elements are as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
st2[0]
st2[1]
st2[2]
st2[3]
st2[4]
st2[5]
st2[6]
st2[7]
st2[8]
st2[9]
st2[10]
st2[11]
st2[12]
st2[13]
st2[14]
Here, I wrote it. Basically the same as yours, but with a few changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
int main()
{
	char string1[ 99 ];
	char string2[ 99 ];

	std::cout << "Please enter string 1: ";
	std::cin >> string1;

	std::cout << "Please enter string 2: ";
	std::cin >> string2;

	int index = 0;

	stringCheck( string1, string2, index );

	return 0;
}


void stringCheck( char *s1, char *s2, int i )
{
	if( ( s1[ i ] == '\0' ) && ( s2[ i ] == '\0' ) )
	{
		std::cout << "Both strings are the same length.\n";
		return;
	}

	if( ( s1[ i ] == '\0' ) || ( s2[ i ] == '\0' ) )
	{
		if( s1[ i ] == '\0' )
		{
			std::cout << "String 2 is longer.\n";
			return;
		}

		if( s2[ i ] == '\0' )
		{
			std::cout << "String 1 is longer.\n";
			return;
		}
	}

	stringCheck( s1, s2, ++i );
}
I CANT THANK YOU ENOUGH!!!!
=D
Topic archived. No new replies allowed.