C++ program help

This is a discussion on C++ program help within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I was working on a program where I had to convert an old C program into a C++ ...

  1. #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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  3. #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

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    You didn't check that your fopen worked (fp would be NULL if it failed); that could be it.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  6. #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?

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    You want to make sure it's != to 0.

  8. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


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