Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<stdio.h>
#include<string.h>

void len();// function declaration
void cpy();// function declaration
void cat();// function declaration
void cmp();// function declaration
void rev();// function declaration

void main()    
{    
    int n;

    printf(" 1.Find length of string.\n 2.Copy one string to another.\n 3.Append one string to another.\n 4.Compare two strings.\n 5.Reverse one string\n");

    printf("\nChoose your string option:");   
    scanf("%d",&n);

    printf("\n"); 

    switch(n) {
      case 1: 
        len(); //function call
        break;
      case 2:
        cpy();//function call
        break;
      case 3:
        cat();//function call
        break;
      case 4:
        cmp();//function call
        break;
      case 5:
        rev();//function call
        break;
      default:
        printf("ERROR,Wrong Input!\n");
    }
}
   
void len() // function definition     
{   
    char a[10];
    int a1;
    
    printf("enter a string:");
    gets(a);
    
    a1=strlen(a);// strlen is a string manipulation function for finding the length of the string.
    
    printf("The length of the string is %d",a1);
}    

void cpy() // function definition 
{
    char str1[10],str2[10];
    
    printf("Enter String=");
    gets(str1);
    
    strcpy(str2,str1);      
    printf("The given string is %s",str2);
}
    
void cat() // function definition 
{
    char str1[10],str2[10];
    
    printf("Enter string 1=");        
    gets(str1);
    
    printf("Enter string 2=");       
    gets(str2);
    
    strcat(str1,str2);      
    printf("The New appended String is %s",str1); 
}    

void cmp() // function definition 
{
    char str1[10],str2[10];
    
    printf("Enter string 1=");
    gets(str1);
    
    printf("Enter string 2=");
    gets(str2);

    if(!strcmp(str1,str2)) {
        printf("STRINGS ARE EQUAL");
    } else {
        printf("STRINGS ARE NOT EQUAL");
    }
}   

void rev() // function definition    
{
    char str[10];
    printf("Enter string 1=");
    gets(str);
    strrev(str);
    printf("The reversed string is : %s",str);
}


What I have tried:

So when i run the above code , then in the output screen when i choose any of the option(1-5) it took the other input details required to provide(for eg: i have to input a string to find the length of that string on it's own and show the output without i even entering a string : this is the pic of output problem i am getting :

another output screen with diffrent values

I have also run the same string manipulation function individually in a code , it works fine , and i get all my desired output without any other problem, but when i run this above code all the string manipulation function all together i get problems in my output.
Posted
Updated 22-Dec-20 7:43am
v2

1 solution

You need to flush the input buffer: add the following after the scanf call:
C++
while ((c = getchar()) != '\n' && c != EOF)
  continue;

Note the variable c should be declared above as int c;. I also suggest you add newline characters (\n) to the end of all your printf strings.
 
Share this answer
 
Comments
DEBASISH BORAH 23-Dec-20 11:10am    
thank you very much for your kind help , i am new here so i am learning.
but i want to say is I use two c compiler : 1) Turbo C and other 2) codeblocks

when i used your code as it is , it runs perfectly on Turbo C but the same problem(as i have already mentioned on above question) again comes in codeblocks ,
I also had used fflush(stdin) as it also do the same , but no achievement
Can u tell is it a compiler problem or else, and also can you suggest/recommend me good compiler for the same.
Richard MacCutchan 23-Dec-20 11:49am    
If you are on Windows then get a copy of Visual Studio 2019 Community Edition. It is free from Microsoft and contains a fully compliant C/C++ compiler.
DEBASISH BORAH 25-Dec-20 10:35am    
ok thank you very much , your help means a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900