Avatar of sudarshantk
sudarshantkFlag for India

asked on 

if(buff[0] == 'Q') ...Continue

#include <iostream.h>
#include <string.h>
#include <stdio.h>
void fill(char *buff1, int len);

int main(int argc, char* argv[])
{
      char buff1[50];
      char buff[50];
      char *searchString="a";
      int len=0, occurance=0;
      char *p;
      
        for(;;)
        {
               cout << "Enter Series of string. Q to quit" << endl;
                memset(buff, ' ', 50);
               cin >> buff;
              //len=strlen(strstr(buff, searchString));
              if( p=strstr(buff, searchString)){
                     len=strlen(p);
                     printf("%d\n",len);
              }else{
                     printf("(%s) not found in (%s)\n",searchString,buff);
                     len=0;
      cout << buff[0] << endl;  
      if(buff[0] == 'Q')  //Some Problem Here Does'nt exit, 2'nd time onwards
                return 0;
       else
                continue;
      }
             memset(buff1, ' ', strlen(buff1));
             strcpy(buff1, strstr(buff, searchString));
             fill(buff1, len);
             memset(buff, ' ', 50);
             while(len != 0)
             {
                      cout << "We are in line 31" << endl;
      occurance++;
      cout << occurance << endl << endl << endl;      
      cout << buff1 << endl;
      //len=strlen(strstr(buff1+1, searchString));
      if( p=strstr(buff1+1, searchString) ){
      len=strlen(p);
      printf("%d\n",len);
      }else{
             printf("(%s) not found in (%s)\n", searchString, buff1+1);
             len=0;
             break;
      }
                        
      if(len != 0)
      {
               strcpy(buff1, strstr(buff1+1, searchString));
               fill(buff1, len);
      }

      cout << "We are in line 43" << endl;
      //memset(buff1, ' ', len);
             }

       }
      
return 0;
}
void fill(char *buff1, int len)
{
         *(buff1+len)='\0';
}
---------------------------------------------------------
      if(buff[0] == 'Q')  //Some Problem Here Does'nt exit, 2'nd time onwards
------------------------------------------------------------
Please run this code and let me know if you have a fix.
C++

Avatar of undefined
Last Comment
Infinity08
Avatar of ozo
ozo
Flag of United States of America image

buff1 seems to be uninitialized the first time you run
memset(buff1, ' ', strlen(buff1));
which means that this could ovetflow
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ozo
ozo
Flag of United States of America image

if
 cin >> buff;
is longer than 50 characters, p=strstr(buff, searchString) could search through invalid memory
Avatar of Infinity08
Infinity08
Flag of Belgium image

ozo pointed out the main problems. Here's just one more :

>> #include
>> #include
>> #include

Since this is C++, you should be using these includes instead :

        #include
        #include
        #include

and use the namespace qualifier std:: for each cout, endl, etc.
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo