Overclock.net banner

Need help with my program it doesn't repeat properly

252 views 4 replies 3 participants last post by  Mrzev 
#1 ·
Need help with my program it does not repeat properly it doesn't wait for the input after you press y to continue instead it like the below

Output of the program:

Enter first string
s
Enter second string
s
Both strings are the same.
Press 'y' to Continue or press 'n' to Exit. (problem is here when i press y it repeats without asking for the input)
y
Enter first string
Enter second string
Both strings are the same.
Press 'y' to Continue or press 'n' to Exit.

Code:

Code:
#include<stdio.h>

int compare_string(char*, char*);

int main()
{
    char first[1000], second[1000], result;
    char c;

    do
{
    {
    printf("Enter first string\n");
    scanf("%[^\n]%*c", first);

    printf("Enter second string\n");
    scanf("%[^\n]%*c", second);

    result = compare_string(first, second);

    if ( result == 0 )
        {
       printf("Both strings are the same.\n");
        }
    else
        {
       printf("Entered strings are not equal.\n");
       printf( "The location of the error is at: %d\n", result);
        }
        }
         {

    printf( "Press 'y' to Continue or press 'n' to Exit.\n" );
    scanf(" %c",&c);
        while( !( c == 'y' || c == 'n')){

         printf( "Invalid Character Entered!\n" );
         printf( "Please press 'y' to Continue or press 'n' to Exit.\n" );
         scanf(" %c",&c);
        }
        }
            }while ( c != 'n');

    return 0;
}

int compare_string(char *first, char *second)
{
    int count = 1;

   while(*first==*second)
   {
      if ( *first == '\0' || *second == '\0' )
         break;

      first++;
      second++;
      count++;
   }
   if( *first == '\0' && *second == '\0' )
      return 0;
   else
      return count;
}
 
See less See more
#2 ·
Your code leaves the '\n' on the stdin stream instead of consuming it when you ask yes/no. As a result, both your string reads immediately encounter it and move on. Change it to

Code:

Code:
scanf(" %c%*c",&c);
Also, try inputting a 1000+ char string and see what happens. You will overrun your array bounds.

Code:

Code:
//we use 999 because the last char of the array must be '\0'
scanf(" %999[^\n]%*c", str);

//consider using fgets. Note that it automatically subtracts one for the '\0'
fgets(str, 1000, stdin);
//it includes the '\n' as well, but since both strings will always end in '\n'
//it won't mess with the comparison (though you could replace the '\n' with '\0' if you wanted
 
#5 ·
Quote:
Originally Posted by hajile View Post

Your code leaves the '\n' on the stdin stream instead of consuming it when you ask yes/no. As a result, both your string reads immediately encounter it and move on. Change it to

Code:

Code:
scanf(" %c%*c",&c);
Also, try inputting a 1000+ char string and see what happens. You will overrun your array bounds.

Code:

Code:
//we use 999 because the last char of the array must be '\0'
scanf(" %999[^\n]%*c", str);

//consider using fgets. Note that it automatically subtracts one for the '\0'
fgets(str, 1000, stdin);
//it includes the '\n' as well, but since both strings will always end in '\n'
//it won't mess with the comparison (though you could replace the '\n' with '\0' if you wanted
I strongly recommend learning and using regular expressions. You will be surprised on how many places you can do this kind of stuff. Searching trough a word doc, pulling data from a database, filtering out data files.... well worth the investment.
 
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top