Thread: Pointers (HELP!!)

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    23

    Pointers (HELP!!)

    the following is a program i have written for an assginment. my question is is that how you pass a pointer to a function? or did i do that wrong?
    My instructor told us to use pointer syntax throughout and not array indexes are allowes, does that mean that my i should use something else than the "char message[]" if so what should i use?
    Also, how else can i check if the message is only lower case letters, digits, and whitespace(is there a special character for whitespace ex.'\n')?
    please help me and thank you,
    lucy
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    #define MAX 81
    #define SHIFT 3
    
    int main (void) 
    {
    
    	char message[MAX];
    	char *p = &message;
    
    	void header(void);
    	void getMessage(char*);
    	void isValid(char*);
    	void encrypt(char*);
    
    	header();
    	getMessage(*p);
    	isValid(*p);
    	encrypt(*p);
    
    
    	return 0;
    }
    void header(void)
    {
    	puts("   **************************************");
    	puts("   *      ENCRYPTION APPLICATION        *");
    	puts("   **************************************");
    	printf("\n");
    	puts("You're about to encrypt a message using Caesar sipher:");
    
    	return;
    }
    
    void getMessage(char * p)
    {
    	char * s = p;
    
    	printf("Enter a message to be encryptes -> ");
    	while((*s=getch())!="\n"){
    		if(*s == '\b'){
    			s--;
    			s=(s<p) ? p : (printf("\b \b"),s);
    		}
    		else {
    			printf("*");
    			s++;
    		}
    	}
    	*s='\0';
    	return;
    }
    void isValid(char * p)
    {
    	char * pMessage = p;
    
    	while (*pMessage != '\0')
    	{
    		if(isupper(*pMessage))
    		{
    			puts("Error: No Capital Letters");
    			puts("Please retype your message.");
    		}
    		pMessage++;
    	}
    	return;
    }
    
    void encrypt(char*p)
    {
    	char *pMessage = p;
    	char temp;
    
    	while (*pMessage != '\0')
    	{
    		if(*pMessage<55)
    		{
    			*pMessage+=SHIFT;
    		}
    		else if(*pMessage>=55)
    		{
    			temp = SHIFT + 4;
    			*pMessage-=temp;
    		}
    		else if(*pMessage<120)
    		{
    			*pMessage+=SHIFT;
    		}
    		else if(*pMessage >= 120)
    		{
    			temp = SHIFT +20;
    			*pMessage-=temp;
    		}
    
    		*pMessage++;
    	}
    	printf("The encrypted message: %s",pMessage);
    	return;
    }
    Last edited by lucy; 10-16-2001 at 07:36 PM.

  2. #2
    Unregistered
    Guest
    You can always do something like:

    if ( isalpha( *(s+count) )

    That basicly is the same thing as:

    s[count]

    I believe your instructor doesn't want you doing:
    Code:
    for( count=0; count<strlen(s); count++)
    {
       if( isupper( s[count] ) ...
    }
    Thus, you can do the same thing if you do:

    if( isupper( *(s+count) ) ) ...

    If you want to allocate an "array", without using an array, you can do something like this:

    char *myArray = malloc( sizeof(char) * myArraySize );

    Then you can treat it like an array.

    Quzah.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    is there another way to shift the characters back to the begining of the alphabet(digits) than this? if the shift was 4, 5, or 6 this i know will not work.
    Code:
    	while (*pMessage != '\0')
    	{
    		if(*pMessage<55)
    		{
    			*pMessage+=SHIFT;
    		}
    		else if(*pMessage>=55)
    		{
    			temp = SHIFT + 4;
    			*pMessage-=temp;
    		}
    		else if(*pMessage<120)
    		{
    			*pMessage+=SHIFT;
    		}
    		else if(*pMessage >= 120)
    		{
    			temp = SHIFT +20;
    			*pMessage-=temp;
    		}
    
    		*pMessage++;
    	}

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    in function main()

    >char *p = &message;
    should be char *p=message;

    > getMessage(*p);
    > isValid(*p);
    > encrypt(*p);
    ->
    getMessage(p);
    isValid(p);
    encrypt(p);

    In function getMessage()
    >while((*s=getch())!="\n"){
    while((*s=getch())!=13){

    in function isValid()
    > puts("Please retype your message.");
    > }
    add this line: pMessage++;

  5. #5
    Unregistered
    Guest
    By shifting them to the beginning of the alphabet, do you mean forcing them to lower case? If so, use 'tolower'.

    These functions/macros are in the 'ctype.h' header.

    letter = tolower( letter );
    letter = tolower( 'A' );

    Remember: 'a' == single character, "a" == string consisting of 1 letter and a null terminator. There is a difference.

    Thus:

    if( letter = '\n' ) ...

    Notice that the \ character is an "escape character". This means that it is not treated like a normal character. To actually represent a single character \ ..you actually have to do: '\\'.

    Quzah.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    what i mean by shifting is if the letter was 'a' then it becomes 'd', 'x' is sent to 'a', 'z' is sent to c, '3' is sent to '6', and '8' is sent to '1'.

    when i used this line nothing happens when i hit enter :
    while((*s=getch())!='\n'){

    does that meant i need to use:
    while((*s=getch())!='\\n'){

    the next line works, but isnt 13 a type a character in the ascii table and not newline?
    while((*s=getch())!=13){

    but in my other programs the first line worked just fine....?

    in the isValid() function how can i validate that the message is only consist with digits, lower case letters, and spaces?

    thanks for the advice winbill and Quzah

    lucy

  7. #7
    Unregistered
    Guest
    Well, with 'getch', you're looking for input from the keyboard. That means that this may not work correctly (or at least, not the way you are expecting it to work).

    > what i mean by shifting is if the letter was 'a' then it
    > becomes 'd', 'x' is sent to 'a', 'z' is sent to c, '3' is sent to '6',
    > and '8' is sent to '1'.

    Ah, gotcha.

    > when i used this line nothing happens when i hit enter :
    > while((*s=getch())!='\n'){

    This is what I meant by it won't work correctly. What I would do
    is to read an entire line of text first, then pull it from the buffer.

    Something like this:

    fgets( buf, SIZE_OF_BUFFER, stdin );

    Then, read it a character at a time:

    for( x = 0; x < SIZE_OF_BUFFER && buf[x] != '\n'; x++ )
    {
    ... process character ...
    }

    > does that meant i need to use:
    > while((*s=getch())!='\\n'){

    No. Try this example and you'll see what I mean:
    Code:
    #include <stdio.h>
    int main( void )
    {
       printf("To use a single \\, do this: \'%c%c\'\n", '\\', '\\');
       printf("To use a newline \\n, use this: \'%c%c\'\n", '\\','n');
       printf("To use a single quote, use this: \'%c%c\'\n", '\\','\'');
       printf("There are more. Consult your friendly C book.\n" );
    
       return 0;
    }
    > the next line works, but isnt 13 a type a character in the ascii
    > table and not newline?
    > while((*s=getch())!=13){

    Correct.

    printf("The newline character's decimal value is: %d\n.", '\n' );

    > but in my other programs the first line worked just fine....?
    > in the isValid() function how can i validate that the message is
    > only consist with digits, lower case letters, and spaces?

    Code:
    int valid=1;
    for( x = 0; x < strlen(s) && valid; x++ )
    {
       if ( !isalnum( s[x]  && !isspace( s[x] ) valid = 0;
    }
    This code will stop when the characater in the string is not a alpha-numeric character, and when it isn't a space.

    Quzah.

  8. #8
    Unregistered
    Guest
    Damn forum. It butchered my sample code. Let's try this:

    .... damnit ... (previews failed here)

    Ok, this forum will not let you "escape" single quotation marks, and backslashes. Here's what I mean:

    To display a single \ in a string, you must escape it:

    \ \

    Without the space between them. Thus, to represent the single \ in a character also, you do the same thing, except you use single quotation marks around it.

    To display a single quotation mark, or a double quotation mark, in a string, you must 'escape them'. To 'escape' a character means to preceed the character with a backslash.

    This is not to be confused with the ANSI-ESCAPE sequence character. This is something entirely different, that behaves very similar.

    Quzah.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    isalnum and isspace are functions right?
    this is what i have and i dont think its right or do i have the wrong concept?
    pMessage shouldn't have [] next to it right?

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    #define MAX 81
    #define SHIFT 3
    
    int main (void) 
    {
    
    	char message[MAX];
    	char *p = message;
    	int flag;
    
    	void header(void);
    	void getMessage(char*);
    	int isValid(char*);
    	void messageEncoding(char*);
    
    	header();
    	getMessage(p);
    	flag=isValid(p);
    	if (flag = 0)
    	{
    		getMessage(p);
    	}
    	messageEncoding(p);
    
    
    	return 0;
    }
    
    int isValid(char * p)
    {
    	char * pMessage = p;
    	int valid=1;
    	int x;
    
    
    
    	for( x = 0; x < MAX (*pMessage) && valid; x++ )
    	{
    		if ( !isalnum(*pMessage[x])  && !isspace(*pMessage[x])) 
    			valid = 0;
    	}
    
    /*	while (*pMessage != '\0')
    	{
    		if(isupper(*pMessage))
    		{
    			return 0;
    			break;
    		}
    		pMessage++;
    	}*/
    	return valid;
    }
    
    void messageEncoding(char*p)
    {
    	char *pMessage = p;
    	char temp;
    
    	while (*pMessage != '\0')
    	{
    		if(*pMessage<55)
    		{
    			*pMessage+=SHIFT;
    		}
    		else if(*pMessage>=55)
    		{
    			temp = SHIFT + 4;
    			*pMessage-=temp;
    		}
    		else if(*pMessage<120)
    		{
    			*pMessage+=SHIFT;
    		}
    		else if(*pMessage >= 120)
    		{
    			temp = SHIFT +20;
    			*pMessage-=temp;
    		}
    
    		*pMessage++;
    	}
    	printf("\n\nThe encrypted message: %s\n",&pMessage);
    	return;
    }
    **************************************
    * ENCRYPTION APPLICATION *
    **************************************

    You're about to encrypt a message using Caesar sipher:
    Enter a message to be encryptes -> ***

    The encrypted message: / ↕
    Press any key to continue

    i get "/ ↕" when i try to encrypt "y2k" or anything else including upper case letters from my original code y is that?

  10. #10
    Unregistered
    Guest
    You should test your functions one at a time to ensure that they work properly. Do something like this:

    messageEncode( "hello" );

    See if it comes out right.

    Also, make sure the function that gets the input is working correct.

    Quzah.
    //getting ready to go home from work now

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    23

    Unhappy

    i think everything is fine besides the messageEncoding function, i keep getting garbage when i print out the encrypted string. why is it doing that?
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    #define MAX 81
    #define SHIFT 3
    
    	void header(void);
    	void getMessage(char);
    	int isValid(char);
    	void messageEncoding(char, char);
    int main (void) 
    {
    
    	char message[MAX];
    	char message2[MAX];
    	int flag;
    
    	header();
    	getMessage(message);
    	flag = isValid(message);
    	if (flag = 0)
    	{	
    		puts("Message can not consist upper case letters or symbols.");
    		getMessage(message);
    	}
    	messageEncoding(message, message2);
    
    
    	return 0;
    }
    void header(void)
    {
    	puts("   **************************************");
    	puts("   *      ENCRYPTION APPLICATION        *");
    	puts("   **************************************");
    	printf("\n");
    	puts("You're about to encrypt a message using Caesar cipher:");
    
    	return;
    }
    
    void getMessage(char * p)
    {
    	char * s = p;
    
    	printf("Enter a message to be encrypted-> ");
    	while((*s=getch())!='\r'){
    		if(*s == '\b'){
    			s--;
    			s=(s<p)?p: (printf("\b \b"),s);
    		}
    		else {
    			printf("*");
    			s++;
    		}
    	}
    	*s='\0';
    		printf("\n %s\n",p);
    	
    	
    }
    int isValid(char * p)
    {
    	char * pMessage = p;
    	int valid=1;
    
    	while (*pMessage != '\0')
    	{
    	if ( !isalpha(*pMessage)  && !isspace(*pMessage) && !isdigit(*pMessage) && isupper(*pMessage))
    		valid=0;
    	 *pMessage++;
    	}
    	return valid;
    }
    
    void messageEncoding(char *p, char *p2)
    {
    	char *pMessage = p;
    	char *pMessage2 = p2;
    
    	while (*pMessage != '\0')
    	{
    		if((*pMessage) =='x'|| (*pMessage) =='y'||(*pMessage) =='z')
    			*pMessage2 = (*pMessage) - 26 + SHIFT;
    		
    		if(isspace(*pMessage))
    			pMessage2 = ' ';
    		
    		if(isdigit(*pMessage))
    			*pMessage2 = (*pMessage) - 10 + SHIFT;
    		
    		else 
    			*pMessage2 = (*pMessage) + SHIFT;
    
    		*pMessage++;
    		*pMessage2++;
    	}
    	printf("\n\nThe encrypted message: %s\n",p2);
    	return;
    }#include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    #define MAX 81
    #define SHIFT 3
    
    //	void header(void);
    	void getMessage(char);
    	int isValid(char);
    	void messageEncoding(char, char);
    int main (void) 
    {
    
    	char message[MAX];
    	char message2[MAX];
    //	char *p = message;
    //	char *p2= message2;
    	int flag;
    
    //	header();
    	getMessage(message);
    /*	flag = isValid(message);
    	if (flag = 0)
    	{	
    		puts("Message can not consist upper case letters or symbols.");
    		getMessage(message);
    	}*/
    	messageEncoding(message, message2);
    
    
    	return 0;
    }
    void header(void)
    {
    	puts("   **************************************");
    	puts("   *      ENCRYPTION APPLICATION        *");
    	puts("   **************************************");
    	printf("\n");
    	puts("You're about to encrypt a message using Caesar cipher:");
    
    	return;
    }
    
    void getMessage(char * p)
    {
    	char * s = p;
    
    	printf("Enter a message to be encrypted-> ");
    	while((*s=getch())!='\r'){
    		if(*s == '\b'){
    			s--;
    			s=(s<p)?p: (printf("\b \b"),s);
    		}
    		else {
    			printf("*");
    			s++;
    		}
    	}
    	*s='\0';
    		printf("\n %s\n",p);
    	
    	
    }
    int isValid(char * p)
    {
    	char * pMessage = p;
    	int valid=1;
    
    	while (*pMessage != '\0')
    	{
    	if ( !isalpha(*pMessage)  && !isspace(*pMessage) && !isdigit(*pMessage) && isupper(*pMessage))
    		valid=0;
    	
    	else
    		valid = 1;
    		*pMessage++;
    	}
    	return valid;
    }
    
    void messageEncoding(char *p, char *p2)
    {
    	char *pMess = p;
    	char *pMess2 = p2;
    
    	while (*pMess != '\0')
    	{
    		if((*pMess) =='x'|| (*pMess) =='y'||(*pMess) =='z')
    			*pMess2 = (*pMess) - 26 + SHIFT;
    		
    		if(isspace(*pMess))
    			pMess2 = ' ';
    		
    		if(isdigit(*pMess))
    			*pMess2 = (*pMess) - 10 + SHIFT;
    		
    		else 
    			*pMess2 = (*pMess) + SHIFT;
    
    		*pMess++;
    		*pMess2++;
    	}
    	*pMess2='\0';
    	printf("\nThe encrypted message: %s\n",p2);
    	return;
    }
    Last edited by lucy; 10-16-2001 at 11:46 PM.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    in function messageEncoding

    >*pMessage++;
    >*pMessage2++;
    ->
    pMessage++;
    pMessage2++;

    add *pMessage2=0;
    before display the encrypted message

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    im still getting a bunch of garbage but less that the first time

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    do not press the function key.

    The getch function reads a single character from the console without echoing. getche reads a single character from the console and echoes the character read. Neither function can be used to read CTRL+C. When reading a function key or an arrow key, getch and getche must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM