Thread: Letting the user determine size of array?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    16

    Letter user determine the size of an array?

    Hello all,

    I am working on a program called Caesar Cipher which encrypts messages by increasing each character by a value determined by the user (key). It will also roll over if the key rolls over the end of the alphabet.

    example) S would translate to U with a key of 2. Z would translate to B with a key of 2.

    I have the program almost complete, but I am having one problem. I cannot determine how to let the user determine the size of the array. I assume I need to do this in order to adjust the size of the array according to the message. One of the examples messages to test the project is:

    Go ahead, make my day.

    If I change the array size to 21 (rather than 80) it will encrypt properly and I will get the encrypted message as:

    Jr dkhdg, pdnh pb gdb


    The only limit we have is that messages will not exceed 80 characters (I assume I must initialize the size of the array to 80 because of this, correct?). I am having problems encrypting the text properly if the message is < 80. I have to hit enter or input spaces so the message reaches the desired length of 80 before it will then prompt me for the key. Then it will encrypt the message (but of course with all the spaces). How can I determine the size of the array to be the total characters entered by the user even though the initial size is 80? Is there a different approach to this that I am missing?


    Here is my code. I apologize as I haven't formatted it yet so I hope its not too difficult to read.

    Code:
    #include <stdio.h>
    
    
    
    
    int main()
    {
    
    
        char n[80];
        int shift;
        int i;
        int k;
        int aSize = sizeof(n)/sizeof(n[0]);
    
    
    
    
        printf("Please enter message:");
    
    
        for (i = 0; i < aSize; i++)
        {
        scanf ("%c", &n[i]);
        }
    
    
    
    
        printf("Please enter shift amount:");
        scanf("%d",&k);
    
    
        printf("Encrypted message is:");
        for (i=0; i < aSize; i++)
        {
            if ( ( ( n[i] >= 'A' ) && ( n[i] <= 'Z' ) ) || ( (n[i] >= 'a' ) && ( n[i] <= 'z' ) ) )
            {
            n[i]+=k;
    
    
                if ((n[i] >= 'Z') && (n[i] <= 'a'))
                {
                n[i]=((n[i]-'A')) % 26 + 'A';
                printf("%c",n[i]);
                }
                else if (n[i] >= 'z')
                {
                n[i]=((n[i]-'a')) % 26 + 'a';
                printf("%c",n[i]);
                }
                else
                {
                printf("%c",n[i]);
                }
            }
            else
            {
                printf("%c",n[i]);
            }
        }
    
    
        return 0;
        }
    Can anyone give me a hint towards getting this to work right for messages less than 80?
    Last edited by Frankie15; 10-07-2011 at 07:28 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to calculate the length after they input a string, not before.


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

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    What would I put in place "aSize" in the first for loop? char n must have a size of 80 since the character must be less than 80 characters, correct?

    The only way I can get this to work is to know the length of the message before I type it in. That example phrase is 21 characters. If I put i < 21 then it will work properly. Although, of course it will not work if the message is greater or less than 21.

    When I move the code like so:
    Code:
    printf("Please enter message:"); for (i = 0; i < aSize; i++) { scanf ("%c", &n[i]); } aSize = sizeof(n)/sizeof(n[0]);
    printf("Size is %d",aSize);
    It still says that its 80. How do I get the first for loop to stop even when the length is less than 80? I tried using a

    Code:
    while (n[i] != '\n')
    but its not working or I must be doing it wrong.


    Just a little confused on this part.

    Thanks for the help
    Last edited by Frankie15; 10-07-2011 at 07:43 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look up strlen() in your C Library Documentation...

    Make your buffer bigger... maybe 1024 ... null terminate your string correctly, use strlen() to determine how much the user typed. There's no reason to encrypt the entire buffer...
    Last edited by CommonTater; 10-07-2011 at 08:13 PM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    Quote Originally Posted by Frankie15 View Post
    I cannot determine how to let the user determine the size of the array.

    The only limit we have is that messages will not exceed 80 characters (I assume I must initialize the size of the array to 80 because of this, correct?).
    Can anyone give me a hint towards getting this to work right for messages less than 80?
    You can set the size two different ways, ask the user for set the size (i.e "enter array size:") the issue I see in having the user input that is it may not be "user friendly" and you'll have to think, "does the user know what an array is?" or you can have it set based on the number of characters the user inputs (i.e character counting). You can take the value you get from the question or the number of characters inputed and use it for setting the size of the array, and even comparing it to make sure it is not over 80.

    Code:
    //user input
    //calculate the number of characters or take the value the user gives and set to something like numberCharacters
    
    Array[numberCharacters+1] //+1 iff '\0' doesn't count as a character
    
    if (numberCharacters >= 80) fprintf(stderr, "BAD YOU MESSED UP!\n");
    else //program works magic

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    I am still confused on how to do this. We haven't learned strlen yet so I don't believe we can use it.

    The program has to run exactly like I have listed (Enter message...then enter shift...then print the encrypted message). I cannot add anything (like asking the user to enter the array size which would make this program much much simpler).

    I just don't understand how I can make this program work with strings of any length < 80. When I use the for loop to determine the max as 80 it will not stop the loop (obviously) for anything less than 80 characters. Is it possible to get the loop to stop before 80 characters have been inputted (with a new line character?). I have tried messing around with getting it to stop with a /n character, but I haven't had any luck.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is what I suggest:
    • Read in input as a string,.e.g., by using fgets.
    • Use an array of 81 characters. The +1 is to account for the null character.
    • To loop over the characters of the string, start with the first character and stop when you reach the null character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Frankie15 View Post
    We haven't learned strlen yet so I don't believe we can use it.
    Any teacher worth a pile of horsepucky in a paddoc would reward your initiative for looking ahead and finding good solutions.

    Only a fool would punish initiative in a student.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Any teacher worth a pile of horsepucky in a paddoc would reward your initiative for looking ahead and finding good solutions.

    Only a fool would punish initiative in a student.
    While I agree, I also see the point of making them learn how to find the end of the string without strlen. If they don't understand looping through a string to find a specific value, future array usage is going to be pretty bleak.


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

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    @Quzah ... point taken.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 10-31-2007, 08:43 AM
  2. letting user input number of elements in array
    By iamthejake2000 in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2005, 12:35 PM
  3. Replies: 42
    Last Post: 12-19-2004, 08:59 AM
  4. Determine array size
    By eth0 in forum C++ Programming
    Replies: 16
    Last Post: 01-08-2004, 10:26 AM
  5. Determine the size of a character array...
    By Nutshell in forum C Programming
    Replies: 1
    Last Post: 01-10-2002, 10:22 AM