Thread: still problems with ceasar shift

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    15

    still problems with ceasar shift

    Hey there again guys,

    I am still having a little trouble with this ceasar shift program that I am doing for uni. I have got most of the program down and it seems to work pretty well, there is just one small glitch that I cannot seem to get around, and it is something relatively strait forward. The problem is when a space is encountered it seems to get processed in the function decryptEncryptLine() and the output is moved along and is not right.

    input = my name is trevor
    output = PB BQDPH HLV VWUHYRU

    it seems that whenever i try and incorporate a way of ignoring spaces the output ends up all over the place.

    Can someone run my code and look over it and point me in the direction that I should be heading. Its a work in progress and I have only been learning C for a couple of months.

    Thanks heaps

    Code:
    //------------------------------------------------------------------------------
    // Author:      Airmax (DON'T USE YOUR REAL NAME OR STUDENT NUMBER
    // Student No.: 12345678          FOR PEER REVIEWED ASSIGNMENTS.)
    // Assessment:  Assignment 4
    // Purpose:     A program to accept alpha characters and encrypt or decrypt them
    //              using a ceasar shift cycle.
    //------------------------------------------------------------------------------
    
    // included libraries
    #include <stdio.h>
    #include <ctype.h>
    //------------------------------------------------------------------------------
    // constants
    const int UPPER_LIMIT = 3; // Maximum menu choice
    const int LOWER_LIMIT = 0; // Minimum menu choice
    const int LOWEST_SHIFT = 1; // Lowest shift possible
    const int HIGHEST_SHIFT = 25; // Highest shift possible
    //------------------------------------------------------------------------------
    
    
    //------------------------------------------------------------------------------
    // Function prototypes
    
    int getValidIntegerInRange(int lowestAllowed, int highestAllowed,char *prompt);
    // Purpose:   To make sure that the user has entered a valid 
    //            integer within range
    // Arguments: Minimum and maximum allowable integers and pointer
    // Ret.Val:   returns a valid menu choice
    // Pre:       lowest and highest int are declared and an array storing the 
    //            prompt must exist 
    // Post:      The valid menu choice is passed back to main()  
    //------------------------------------------------------------------------------
    
    
    //------------------------------------------------------------------------------
    int getShiftValue(int minVal, int maxVal);
    // Purpose:   To get a valid shift value from the user
    // Arguments: Minimum and maximum allowabe integers
    // Ret.Val:   A shift value
    // Pre:       minVal and maxVal must be declared
    // Post:      The appropriate shift value will be returned        
    //------------------------------------------------------------------------------
    
    
    //------------------------------------------------------------------------------
    void decryptEncryptLine(int shift);
    // Purpose:   To encrypt or decrypt text using a ceasar shift cycle
    // Arguments: The shift value requested by the user
    // Ret.Val:   Void
    // Pre:       The shift value has been recieved from the user
    // Post:      The encrypted or decrypted text will output to the terminal           
    //------------------------------------------------------------------------------
    
    
    //------------------------------------------------------------------------------
    //main
    int main() {
      int choice;
      int choice2;
      int shiftValue=3; // default shift value
      char menu[] = "\nMenu\n====================\n 1. Enter shift value\n 2. "
                    "Encode\n 3. Decode\n 0. Quit\n====================\nChoice: \0";
      
      while (choice != 0) {
      
       // get valid menu choice
       choice = (getValidIntegerInRange(LOWER_LIMIT, UPPER_LIMIT, menu));
       
      
      switch (choice) {
             
             case 0:
             return 0;
             break;
       
             case 1:
             shiftValue = getShiftValue(LOWEST_SHIFT, HIGHEST_SHIFT);
             choice2 = (getValidIntegerInRange(2, 3, menu));
                               
                     if (choice2 == 2) {
                        decryptEncryptLine(shiftValue);
                        }
                   
                     else if (choice2 == 3) {
                     decryptEncryptLine(-(shiftValue));
                     }
             break;
      
      
             case 2:
             decryptEncryptLine(shiftValue);
             break;
             
             case 3:
             decryptEncryptLine(-(shiftValue));
             break;
             
             }
      }
      return 0;
      
    }        
    //------------------------------------------------------------------------------
    
    
    
    //------------------------------------------------------------------------------
    // Functions
    //------------------------------------------------------------------------------
    
    
    //------------------------------------------------------------------------------
    // function to get valid menu choice and re prompt if input not valid
    int getValidIntegerInRange(int lowestAllowed, int highestAllowed,char *prompt) {
      int menuChoice;
      int inputsGathered;
      
      // get users choice
      printf("%s\n", prompt);
      inputsGathered = scanf("%i",&menuChoice);
      
      //validate users input
      while(inputsGathered !=1 || menuChoice < lowestAllowed
                               || menuChoice > highestAllowed) {
        
        // clear input pipe
        scanf("%*[^\n]");
        scanf("%*c");
        
        //allow user to re enter inout
        printf("Invalid input. Please enter a choice between %i and %i: "
               ,lowestAllowed, highestAllowed);
        inputsGathered = scanf("%i", &menuChoice);
      }
      
     // clear input pipe
     scanf("%*[^\n]");
     scanf("%*c");
     
     //return users choice
     return menuChoice;
    }
    //------------------------------------------------------------------------------
    
    
    //------------------------------------------------------------------------------
    // a function to get the user to input the shift value
    int getShiftValue(int minVal, int maxVal) {
      int shiftVal;
      int inputsGathered;
      
      
      // get users shift value
      printf("Please enter a shift value between %i and %i: ", minVal, maxVal);
      inputsGathered = scanf("%i",&shiftVal);
      
      //validate users input
      while(inputsGathered !=1 || shiftVal < minVal || shiftVal > maxVal) {
        
        // clear input pipe
        scanf("%*[^\n]");
        scanf("%*c");
        
        //allow user to re enter inout
        printf("Invalid input. Please enter a shift value between"
               " %i and %i: ", minVal, maxVal);
        inputsGathered = scanf("%i", &shiftVal);
      }
      
     // clear input pipe
     scanf("%*[^\n]");
     scanf("%*c");
     
     //return users choice
     return shiftVal;
    }
    //------------------------------------------------------------------------------
    
    
    //------------------------------------------------------------------------------  
    // a function to encrypt or decrypt the input string and
    // return the encrypted/encrypted equvilent 
    
    void decryptEncryptLine(int shift) {
      char input;
      char output;
      const char SENT = '\n';
      
      printf("Please enter characters to be encrypted/decrypted\n\n");
      scanf("%c", &input);
      
      
                
      while (input != SENT) { 
            
          if (input == ' ') {
                     printf("%c",input); 
                     }
                     
           else if (input != ' ') {         
              
              input = (toupper(input)); // convert lowercase to uppercase
              output = input + (shift); // apply ceasar shift
              
              
              if (output > 90) { // encode wrap around
                         output -= 26;
                         }
              
              else if (output < 65) { // decode wrap around
                      output += 26;
                      }
              
              }
               
                 
              
             printf("%c",output);
             scanf("%c", &input);
             
             }
              
              
              
    
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, play computer and see what happens when the input is a space:
    Code:
    void decryptEncryptLine(int shift) {
      char input;
      char output;
      const char SENT = '\n';
      
      printf("Please enter characters to be encrypted/decrypted\n\n");
      scanf("%c", &input);
      
      
                
      while (input != SENT) { 
            
          if (input == ' ') {
                     printf("%c",input); 
                     }
                     
           else if (input != ' ') {         
              
              input = (toupper(input)); // convert lowercase to uppercase
              output = input + (shift); // apply ceasar shift
              
              
              if (output > 90) { // encode wrap around
                         output -= 26;
                         }
              
              else if (output < 65) { // decode wrap around
                      output += 26;
                      }
              
              }
               
                 
              
             printf("%c",output);
             scanf("%c", &input);
             
             }
              
              
              
    
    
    }
    The parts in red are what happen when you see a space. Now count the number of print statements.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Thanks man, i see the problem now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Ceasar Shift program
    By trevordunstan in forum C Programming
    Replies: 11
    Last Post: 09-11-2008, 09:40 PM
  3. Caeser Cipher
    By L_U_K_E in forum C++ Programming
    Replies: 35
    Last Post: 06-30-2006, 07:15 AM
  4. Simulating Shift Key
    By PrimeTime00 in forum Windows Programming
    Replies: 7
    Last Post: 10-15-2004, 05:53 AM
  5. circular shift
    By n00bcodezor in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 03:51 AM