/* This program accepts two strings from the user, counts the number of *
* characters in each string, and returns a pointer to the longer string*/
#include <stdio.h>
#include <stdlib.h>
int result = 0;
int compare(char *, char *);
int main (void)
{
char first[50], second[50];
/* Takes two strings from the user */
system("clear");
printf("Please enter two names.\n");
printf("After each string, press enter.\n");
scanf("%s", first);
scanf("%s", second);
//Troubleshooting line
printf("\nThe first name you entere is %s\n", first);
printf("\nThe second name you entered is: %s\n", second);
//End troubleshooting
result = compare(first, second);
if (result == 1)
{printf("\nThe longer name is: %s\n", first);}
if (result == 2)
{printf("\nThe longer name is %s\n", second);}
return 0;
}
int compare(char * first, char * second)
{
//Troubleshooting line
printf("\nComing into the function, the first name is: %s\n", first);
printf("\nComing into the function, the second name is: %s\n", second);
//End troubleshooting
char *p, *p2;
int one=0, two=0, count = 0;
int result = 0;
do
{
*p = first[count];
count++;
one++;
} while ( *p != '\0');
/* Troubleshooting; checking variables */
printf("\nAfter the first while loop, first is %s", first);
printf("\nAfter the first while loop, second is %s", second);
count = 0;
printf("\nThe counter has been reset to %d", count);
/* End Troubleshooting check */
do
{
*p2 = second[count];
count++;
two++;
} while (*p2 != '\0');
//Troubleshooting
printf("\nAfter the second while loop, first is %s", first);
printf("\nAfter the second while loop, second is %s", second);
printf("\nThe first name is: %s", first);
printf("\nThe second name is: %s", second);
printf("\nThe variables one & two are %d & %d", one, two);
//End troubleshooting
if (one > two);
{
result = 1;
return result;
}
if (one < two);
{
result = 2;
return result;
}
}
I link the entire program only because I don't know where this error is. The troubleshooting checks isolate the error to the second do/while loop in the compare function, however. Before the loop, the string 'second' is correct; afterwards the output of 'second' is garbage.
The error, hopefully, is something glaringly obvious. I want to understand why this change is happening; please help?

New Topic/Question
Reply




MultiQuote





|