Thread: Code review needed

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    3

    Question Code review needed

    Hello there. This is a small program that reads a line from the screen, takes the ASCII values from the entered text, and edits it. Next, the ASCII values are reintepreted(am i misspelling?) as text, and displayed. However, my final output has (allways?) at least 7 characters, even if you enter only one. I'm allso trying to accomplish, that the part of the string which isn't filled, will be printed empty. In spaces.

    compiled with -std=c99.

    Code:
    #include <stdio.h>
    
    int main(){
        int maxsize;
        printf("How long is your message?\n");
        scanf("%d",&maxsize);
        int iinput[maxsize], ioutput[maxsize], key, c;
        char cinput[maxsize], coutput[maxsize];
        
        printf("What is the message you want to (de)code?\n");
        scanf("%s",cinput);
        for(int i=0;i<maxsize;i++){
            iinput[i]=(int)cinput[i];
        }
        printf("Thank you. ");
        printf("What is your key?\n");
        scanf("%d",&key);
        lchoice:
        printf("1 for coding, 2 for decoding.\n");
        scanf("%d",&c);
        switch (c)
        {
            case 1:
                for(int j=0;j<maxsize;j++){
                    ioutput[j]=iinput[j]+key;
                }
                for(int k=0;k<maxsize;k++){
                    coutput[k]=(char)ioutput[k];
                }
                break;
            case 2:
                for(int j=0;j<maxsize;j++){
                    ioutput[j]=iinput[j]-key;
                }
                for(int k=0;k<maxsize;k++){
                    coutput[k]=(char)ioutput[k];
                }
                break;
            default:
                printf("You did not enter 1 or 2.\n");
                goto lchoice;
        }
        printf("This is the coded message:\n");
        printf("%s\n",coutput);
        system("pause");
        return(0);
    }
    I would really appreciate some help. Thanks in advance.
    Last edited by BramvandenH; 05-10-2013 at 05:34 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your loops are running from zero to maxsize. If maxsize exceeds the number of characters input, you need to output less than maxsize characters.

    ASCII values are text. The character 'A' ALSO has a numeric (integral) value of 65 (at least in the ASCII character set - the value is different in other character sets). Your "reinterpreting" suggests you don't understand that principle.

    Plenty of people will suggest you need to wash your mouth out with soap and water for using a goto as well.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    3
    Thank you for the response.
    I'm wondering how to find out HOW to output less than maxsize characters. Allso, i don't know how to avoid the goto. I understand your code become a mess with it.
    (I understand ASCII-table values, english isn't my native language... expressing myself about this subject isn't allways simple...)

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    When you use the %s format to input a string, the data is stored to an array of char with a terminating character that has value zero. So, given the code "scanf("%s",cinput)" if the user enters "AB" followed by the enter key, the array cinput receives the characters 'A', 'B', and '\0' (a character with value zero). Two characters entered, three into the array. You can detect that zero to find the end of the string. Rather than do it manually, use functions from <string.h> (eg strlen()) which work with strings like that.

    That is a convention in C. A "string" is an array of char with a zero marker appended.

    An aside: it is not a good idea to use an unadorned %s format in scanf(). Let's say cinput is an array of five characters, and the user enters "ABCDEFG". That will result in eight characters (the 7 letters plus terminating zero) being written to an array that can only hold five characters. You can't stop a user misbehaving, but can cope with that by using the %4s format, which causes scanf() to stop reading the string if the user has entered too much.

    To avoid the goto, loop up thing like "loops". There are various types of looping constructs in C, such as the while loop. For example;
    Code:
    int continuing = 1;
    while (continuing)
    {
         stuff_you_want_to_do_more_than_once();
         if (want_to_exit()) continuing = 0;   /*  cause the looping to stop */
    }
    There are other ways of looping, each suited to some circumstances better than others.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    3
    Thanks again. I was wondering: if you declare a string like this:
    Code:
    char string[5]
    , will the NULL character be in string[4], or string[5]? or in string[5] and string[6]? I noticed that on my 64bit PC the output is like expected, not those 2 characters that make up the NULL.
    I do know about the existence of loops( as seen in my code ), however, i don't know how to apply one in this specific situation. Since i want to execute all those lines only once.
    Last, is there a way to do something like this:
    Code:
    scanf ("%[maxsize]s",cinput);
    , so that the number of items which are read is a variable?
    Thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BramvandenH
    I was wondering: if you declare a string like this:
    Code:
    char string[5]
    will the NULL character be in string[4], or string[5]? or in string[5] and string[6]?
    If you declare an array to have 5 elements, then the valid indices for the array are from 0 to 4, inclusive. Therefore, string[5] is out of bounds, as is string[6]. The null character can thus be in string[0] (i.e., the string is empty), or string[1], or string[2], or string[3], or string[4], as the case may be.
    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

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by BramvandenH View Post
    Last, is there a way to do something like this:
    Code:
    scanf ("%[maxsize]s",cinput);
    , so that the number of items which are read is a variable?
    A format string is just a string as any other one in your program. Thus you can create it during run time as you like it using sprintf():
    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main(void)
    {
        char format_str[20];  // adapt the size of the format string as needed
        size_t str_size;
        
        printf("Enter the size of input string: ");
        scanf("%zu", &str_size);
    
        char input_str[str_size + 1];
        /* To create the format string "%xs" where x is the number of allowed
         * characters (str_size, e.g. "%5s") you need the following parts:
         * %% - will escape the %
         * %zu - will put the given size at that point (%zu is the format specifier
         *       for type size_t)
         * s - for the string format specifier 
         */
        sprintf(format_str, "%%%zus", str_size);
        printf("Format string is <%s>\n", format_str);
        
        printf("Enter string with at most %d characters: ", str_size);
        scanf(format_str, input_str);
        printf("Your input was: <%s> (%zu characters accepted)\n", input_str, strlen(input_str));
    
        return 0;
    }
    The above code works for C99 and higher.

    I would also recommend to study the man pages for the printf()-family and the scanf()-family.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get a code review
    By Richardcavell in forum C Programming
    Replies: 42
    Last Post: 03-19-2011, 10:36 PM
  2. C Code Review
    By trillianjedi in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 06-18-2008, 12:29 PM
  3. review this code
    By KIBO in forum C Programming
    Replies: 12
    Last Post: 08-14-2007, 02:28 PM
  4. Code Review
    By Thantos in forum C Programming
    Replies: 8
    Last Post: 03-06-2004, 06:20 PM
  5. Code review please
    By Brighteyes in forum C Programming
    Replies: 9
    Last Post: 03-29-2003, 06:28 PM

Tags for this Thread