Like Tree1Likes
  • 1 Post By dwks

Functions and Character Arrays HELP!

This is a discussion on Functions and Character Arrays HELP! within the C Programming forums, part of the General Programming Boards category; Code: #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <string.h> //char Rule1(int); int Rule2(char); void Display(); char input_string[100]; int main() { printf("Please Enter ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    5

    Functions and Character Arrays HELP!

    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #include <string.h>
    
    
    //char Rule1(int);
    int Rule2(char);
    void Display();
    char input_string[100];
    
    
    int main()
    { 
    	
    	printf("Please Enter String: ");
    	
    	scanf("%s", &input_string);
    	
    	Rule2(input_string);
    
    
    	char input_string[100];
    
    
    	getchar();
    }
    
    
    
    
    int Rule2(char* input_string) // Rule 2 function
    {
    	
    	char AY[2]={'A','Y'};
    	
    		if(input_string[0] == 'A' || input_string[0] == 'a' || input_string[0] == 'E' || input_string[0] == 'e' || input_string[0] == 'I' || input_string[0] == 'i' || input_string[0] == 'O' || input_string[0] == 'o' || input_string[0] == 'U' || input_string[0] == 'u') 
    			{
    				strcat(input_string, AY);
    				Display(); // Displays new word
    			}
    	}
    
    
    void Display()
    {
    	printf("The word you enter is %s\n", input_string);
    	//PigLatin=fopen("piglatin.txt", "w");
    	//fprintf(PigLatin,"%s");
    		//fprintf(PigLatin, "%s", input_string);
    
    
    }
    i get an C2143 error at the Rule2 function call in main and its driving me crazy! Please help! thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    That's because the prototype for Rule2 is Rule2(char), while its definition is Rule2(char*). Notice the extra '*'. These things have to match.

    A few other issues: You declare input_string as a global variable, and then you declare another variable with the same name inside main() which hides the global one. You'll definitely end up using one variable instead of the other by accident and wondering why there's no data in the array.

    You should be declaring input_string inside main() before you use it (i.e. before the scanf call). Then passing it to Rule2 is fine (although you need to change the prototype to be char* instead of char as I mentioned). There's a handy function called tolower() [and also toupper()] in <ctype.h> that will turn 'A' into 'a', which you may want to use.

    Also: your string AY is not null-terminated, so the strcat() will do a buffer overrun and may crash your program. Definitely declare it as
    Code:
    char AY[3] = {'A','Y','\0'};
    char AY[3] = "AY";  // implicit \0
    char AY[] = "AY";  // implicit size plus implicit \0
    All those are equivalent, the last obviously being the simplest. However, you don't even need a variable if you're just strcat'ing, you can say
    Code:
    strcat(input_string, "AY");
    Hmm... also, main should return 0. You should turn on compiler warnings, I'm sure they'll help you spot some mistakes before they get too buried. For g++/gcc just add "-Wall -Wextra" to your command line.

    Sorry if that was a bit harsh/rushed... wasn't intended to be. You've actually posted some code which is more than some people do. Please feel free to ask if you have questions.

    [edit] P.S. If you're feeling adventurous, switch statements or strchr() [a function in string.h] can be used to check if one character belongs to a set of characters you're interested in (e.g. vowels). Combine with tolower() for best effect. [/edit]
    Last edited by dwks; 07-17-2012 at 01:44 PM.
    AndiPersti likes this.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    This too:
    Code:
    printf("Please Enter String: ");
         
    scanf("%s", &input_string);
    In this context, the & is not needed. The array name by itself is sufficient and is treated as a char pointer so you don't need the address-of operator to turn it into one.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    5
    Thanks for the help guys! This bit of code was copy and pasted from my project which was open on another window of visual studio so things like the return 0 i miss :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. character arrays
    By enkwiringmindz in forum C Programming
    Replies: 8
    Last Post: 02-10-2010, 12:28 AM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. Character Arrays
    By JHaney in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2005, 12:53 PM
  4. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 12:02 PM
  5. help with character arrays
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-28-2002, 05:13 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21