C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-03-2008, 02:15 AM   #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
racerday182 is offline   Reply With Quote
Old 12-03-2008, 02:21 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,319
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*.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 12-03-2008, 11:10 AM   #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
racerday182 is offline   Reply With Quote
Old 12-03-2008, 11:16 AM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,319
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().
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 12-03-2008, 11:21 AM   #5
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,276
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);  
}
C_ntua is online now   Reply With Quote
Old 12-03-2008, 06:20 PM   #6
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,319
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?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Reply

Tags
c++, convert, redesign, translate

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:12 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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