Tuesday, July 14, 2009

C++ Programmers!! help!!?

there's an error in this code... can you help me repair it?!








#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;conio.h%26gt;


#include %26lt;string.h%26gt;





void initval();





int chars[255];





int main()


{


FILE *file_r;


int ferr = 0, i;


char filename[50];


while (ferr == 0)


{


system("cls");


printf("Enter file name: ");


gets(filename);


file_r = fopen(filename, "r");


if (file_r == 0)


{


printf("Error! File does not exist!");


getch();


}


else


{


ferr = 1;


}


}


char *r;


char c, ctr = 0;





while ((c = getc(file_r)) != EOF)


{


ctr++;


}





fclose(file_r);


file_r = fopen(filename, "r");





char string[ctr];





i = -1;


while ((c = getc(file_r)) != EOF)


{


i++;


string[i] = c;


}





printf("\n%s", string);





printf("\n\n%s\n\n", strrev(string));





//tig-ihap





for (i = 0; i %26lt; strlen(string); i++)


{


chars[string[i]]++;


}





for (i = 0; i %26lt; 256; i++)


{


if (chars[i] != 0)


{


printf("%c = %d\n", i, chars[i]);


}


}





getch();





fclose(file_r);


return 0;


}





void initval()


{


int i;


for (i = 0; i %26lt; 256; i++)


{


chars[i] = 0;


}


}

C++ Programmers!! help!!?
do this:





char [] string = new char [ctr];








and don't forget





delete [] string





at the end to free up the memory
Reply:from the code you provided i copy and paste it on my system and i recieved no error messages and the program ran fine most likely it has to do with your compiler in my unix based system it didn't recongize the conio.h header file try using a different compiler
Reply:you cant declare a char string that way (char string[ctr]). the length of the string has to be a constant size such as "char string[20];". as the other person said, you could also do this "char [] string = new char [ctr];"
Reply:The code you're using isn't C++, it's just C. It'd help if you listed the error.


No comments:

Post a Comment