I'm writing a function that compare two c-strings..
unable to complete the function .. please help .. !!
unpredictable output .. !!
i'm doing this .. !!
=> evaluate the length of string 1, and string 2
=> using lesser length as a loop control variable
=> if the strings of same characters then comparing their length.
If i made some modification with the code, gives warning message "not all control paths return value.???"
Please help I'm too much frustrated nOw........
#include <cstring>
#include <iostream>
using namespace std;
int length(char a[])
{
int temp = 0;
for (int i = 0; i !='\0' ; i++)
{
temp ++;
}
return temp;
}
int compare(char a[], char b[])
{
int a1, a2, var, x;
a1 = length(a);
a2 = length(b);
if (a1 > a2)
{
var = a2;
}
else
var = a1;
for (int i = 0; i < var; i++)
{
if (a[i]==b[i])
x = 0;
else if (a[i]<b[i])
return -1;
else if (a[i]>b[i])
return 1;
}
if (x==0)
{
if (a1>a2)
return 1;
if (a2>a1)
return -1;
}
return x;
}
int main()
{
char discard;
const int size = 30;
char a[size], b[size];
cin.get(a, size+1);
cin.get(discard);
cin.get(b, size+1);
length(a); length(b);
cout << compare(a, b);
system("Pause");
return 0;
}