Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Need help with my program it doesn't repeat properly
New Posts  All Forums:Forum Nav:

Need help with my program it doesn't repeat properly

post #1 of 5
Thread Starter 
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:
#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;
}
post #2 of 5
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:
scanf(" %c%*c",&c);



Also, try inputting a 1000+ char string and see what happens. You will overrun your array bounds.
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
post #3 of 5
Thread Starter 
Thank you for the reply.

But i tried using fgets(str, 1000, stdin); instead of scanf still having the same problem
post #4 of 5
Thread Starter 
hahaha sorry bro for the silly question got it done already thanks alot.. smile.gif
post #5 of 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:
scanf(" %c%*c",&c);



Also, try inputting a 1000+ char string and see what happens. You will overrun your array bounds.
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.
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Need help with my program it doesn't repeat properly