C Board  

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

Reply
 
LinkBack Thread Tools Display Modes
Old 12-06-2008, 07:25 PM   #1
Registered User
 
Join Date: Nov 2008
Posts: 11
Unhappy C++ program help

Hey guys,
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);
}
where the member function ciphertext is my constructor function. When I try to compile my code, I am getting an error: "c2440: cannot conver from void* to char*" and it says this error occurs at the line above marked error here. This is very confusing to me because the code above worked perfectly when I was to write the program in regular C. Please help me understand where I am going wrong.
Thanks,
Andy
racerday182 is offline   Reply With Quote
Old 12-06-2008, 07:30 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
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];
This is assuming, of course, that converting C "strings" to C++ std::strings is also not part of the deal.
tabstop is offline   Reply With Quote
Old 12-06-2008, 07:57 PM   #3
Registered User
 
Join Date: Nov 2008
Posts: 11
Post

Quote:
Originally Posted by tabstop View Post
This is assuming, of course, that converting C "strings" to C++ std::strings is also not part of the deal.
Yeah we don't have to do that

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   Reply With Quote
Old 12-06-2008, 10:55 PM   #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   Reply With Quote
Old 12-06-2008, 11:13 PM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 12-07-2008, 02:38 PM   #6
Registered User
 
Join Date: Nov 2008
Posts: 11
Exclamation

Quote:
Originally Posted by rags_to_riches View Post
You didn't check that your fopen worked (fp would be NULL if it failed); that could be it.
I am doing that in my code, I just didn't show it here.

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++;
			}
		}
but now the if statement is never true so nothing gets stored into ciphertext[m]. For some reason my ciphertext[m] never equals 0 to start off with, why, I do not know?
racerday182 is offline   Reply With Quote
Old 12-07-2008, 03:25 PM   #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   Reply With Quote
Old 12-07-2008, 03:41 PM   #8
Registered User
 
Join Date: Nov 2008
Posts: 11
Unhappy

Quote:
Originally Posted by rags_to_riches View Post
You want to make sure it's != to 0.
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   Reply With Quote
Reply

Tags
c++, convert, error, translate

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:47 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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