Thread: Please help! Novice coder trying to figure c out.

  1. #16
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    Ok, my main code looks like this now. Also for some reason when I input 's' as a encryption type it outputs
    Original word is: good
    Segmentation fault
    But when I try to do 'm' or 'u' the program just terminates, any thoughts?
    Code:
    #include"encrypt.h"//make your function declaration in this .h file
    #include<stdio.h>
    
    int main()
    {
      char input[20], encrypt[20];
      printf("Please enter a sentence to encrypt: ");
      scanf("%s", &input);
    
      printf("Please enter encryption level, 's' for simple, 'm' for medium, or 'u' for ultra:");
      scanf("%s", &encrypt);
    
      if(encrypt[0] == 's')
      {
          printf("Original word is: ");
          printf("%s\n", input);
          printf("Encrypted word is: ");
          simpleEncrypt(*input);
          printf("%s",input);
      }
      else if(encrypt[0] == 'm')
      {
          printf("Original word is: ");
          printf("%s\n", input);
          printf("Encrypted word is: ");
          printf("%s",mediumEncrypt(input));
      }
      else
      {
          printf("Original word is: ");
          printf("%s\n", input);
          printf("Encrypted word is: ");
          printf("%s",ultraEncrypt(input));
      }
        
      return 1;
    }

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Why do you have an array for encrypt when it's only taking a single character? Chance it to a single char

    Code:
      char input[20], encrypt;
    Change the input function to:

    Code:
      scanf("%c", &encrypt);
    Then you'll accept only a single char. Also, change the ifs from checking against a sub-scripted array to checking against a single char variable.
    That should resolve a few problems for now and repost the whole update. I'd like the put it into my compiler to see what is causing the segfault.

  3. #18
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    Ok, I've only changed simpleEncrypt, medium and ultra are still like when I started.
    Header:
    Code:
    /*!
     *  Replaces each letter in the character array wih its next lexicographical
     *  letter.
     *  Sample: "Good" becomes "Hppe"
    !*/
    void simpleEncrypt(char *input);
    
    /*!
     *  Add to each letter with its index in the given input.
     *  Sample: "Good" becomes "Gpqg"
     *
    !*/
    char mediumEncrypt(char input[]);
    
    /*!
     *  Add to each letter with the square of its index in the given
     *  input.
     *  Sample: "Good" becomes "Gpsm"
    !*/
    char ultraEncrypt(char input[]);
    encrypt.c
    Code:
    #include"encrypt.h"
    #include<stdio.h>
    
    void simpleEncrypt(char *input)
    {
      int i, len = strlen(input);
      //for loop goes through all the characters in input and adds 1 to the ascii value 
      //of each character
      for(i = 0;i < len; i++)
        {
          input[i]++;
          //if the character is now ascii value 91 then we must loop around to ascii 
          //value 65. In other words goes from Z to A.
          if(input[i] == 91)
        {
          input[i] = 65;
        }
          //if the character is now ascii value 123 then we must loop around to ascii
          //value 97. In other words goes from z to a.
          else if(input[i] == 123)
        {
          input[i] = 97;
        }
        }
    }
    
    char mediumEncrypt(char input[])
    {
      int i, len = strlen(input);
      //for loop goes through all the letters in the input
      for(i = 0;i < len; i++)
        {
          //casting to the corresponding ascii value
          input[i] = (int)input[i];
          //incrementing the ascii value by its index in the sentence
          input[i] = input[i] + i;
          //since we are assuming sentences less than 20 characters, all we have to 
          //do is subtract the ascii value by 26 to assure we wrap around
          if(input[i] >= 91)
        {
          input[i] = input[i]-26;
        }
          if(input[i] >= 123)
        {
          input[i] = input[i]-26;
        }
          //casting back to a character
          input[i] = (char)input[i];
        }
      return *input;
    }
    
    char ultraEncrypt(char input[])
    {
      int i, len = strlen(input);
    
      //for loop goes through all the characters in the input sentence
      for(i = 0;i < len; i++)
        {
          input[i] = (int)input[i];
          //this integer serves as a boolean flag, if it is 1 then the character is uppercase
          int upper;
          if(input[i] >= 65 && input[i] <= 90)
        {
          upper++;
        }
          //incrementing the ascii value by the square of its position in the sentence
          input[i] = input[i] + (i*i);
          //here we need a while loop to assure we wrap around to the correct value
          if(upper == 1)
        {
          //the while loop decrements input[i] to its corresponding ascii value
          while(input[i] > 90)
            {
              int count;
              count = input[i] - 90;
              input[i] = 65 + count;
            }
        }
          else
        {
          //while loop to assign the correct ascii value if the character was lowercase
          while(input[i] > 122)
            {
              int count;
              count = input[i] - 122;
              input[i] = 97 + count;
            }
        }
          input[i] = (char)input[i];
        }
      return *input;
    }
    main
    Code:
    #include"encrypt.h"//make your function declaration in this .h file
    #include<stdio.h>
    
    int main()
    {
      char input[20], encrypt;
      printf("Please enter a sentence to encrypt: ");
      scanf("%s", &input);
    
      printf("Please enter encryption level, 's' for simple, 'm' for medium, or 'u' for ultra:");
      scanf("%c", &encrypt);
    
      if(encrypt == 's')
      {
          printf("Original word is: ");
          printf("%s\n", input);
          printf("Encrypted word is: ");
          simpleEncrypt(*input);
          printf("%s",input);
      }
      else if(encrypt == 'm')
      {
          printf("Original word is: ");
          printf("%s\n", input);
          printf("Encrypted word is: ");
          mediumEncrypt(*input);
          printf("%s",input);
      }
      else
      {
          printf("Original word is: ");
          printf("%s\n", input);
          printf("Encrypted word is: ");
          ultraEncrypt(*input);
          printf("%s",input);
      }
        
      return 1;
    }

  4. #19
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Code:
    /*!
     *  Add to each letter with its index in the given input.
     *  Sample: "Good" becomes "Gpqg"
     *
    !*/
    char mediumEncrypt(char input[]);   <<< Wrong. All your prototypes should be changed to expect a pointer and return void. In fact, don't put the variable name in there, just the expected data type: void mediumEncrypt(char *);
    Do that for ALL your prototypes.

    Code:
      if(encrypt == 's')
      {
          printf("Original word is: ");
          printf("%s\n", input);
          printf("Encrypted word is: ");
          simpleEncrypt(*input);       <<<<< Wrong. This ends up passing the value in the first element of input, not the address. Pass the unsubscripted array name. Do not dereference it.  
          printf("%s",input);
      }
    More wrong:

    Code:
          int upper;
          if(input[i] >= 65 && input[i] <= 90)
        {
          upper++;
        }
    You never initialize upper, so it contains any value a signed int can hold and you end up incrementing it.

    Also, you're getting the newline in the scanf that gets the encryption level. You'll need to fflush(stdin);
    Last edited by Cynic; 04-29-2012 at 07:10 PM.

  5. #20
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    Thanks for everything!

  6. #21
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Cynic View Post
    You'll need to fflush(stdin);
    You must be new here, as this comes up a lot.
    fflush(stdin); should never be used in any C program. Please read the FAQ for why, and for a suitable replacement.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > char mediumEncrypt(char input[]); <<< Wrong. All your prototypes should be changed to expect a pointer
    Wrong in your wrong.

    As a function parameter, char input[ ] and char *input have exactly the same meaning.

    > In fact, don't put the variable name in there, just the expected data type
    Whilst the name is optional, it does serve to document the API.

    What would you prefer to see in a tool-tip
    char *find( char *needle, char *haystack );
    or
    char *find( char *, char * ); //!! mmm, I wonder which way round these parameters are


    > Also, you're getting the newline in the scanf that gets the encryption level. You'll need to fflush(stdin);
    Actually,
    scanf(" %c", &encrypt); //!! leading space in format, to skip leading space in input.
    would suffice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# for a C++ coder
    By KIBO in forum C# Programming
    Replies: 5
    Last Post: 04-07-2012, 01:10 AM
  2. new coder using Dev C++
    By newcoder3333 in forum C++ Programming
    Replies: 3
    Last Post: 07-29-2006, 05:35 PM
  3. HL2 mod needs coder
    By livewire in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 07-30-2005, 10:29 AM
  4. Replies: 10
    Last Post: 06-17-2005, 10:00 PM
  5. Profile of a coder
    By iain in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 08-20-2001, 06:05 AM

Tags for this Thread