C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 12th, 2013, 05:52 PM
Miktor Miktor is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 21 Miktor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 40 m 16 sec
Reputation Power: 0
Called object type 'int' is not a function or function pointer

I wrote this program to caesar encrypt a string (alphabetic characters only). (i.e. take all the characters in a sentence and rotate them key times - i.e. whatever number is entered as the key command line parameter)
Code:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

char caesar_encode (char, int);  // prototype of caesar_encode function

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

int main (int argc, string argv[])
{
    int key = 0;
    int length, base, asciiCode;
    string sentence;
    char currentLetter, caesar_encode;

    // if wrong number of command line arguments, shout at user then quit
    if (argc != 2)
    {
        printf("Incorrect number of command line arguments.\n\n");
        return 1;
    }

    // if key parameter is too low, shout at user then quit
    key = atoi(argv[1]);
    // printf("key = %i\n\n", key);
    if (key < 1)
    {
        printf("Key parameter is too low.\n\n");
        return 1;
    }
    
    // since key values modulo 26 are equivalent for the purposes of our program
    // if a key value is larger than 26, reduce it to its modulo 26 equivalent
    // (i.e. a number from 0 to 26)
    if (key > 26)
    {
        // printf("Reducing key larger than 26 to its modulo 26 equivalent.\n");
        // printf("Old key was %i\n.", key);
        key = key % 26;
        // printf("New key is %i.\n\n", key);
        
    }
    
    // prompt user for a string
    // printf("Enter a string to encrypt: ");
    sentence = GetString();
    string newSentence = sentence; // set up newSentence same length as sentence
    length = strlen(sentence);
    
    // loop through string, adjusting each letter according to the key value
    for (int i = 0; i < length; ++i)
    {
        printf("\n");
        // get current letter from sentence
        currentLetter = (char) sentence[i];
        
        // only Caesar encrypt currentLetter is it's actually a letter
        // (as per the pset2 spec, non-alphabetic characters aren't encoded)
        if (isalpha(currentLetter))
        {
        	newSentence[i] = caesar_encode (currentLetter, key);
		}
    }
    printf("%s\n", newSentence);
    // printf("\n\n");
    return 0;
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// caesar encoding function - accepts a char and a key value as parameters
// returns the caesar cipher encoded char
// precondition: the function is only called if it has already been determined
// that the char letter parameter is alphabetic. Therefore we don't need to test
// for this inside the function.

char caesar_encode (char letter, int key)
{
	char encoded_letter;
	int base;
	
	// CODE TO CAESAR CIPHER ENCODE THE LETTER HERE
	// if lowercase letter, base = 'a' - 1;
	// if uppercase letter, base = 'A' - 1;
    // and currentLetter = base + ((currentLetter + key) % 26)
	if (letter >= 'a' && letter <= 'z')
		base = 'a' - 1;
	if (letter >= 'A' && letter <= 'Z')
		base = 'A' - 1;
	printf("base = %i\n", base);
    
    printf("Before encoding, letter was: %c", letter);
    encoded_letter = (char) (base + (int)((int) letter + key));	// encode letter
	printf("After encoding, letter is: %c", encoded_letter);
	
	return encoded_letter;
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


I'm getting the following error, however:

caesar.c:63:48: error: called object type 'int' is not a function or function pointer
newSentence[i] = (char) caesar_encode ((char) currentLetter, key);
~~~~~~~~~~~~~ ^

Can anybody help? Thanks! :/

Reply With Quote
  #2  
Old February 12th, 2013, 06:13 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 49 m 18 sec
Reputation Power: 1974
newSentence[i] = (char) caesar_encode ((char) currentLetter, key);

You are using caesar_encode as a function which takes two arguments. You even have a function prototype and a function implementation by that name. However:
Code:
char caesar_encode (char, int);  // prototype of caesar_encode function

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

int main (int argc, string argv[])
{
    int key = 0;
    int length, base, asciiCode;
    string sentence;
    char currentLetter, caesar_encode;

Within main, from where you call that function, you reused the name to be a char variable, which is an integer type. That reuse of the name hides the function. When an identifier in the local scope has the same name as an identifier outside that scope, it's the one within the local scope that is used. Scope and name hiding are important concepts in C, C++, and a number of other languages.

Change the name of either the function or of the variable. Changing the function to CaesarEncode would be my choice.

Last edited by dwise1_aol : February 12th, 2013 at 06:16 PM.

Reply With Quote
  #3  
Old February 13th, 2013, 02:54 AM
Miktor Miktor is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 21 Miktor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 40 m 16 sec
Reputation Power: 0
Thanks dwise!

This was a problem. Another problem was my not deducting the base from (char value + key) part of the calculation to encode the char. Spent a week scratching my head over that problem set! Just glad I finally figured it out!

Once again, thanks for your help.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Called object type 'int' is not a function or function pointer

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap