![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 11
| 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);
}
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 | |
| | #2 |
| C++ Witch 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);
}
__________________ 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 | |
| | #3 |
| Registered User Join Date: Nov 2008
Posts: 11
| 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 | |
| | #4 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,319
| Quote:
Quote:
Code: std::cout << cipher_text; 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 | |
| | #5 |
| Registered User 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);
}
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);
}
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 | |
| | #6 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,319
| Quote:
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:
__________________ 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 | |
![]() |
| Tags |
| c++, convert, redesign, translate |
| Thread Tools | |
| Display Modes | |
|
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 |