Thread: program using char and string is working weirdly

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    program using char and string is working weirdly

    Code:
    #include <stdio.h>
    #include <string.h>
    int main (int argc, char *argv[]) {    
    
        int i=0;
        int input=0;
        char str1[i];
        char str2[i];
        int n;
    
        printf("Please enter shift [0--25]: %d %c \n", input, str1[i]); //read a number between 0 and 25 and then read a list of characters
        n=scanf("%c", &str1[i]);
    
        i=0;
        while (str1[i] != '\0') {
            str2[i] = str1[i];
            i=i+input; //This will shift the string of characters by the read number's value
        }
        str2[i] = '\0';
        printf("%c", str2[i]);
    
        return n;
    }


    The above is the program I wrote. The output should be the following for example:



    Please enter shift [0--25]: 5
    abcd (what the user types in)
    fghi (what comes out, a + 5 = f, b + 5 = g, etc)
    ctrl d
    Thank you for any help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int i=0;
    > char str1[i];
    > char str2[i];

    How much space is allocated to your arrays - none.

    > printf("Please enter shift [0--25]: %d %c \n", input, str1[i]);
    What was str1 initialised to before printing it - nothing.

    You probably want something like str1[100] and %s for reading in a string.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    Thank you for that. I have made a few adjustments, however, it still isn't working as planned. Because the following error message won't let me compile it:
    invalid operands to binary & (have 'int *' and 'int') //reference to line 12

    Code:
    #include <stdio.h>
    #include <string.h>
    int main (int argc, char *argv[]) {    
    
        int i=100;
        int input=0;
        char str1[i];
        char str2[i];
        int n;
    
        printf("Please enter shift [0--25]: %d %c \n", input, str1[i]);
        n=scanf("%d %c", &input &str1[i]);
    
        i=0;
        while (str1[i] != '\0') {
            str2[i] = str1[i];
            i=i+input;
        }
        str2[i] = '\0';
        printf("%c", str2[i]);
    
        return n;
    }
    Last edited by kase20; 04-21-2012 at 11:06 PM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Why do you insist on putting [i] in every place after str1?

    In a declaration the number in the square brackets specifies the size of the array,
    whereas elsewhere it means that you want to access the item with the number specified.


    Given an array of size 100 only has valid indexes 0 to 99, you're going out of bounds. Subsequent code goes way out of bounds in fact.
    You need to understand and learn the part in bold.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-19-2011, 02:00 PM
  2. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  3. C char string help in linked list and queues program
    By kdj1881 in forum C Programming
    Replies: 3
    Last Post: 04-13-2011, 05:32 PM
  4. Replies: 2
    Last Post: 09-12-2010, 09:15 AM
  5. Math statement parsed weirdly
    By jverkoey in forum Tech Board
    Replies: 1
    Last Post: 06-05-2005, 05:32 AM

Tags for this Thread