Thread: C to C++ conversion

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    11

    Red face C to C++ conversion

    Hey everybody,
    I was having trouble completing my last assignment which required me to redesign a C program into a C++ program using classes and objects.
    So far my Cpp code looks like this:
    insert
    Code:
    class Ciphertext{
    public:
    	void output_ciphertext_screen(struct key_element key[26], FILE * fp);
    };
    
    void Ciphertext::output_ciphertext_screen(struct key_element key[26], FILE * fp)
    {
    	int i;
    	fprintf(fp,"\n");
    	fprintf(fp,"Encoded : ");
    	for(i=0;i<26;i++)
    		fprintf(fp,"%c ",key[i].enc);
    	fprintf(fp,"\n");
    	fprintf(fp,"Decoded : ");
    	for(i=0;i<26;i++)
    		if(key[i].man_flag == 0)
    			fprintf(fp,"%c ",key[i].dec);
    		else
    			if(fp==stdout)
    				printf("%c ",toupper(key[i].dec));
    			else
    				fprintf(fp,"%c ",key[i].dec);
    	fprintf(fp,"\n\n");
    
    }
    
    int main()
    {
    	
    		output_ciphertext_screen print(key, stdout);
    }
    This code is supposed to take a ciphertext line by line from a file and output it onto the screen using C++. Basically, all I did was take the function I had earlier that is supposed to perform this operation and copied it into the object above. But, after compiling, I am getting errors saying:
    error C2065: 'output_ciphertext_screen' : undeclared identifier.
    I am new to C++ and am trying to get a better grasp of it so if you could point me in the right direction by showing me where my code is wrong or just giving me some tips, I should be able to figure out the rest of the program.
    Thanks in advance,
    Andy

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to create a Ciphertext object and call output_ciphertext_screen using it. Of course the key variable also should be defined.
    Code:
    int main()
    {
        Ciphertext cipher_text;
        cipher_text.output_ciphertext_screen(key, stdout);
    }
    Incidentally, in C++ we often use the C++ I/O stream library instead of C I/O, so we would use std::cout instead of stdout and std::ostream (or std::istream) instead of FILE*.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    11

    Smile Next Step

    Arite, tried that and it gets rid of the errors, but what do I need to do to actually get the program to print out the ciphertext, do i need to define some private members within my class?
    Oh and as for using C++ I/O we are not required to do that, but if that makes the program easier I might go back and change that.

    Thanks again

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by racerday182
    what do I need to do to actually get the program to print out the ciphertext, do i need to define some private members within my class?
    That would be implementation detail. The output_ciphertext_screen() member function is sufficient.

    Quote Originally Posted by racerday182
    Oh and as for using C++ I/O we are not required to do that, but if that makes the program easier I might go back and change that.
    One way would be to overload operator<< for std::ostream such that you can write:
    Code:
    std::cout << cipher_text;
    and the ciphertext would be printed to std::cout (i.e., the C++ I/O equivalent of stdout).
    However, in this case it is mainly just syntactic sugar over output_ciphertext_screen().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, I guess you would define a private member called key inside Ciphertext. But you don't NEED to do that. You could just do:
    Code:
    int main()
    {
       Ciphertext ct;
       key_element myKey[SIZE];
       //fill myKey
       ct.output_ciphertext_screen print(myKey, stdout);  
    }
    But it makes more sense to do:
    Code:
    int main()
    {
       Ciphertext ct;
       ct.Insert(...); //will get a key and store it insided a private key_element of Ciphertext
       output_ciphertext_screen print(myKey, stdout);  
    }
    Were insert would be a faction you have to make that either just takes a key_element and stores it either prompts for it and/or read it from a file

    The first code is bad. It would be OK if you declared output_ciphertext_screen static (and/or Ciphertext itself?) and do:
    Code:
    int main()
    {
       key_element myKey[SIZE];
       //fill myKey
       Ciphertext::output_ciphertext_screen print(myKey, stdout);  
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by C_ntua
    Well, I guess you would define a private member called key inside Ciphertext. But you don't NEED to do that. You could just do:
    I think that the important question to ask is: what does the Ciphertext class model?

    Admittedly, I did not look very closely into the given code, but now I see that the Ciphertext class seems to be doing more than it should. The output_ciphertext_screen() is misleading because it does not just output ciphertext, but outputs plaintext as well (and not necessarily to the "screen").

    racerday182, think of what is the purpose of the Ciphertext class. Think of the classes that would work with it. Do you have a hierarchy of cryptographic algorithm classes, for example? If you do things the "C++ way" as overload operator<< as I suggested, then that should really just print the ciphertext, not the plaintext. Would it print the ciphertext by applying an encryption mode? If so, what about an initialisation vector? Should a key even come into the picture? Perhaps the key also comes into play for the function that generates the ciphertext, hence the ciphertext itself has no notion of a key.

    Keep in mind that a class should model one thing and model it well, and a function should do one thing and do it well.

    Quote Originally Posted by C_ntua
    The first code is bad. It would be OK if you declared output_ciphertext_screen static (and/or Ciphertext itself?) and do:
    That is probably even worse: why should that member function be static?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM

Tags for this Thread