![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 11
| I was working on a program where I had to convert an old C program into a C++ program. I am supposed to create a ciphertext class and use it to output a ciphertext that I get from a file. So far, I have written: Code: class Ciphertext{
private:
char file_name[20];
char *ciphertext[10];
public:
Ciphertext(char file_input[20])
{
FILE *fp;
char input[MAX_LENGTH+1];
int m=0;
strcpy(file_name,file_input);
fp=fopen(file_name, "r");
while((fgets(input,80,fp))!=NULL)
{
ciphertext[m]=malloc(strlen(input)+1);
strcpy(ciphertext[m],input); /**** ERROR HERE*****/
m++;
}
~Ciphertext(){}
int main(void)
{
printf("Please enter file name where the encoded text is found:");
scanf("%s",file_name);
while(getchar()!='\n');
Ciphertext my_ciphertext(file_name);
}
Thanks, Andy |
| racerday182 is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| In C++, calls to malloc are required to be casted to the appropriate type. One idea presumably is to encourage use of the C++ memory acquiring thing, "new", as opposed to the C memory acquiring thing. So maybe Code: ciphertext[m] = new char[strlen(input)+1]; |
| tabstop is offline | |
| | #3 | |
| Registered User Join Date: Nov 2008
Posts: 11
| Quote:
Yay! now the program compiles but it only runs for the first instance of the loop. After the program executes through the while loop the first time it gives me a 'System.NullReferenceException' error. How do I make it work for all instances of the loop? Thanks, Andy | |
| racerday182 is offline | |
| | #4 |
| and the Hat of Ass Join Date: Dec 2007
Posts: 731
| You didn't check that your fopen worked (fp would be NULL if it failed); that could be it. |
| rags_to_riches is offline | |
| | #5 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| NullReferenceException smells a little like .net, somehow. Anyway, the only way I can see that happening is if new fails. Check to see whether ciphertext[m] == 0 before trying to strcpy into it. |
| tabstop is offline | |
| | #6 | |
| Registered User Join Date: Nov 2008
Posts: 11
| Quote:
Tabstop, I have now changed my code to: Code: while((fgets(input,80,fp))!=NULL)
{
ciphertext[m]=new char(strlen(input)+1);
if(ciphertext[m]==0)
{
strcpy(ciphertext[m],input);
m++;
}
}
| |
| racerday182 is offline | |
| | #7 |
| and the Hat of Ass Join Date: Dec 2007
Posts: 731
| You want to make sure it's != to 0. |
| rags_to_riches is offline | |
| | #8 |
| Registered User Join Date: Nov 2008
Posts: 11
| LOL oh thanks good idea. Okay, well that gets rid of the System.NullReferenceException but it's still only reading in the first line of the ciphertext because when it loops through, every other line has ciphertext[m]=0 so it skips over the if statement. I'm not quite sure but I feel like the loop is causing problems regarding allocating space for the ciphertext array of pointers. Also, would any of you happen to have a screen name or gmail account so I could just ask you a few questions quickly and get out of your hair instead of posting on here repeatedly. Please let me know. Thanks, Andy |
| racerday182 is offline | |
![]() |
| Tags |
| c++, convert, error, translate |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Issue with program that's calling a function and has a loop | tigerfansince84 | C++ Programming | 9 | 11-12-2008 01:38 PM |
| Need help with a program, theres something in it for you | engstudent363 | C Programming | 1 | 02-29-2008 01:41 PM |
| This is a simple program.. Help me please I think it has an error.. | lesrhac03 | C Programming | 4 | 02-21-2008 10:39 AM |
| My program, anyhelp | @licomb | C Programming | 14 | 08-14-2001 10:04 PM |