Thread: help with strings..kind of

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    help with strings..kind of

    I'm just learning how to do C programming, and so far its been quite easy since i programmed for some games before, but tonight im a little tired, and stumped. Ive been trying to do this lab for my class. It is supposed to prompt the user for a string, and be able to encode, or decode the string. It seems to be encoding fine, but decoding wont work. I can't seem to figure it out, can you please help? Heres the code i have so far...so close i think! Thanks for your time.

    Code:
    // Dependencies ------------------------------------------------------------- 
    #include <stdio.h>  // For input/output functions. 
    #include <string.h>  // For string functions. 
    // End dependencies --------------------------------------------------------- 
    
    
    // Functions ---------------------------------------------------------------- 
    /* --------------------------------------------------------------------------+ 
      main - Displays a menu for selection of printing the message, encoding a
        message, or decoding a message
    
      Returns 
        program status 
    
      Functional Notes 
        getchar() prevents auto closing of window by expecting an Enter keypress. 
      --------------------------------------------------------------------------*/ 
    char in_msg[250] = "";
    char out_msg[250] = "";
    char encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    char decode[] = "HCFGBMNTVZYPKASQUDXIRWEJLOhcfgbmntvzypkasqudxirwejlo";
    char t[250]; 
    int n;
    int j;
    int i;
    int menu_item = 0;
    // ---------------------------------------------------------------------- 
    // Display course work items menu. 
    // ---------------------------------------------------------------------- 
    int main() 
    {
      do 
      {
         printf("\t***Lab 09 - Encode/Decode***\n"); 
         printf("\t***Please choose one of the following functions...***\n"); 
         printf("\tdecrypt       <1>\n"); 
         printf("\tencrypt       <2>\n"); 
         printf("\tquit:         <3>\n"); 
    
         scanf("%d%*c", &menu_item); 
    
         switch (menu_item)
         { 
           case 1 :
           {
             strcpy (t, in_msg);
             n = strlen (in_msg);
               for ( j = 0 ; j < 53 ; j++)
               {
                 for (i = 0 ; i < n ;i++)
                 {
                   if ( t[i] == encode[j] )
                   {
                     t[i] = decode [j];
                   }
                 }
               }  
             strcpy (out_msg, t);  
             printf("***********************************************************************\n");
             printf("Original Message: %s\n", in_msg);
             printf("Decrypted Message: %s\n", out_msg);  
             printf("***********************************************************************\n");
             strcpy (in_msg, out_msg);
             menu_item = 0;
             break;
           } 
      
          case 2 :
          {
            printf("Please input a new message: ");
            gets(in_msg);
            strcpy (t, in_msg);
            n = strlen (in_msg);
              for ( j = 0 ; j < 53 ; j++)
              {
                for(i = 0 ; i < n ;i++)
                {
                  if (t[i] == decode[j])
                  {
                    t[i] = encode [j];
                  }
                }
              }  
            strcpy (out_msg, t);
            printf("***********************************************************************\n");
            printf("Original Message: %s\n", in_msg);
            printf("Decrypted Message: %s\n", out_msg);  
            printf("***********************************************************************\n");
            strcpy (in_msg, out_msg);
            menu_item = 0;
            break;
          }
          case 3 :
          {
            return 0;
          }
          default:
          {
            printf("Not a valid menu choice, please try again");
            break;
          }
        }
    } while (menu_item == 0 );
    
    
    // ---------------------------------------------------------------------- 
    // Standard DOS window exit for when running outside the IDE. 
    // ---------------------------------------------------------------------- 
    getchar();  // Wait for Enter keypress. 
    return 0; 
    } 
    // End functions ---------------------------------------------------------

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I can't seem to figure it out, can you please help? Heres the code i have so far...so close i think! Thanks for your time.
    Guess what! I can't figure it out from reading your description of the issue either. Oh, that would be because you didn't describe the problem.

    I for one, and I know I'm not alone here, do not just read pages of code because someone posts it and says "it doesn't quite work". It doesn't work that way around here.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Hey, I was looking at this code, and this line:
    Code:
    scanf("%d%*c", &menu_item);
    is something I don't think I have seen yet. What is with the %*c ?

    kermit
    Last edited by kermit; 11-05-2003 at 06:49 PM.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    RE

    The * is supposed to throw out the enter when the user presses enter after the integer. Its not really the best way to scan things in because if the user isnt cooperative it would recieve an error, but it works :/

    And as to the other reply, i'm not trying to be in your way or anything. If you dont have time to help me, dont worry about it. No one is saying you have to. I figured out the problem on my own... well... a way around it, i dont know how it could really be programmed yet... But im sorry that I wasnt clear enough. But you dont have to be so mean about it? I was asking for help, not demanding it. I know how it is when people ask you for help and not even try to write the program themselves first--people do it to me all the time. But that was NOT what I was doing nor my intention to make you write the program. I tried, and no matter how much i messed with it, i couldnt figure it out. So if you dont want to help, then DONT, or say in a nice way at least that you dont understand. Theres no need to be mean about it. Ask me to rephrase it if YOU dont understand me.
    The problem was that it would encode the string, but not decode it correctly. I dont know how to explain it any better than that? so instead of really decoding it, i just saved the original input.... kinda cheap to do it that way, id rather it really decode it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing strings to functions and modifying them
    By diddy02 in forum C Programming
    Replies: 6
    Last Post: 08-11-2008, 01:07 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  4. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  5. c-style string vs. c++-style strings
    By Mbrio in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 12:26 PM