Thread: Array/Function Troubles

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    55

    Array/Function Troubles

    Hello, i recently saw the film The Prestige, and one of the actors keeps a diary, and writes in it using a keyword cipher. I decided to make a program that when i input text it outputs the text in the cipher (switches one letter for another letter).

    I think i have written my function declaration wrong, and when im calling the function i think i've done it wrong, and when i prototype it i think i've done it wrong.

    Also, how do i assign 'x' to the number that the array is on?
    'x' needs to be initialised, but i am not sure how i initialize it to this position?

    Take a look at the code to understand what i mean.

    Thanks in advance to anyone who can help me out

    Code:
    #include <stdio.h>
    
    void fConvert(int, char[]);
    
    int main(void)
    {
    	char cText[100];
    	int x;
    
    	printf("Enter text:");
    	fgets(cText, 100, stdin);
    
    	if(cText[x] != '\0')
    	{
    		fConvert(x, cText);
    	}
    
    	return 0;
    }
    
    void fConvert(int a, char b[100])
    {
    	for(a; b[a]!='\0'; a++)
    	{
    		switch(b[a])
    		{
    			case 'a':
    				printf("t");
    				break;
    			case 'b':
    				printf("e");
    				break;
    			case 'c':
    				printf("s");
    				break;
    			case 'd':
    				printf("l");
    				break;
    			case 'e':
    				printf("a");
    				break;
    			case 'f':
    				printf("b");
    				break;
    			case 'g':
    				printf("c");
    				break;
    			case 'h':
    				printf("d");
    				break;
    			case 'i':
    				printf("f");
    				break;
    			case 'j':
    				printf("g");
    				break;
    			case 'k':
    				printf("h");
    				break;
    			case 'l':
    				printf("i");
    				break;
    			case 'm':
    				printf("j");
    				break;
    			case 'n':
    				printf("k");
    				break;
    			case 'o':
    				printf("m");
    				break;
    			case 'p':
    				printf("n");
    				break;
    			case 'q':
    				printf("o");
    				break;
    			case 'r':
    				printf("p");
    				break;
    			case 's':
    				printf("q");
    				break;
    			case 't':
    				printf("r");
    				break;
    			case 'u':
    				printf("u");
    				break;
    			case 'v':
    				printf("v");
    				break;
    			case 'w':
    				printf("w");
    				break;
    			case 'x':
    				printf("x");
    				break;
    			case 'y':
    				printf("y");
    				break;
    			case 'z':
    				printf("z");
    				break;
    			default:
    				printf("Error, was not a letter");
    				break;
    		}
    	}
    }
    Last edited by Astra; 11-16-2006 at 03:12 PM.

  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
    > if(cText[x] != '\0')
    What's the value of x here?

    Did you perhaps mean to use a for loop to step through the string?

    Also, if you're trying to pass the whole array, then the prototype should be
    void fConvert(int, char[ ]);

    And the call should be
    fConvert(x, cText);

    Since it seems that x (or a in the funtion) is never used, you may as well remove it as a parameter, and just make it local within the function.
    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
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Here's a for loop.
    Code:
    void fConvert(int a, char b[100])
    {
    	for(a = 0; a < '\0'; a++)
    However, the condition will ensure that you're never enter the loop. Remember, you're checking for the null termination of the array, not something on the index.
    Code:
    for(a = 0; b[a] != '\0'; a++)
    Further, why pass in a at all if you're just going to be setting it to zero first thing?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    >if(cText[x] != '\0')

    I made this line to use the fConvert at any point in the array that ISNT the end.

    I made the 'x' so that 'a' in my fConvert function will apply to the 'x', which is the current number that the array is at.

    I still can't see what i'm doing wrong

    (updated code in first post)

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    you don't need x anyway. cos cText is just string which is nothing but one bunch of chars.

    you could think if having a more variable like in your case X if you have number of bunch of chars, like

    Code:
    char cText[10][100];
    which means 10 bunches with each much having 100 chars

    ssharish2005

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by AstraI
    still can't see what i'm doing wrong
    hello.c(12): error 534: (Warning -- Ignoring return value of function 'fgets(char *, int, struct _iobuf *)'

    if(cText[x] != '\0')
    hello.c(14): error 530: (Warning -- Symbol 'x' (line 9) not initialized)

    for(a = 0; a < '\0'; a++)
    hello.c(24): error 681: (Warning -- Loop is not entered)

    hello.c(111): error 952: (Note -- Parameter 'b' (line 22) could be declared const)
    hello.c(111): error 818: (Info -- Pointer parameter 'b' (line 22) could be declared as pointing to const)

    hello.c(112): error 766: (Info -- Header file 'stdlib.h' not used in module 'hello.c')

    For the start the above warning should be fixed...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    do this

    Code:
    if ( cText[0] != '\0')
          then call the fconvert function with just the ctext as a parameter
    ssharish2005

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    @vart, ty for the help.

    I thought fgets was a function in stdlib.h which is why i included that at the top. Is it in stdio.h?

    @ssharish2005, if i put ''cText[0]'' it would only mean the first thing inputted being end of array, but i want it to be for the entire array, can i still do that this way?

    @anybody - if i take out all the x's, in this part i'll be left with:
    Code:
    	if(cText[] != '\0')
    	{
    		fConvert(cText);
    	}
    But, i want: cText[HERE TO SHOW WHAT POSITION ITS AT]

    EDIT: updated code in first post
    Last edited by Astra; 11-16-2006 at 02:43 PM.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    @ssharish2005, if i put ''cText[0]'' it would only mean the first thing inputted being end of array, but i want it to be for the entire array, can i still do that this way?
    thats mean u are checking for whether the user has entered any values. Without any values entered how do u process on it. It is a error checking code

    if i can understand u well

    Code:
    for (i=0;ctext[i] != '\0';i++)
         convert(ctext[i]);
    is this is what u wanted

    ssharish2005

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Astra
    @vart, ty for the help.
    Use English, please

    Quote Originally Posted by Astra
    I thought fgets was a function in stdlib.h which is why i included that at the top. Is it in stdio.h?
    You can comment some h-file and see - what warnings appear.
    Or you can use something like msdn.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    Would that not set i to 0 each time it goes round? I still don't know why i don't need x. Should i use the for loop that ssharish has written, and also the loop in my fConvert?

    ty vart.
    Last edited by Astra; 11-16-2006 at 03:13 PM.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    No you definitely need only one loop for your solution

    For loop inside function use
    Code:
    void fConvert(char[]);
    for declaration and
    Code:
    fConvert(cText);
    for the call

    OR
    in the solution of ssharish2005
    the declaration will be
    Code:
    void convert(char);
    and this function will convert and print only 1 charachter
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    when i take the x's out it has an error saying too few parameters when im calling the function. Do you want me to take out the 'a' from the fConvert too? I am not sure if anyone here knows what i'm trying to do, i was under the impression i could use a function with ghost variables to do the basic conversion (which i DEFINATELY need a loop for, to search the string) then just to call it with putting the variables from my int main into it?

    Dis-regarding what everyone has said, i initialised x to 0 at the top and now it works. :/

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Astra
    i initialised x to 0 at the top and now it works. :/
    1. You can just remove the x and use directly
    Code:
    if(cText[0] != '\0')
    to check if the string is empty
    2. You can remove this check also because the
    Code:
     for(a=0; b[a]!='\0'; a++)
    makes the same check inside the function
    3. You can remove the x from fConvert parameters - becouse you don't need its value and as said before - use local variable

    All this does not checnge the logic of the program. Just makes it easier to read and "clearer"
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    ah okay, my only problem at the moment is that when i remove the x's from this code:

    Code:
    void fConvert(int, char[]);
    
    int main(void)
    {
    	char cText[100];
    	int x = 0;
    
    	printf("Enter text:");
    	fgets(cText, 100, stdin);
    
    	fConvert(x, cText);
    
    	return 0;
    }
    
    void fConvert(int a, char b[100])
    {
    	for(a; b[a]!='\0'; a++)
    	{
    		switch(b[a])
    		{
    It then has a problem because it's expecting an int in here ''fConvert(x, cText); because in my second function i use two variables.

    By local variables, do you mean local to the fConvert function or the main function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clock Troubles
    By _Nate_ in forum C Programming
    Replies: 22
    Last Post: 06-19-2008, 05:15 AM
  2. Replies: 4
    Last Post: 01-15-2006, 05:14 AM
  3. Console-based file i/o troubles
    By Callith in forum C++ Programming
    Replies: 3
    Last Post: 12-25-2004, 09:22 PM
  4. More Program Troubles...
    By face_master in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2002, 05:40 AM
  5. having troubles
    By neandrake in forum C++ Programming
    Replies: 7
    Last Post: 03-07-2002, 09:31 PM