0

i've this code, and when i run it, it produce garbage
how to solve it????

#include <stdio.h>
#include <string.h> 
#include <stdbool.h> // using bool data type

void function (void);
char encode(char);
char decode(char);
void changeSecretCode(char, char);

  char code[27] ={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  char secretCode[27] = {'Z','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A'};


int main ()
{
     function();
     
     system("pause");
     return 0;
}//end of main function

char encode(char c)
{
switch (c)
{
     case 'A' :  return secretCode[0]; break;
     case 'B' :  return secretCode[1]; break;
     case 'C' :  return secretCode[2]; break;
     case 'D' :  return secretCode[3]; break;
     case 'E' :  return secretCode[4]; break;
     case 'F' :  return secretCode[5]; break;
     case 'G' :  return secretCode[6]; break;
     case 'H' :  return secretCode[7]; break;
     case 'I' :  return secretCode[8]; break;
     case 'J' :  return secretCode[9]; break;
     case 'K' :  return secretCode[10]; break;
     case 'L' :  return secretCode[11]; break;
     case 'M' :  return secretCode[12]; break;
     case 'N' :  return secretCode[13]; break;
     case 'O' :  return secretCode[14]; break;
     case 'P' :  return secretCode[15]; break;
     case 'Q' :  return secretCode[16]; break;
     case 'R' :  return secretCode[17]; break;
     case 'S' :  return secretCode[18]; break;
     case 'T' :  return secretCode[19]; break;
     case 'U' :  return secretCode[20]; break;
     case 'V' :  return secretCode[21]; break;
     case 'W' :  return secretCode[22]; break;
     case 'X' :  return secretCode[23]; break;
     case 'Y' :  return secretCode[24]; break;
     case 'Z' :  return secretCode[25]; break;

    } // end switch
 } //end function encode


char decode(char c)
{
   bool found = false; int m=0; 
   char tt = c ;

   while (!found && m < 26)
   {
     	if (c == secretCode[m])
                   {
                       found = true;
                       tt = code[m];
                   }
	m++;
   }
 
   if (!found) 
         printf("Invalid code!");
   return tt;

 } //end function decode

void changeSecretCode(char ori, char baru)
{
   bool found = false; 
   int m=0;
  

   while (!found && m < 26)
   {
     	if (ori == code[m])
                   {
                       found = true;
                       secretCode[m] = baru;
                   }
	m++;
   }
} //end function changeSecretCode

void function (void)
{
  int menu;
  char baru,ori;

  char text[100]; // input data
  char outText[100]; // output data
      
  int j,i;
     
    
       printf("Please choose your menu\n");
       printf("  1. Encode\n");
       printf("  2. Decode\n");
       printf("  3. New Secret Code\n");
       printf("  4. Change Secret Code\n");
       printf("  5. Exit\n");
       
 do
 {
  printf("\n\nSelect Option : ");
  scanf("%d",&menu);
       
        
     if (menu==1)
             {
              printf("Enter message to encode : ");
              fflush(stdin);
              gets(text);
              j = strlen(text);
            
              for (i=0; i < j; i++)
                  {
                      outText[i] = encode(text[i]);
                  }
             
              printf("%s is encoded as %s \n",text, outText);
             }


     if (menu==2)
             {
              printf("Enter message to decode : ");
              fflush(stdin);
              gets(text);
              j = strlen(text);

              for (i=0; i < j; i++)
                  {
                      outText[i] = decode(text[i]);
                  }

             printf("%s is decoded as %s \n",text, outText);
             }
     
     
     if(menu==3)  // print the array
             {
               printf("\nSecret Code\n");
               
                  for(i=0;  i<26; i++) 
	                  printf("%c ",code[i]);
	                  
	           printf("\n");

	           for(i=0;  i<26; i++) 
	               printf("%c ",secretCode[i]);
	               
               printf("\n");
	
             }
             
     if(menu==4) // change the code
            {
              printf("Replace secret code for : ");
              fflush(stdin);
              scanf("%c",&ori);
                    
              printf("New code for %c :  ", ori);
              fflush(stdin);
              scanf("%c",&baru);
              
	            // change to new secret code
              changeSecretCode(ori, baru);
              
              printf("\nNew Secret Code\n");
              
              for(i=0;  i<26; i++) 
	              printf("%c ",code[i]);
	              
	          printf("\n");

              for(i=0;  i<26; i++) 
	             printf("%c ",secretCode[i]);
	             
           	  printf("\n");

             }
     
       
 }while (menu==1 || menu==2 || menu==3 || menu==4);
     
}   //end of function
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by prvnkmr449
0
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
//#include <stdbool.h> // using bool data type

void function (void);
char encode(char);
char decode(char);
void changeSecretCode(char, char);

  char code[27] ={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  char secretCode[27] = {'Z','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A'};


int main ()
{
     function();

     system("pause");
     return 0;
}//end of main function

char encode(char c)
{
switch (c)
{
     case 'A' :  return secretCode[0]; break;
     case 'B' :  return secretCode[1]; break;
     case 'C' :  return secretCode[2]; break;
     case 'D' :  return secretCode[3]; break;
     case 'E' :  return secretCode[4]; break;
     case 'F' :  return secretCode[5]; break;
     case 'G' :  return secretCode[6]; break;
     case 'H' :  return secretCode[7]; break;
     case 'I' :  return secretCode[8]; break;
     case 'J' :  return secretCode[9]; break;
     case 'K' :  return secretCode[10]; break;
     case 'L' :  return secretCode[11]; break;
     case 'M' :  return secretCode[12]; break;
     case 'N' :  return secretCode[13]; break;
     case 'O' :  return secretCode[14]; break;
     case 'P' :  return secretCode[15]; break;
     case 'Q' :  return secretCode[16]; break;
     case 'R' :  return secretCode[17]; break;
     case 'S' :  return secretCode[18]; break;
     case 'T' :  return secretCode[19]; break;
     case 'U' :  return secretCode[20]; break;
     case 'V' :  return secretCode[21]; break;
     case 'W' :  return secretCode[22]; break;
     case 'X' :  return secretCode[23]; break;
     case 'Y' :  return secretCode[24]; break;
     case 'Z' :  return secretCode[25]; break;

    } // end switch
 } //end function encode


char decode(char c)
{
   int found = 0; int m=0;
   char tt = c ;

   while (!found && m < 26)
   {
        if (c == secretCode[m])
                   {
               found = 1;
                       tt = code[m];
                   }
    m++;
   }

   if (!found) 
         printf("Invalid code!");
   return tt;

 } //end function decode

void changeSecretCode(char ori, char baru)
{
   int found = 0;
   int m=0;


   while (!found && m < 26)
   {
        if (ori == code[m])
                   {
               found = 1;
                       secretCode[m] = baru;
                   }
    m++;
   }
} //end function changeSecretCode

void function (void)
{
  int menu;
  char baru,ori;

  char text[100]; // input data
  char outText[100]; // output data

  int j,i;


       printf("Please choose your menu\n");
       printf("  1. Encode\n");
       printf("  2. Decode\n");
       printf("  3. New Secret Code\n");
       printf("  4. Change Secret Code\n");
       printf("  5. Exit\n");

 do
 {
  printf("\n\nSelect Option : ");
  scanf("%d",&menu);


     if (menu==1)
         {
          printf("Enter message to encode : ");
          fflush(stdin);
          gets(text);
          j = strlen(text);

          for (i=0; i < j; i++)
          {
              outText[i] = encode(text[i]);
          }

          printf("%s is encoded as %s \n",text, outText);
          printf("\n\nSelect Option : ");
  scanf("%d",&menu);

         }


     if (menu==2)
         {
          printf("Enter message to decode : ");
          fflush(stdin);
          gets(text);
          j = strlen(text);

          for (i=0; i < j; i++)
          {
              outText[i] = decode(text[i]);
          }

         printf("%s is decoded as %s \n",text, outText);
         printf("\n\nSelect Option : ");
  scanf("%d",&menu);

         }


     if(menu==3)  // print the array
         {
           printf("\nSecret Code\n");

          for(i=0;  i<26; i++)
              printf("%c ",code[i]);

           printf("\n");

           for(i=0;  i<26; i++)
               printf("%c ",secretCode[i]);

           printf("\n");
           printf("\n\nSelect Option : ");
  scanf("%d",&menu);


         }

     if(menu==4) // change the code
        {
          printf("Replace secret code for : ");
          fflush(stdin);
          scanf("%c",&ori);

          printf("New code for %c :  ", ori);
          fflush(stdin);
          scanf("%c",&baru);

            // change to new secret code
          changeSecretCode(ori, baru);

          printf("\nNew Secret Code\n");

          for(i=0;  i<26; i++)
              printf("%c ",code[i]);

          printf("\n");

          for(i=0;  i<26; i++)
             printf("%c ",secretCode[i]);

          printf("\n");
          printf("\n\nSelect Option : ");
  scanf("%d",&menu);


         }


 }while (menu==1 || menu==2 || menu==3 || menu==4);

}   //end of function

Edited by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.