I have to write a program that takes the lines of code from a .cpp as a string into an array. Then I need to compute the ratio of total lines of code to the number of comment lines, and ratio of total number of non comment and non blank lines to the total number of code.
This is the void function that I am using to get these ratio's.
To explain a bit, size is a variable from the main function which is the last array row. I then have the array equal a char value firstChar which I hope takes the first character from the string in each cell from the array. Then if it equals "/" which is the beginning of a comment line. (I have errors on this line I will paste later) In the else statement I have, if firstChar != " " which I would think would be a blank line ? That equals a code line total (a line that isn't a comment or a blank line, I have errors on this line of code too)
The error report is :
1>c:\documents and settings\matt\m y documents\visua l studio 2008\projects\a ssign4\assign4\ assign4.cpp(56) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1> There is no context in which this conversion is possible
1>c:\documents and settings\matt\m y documents\visua l studio 2008\projects\a ssign4\assign4\ assign4.cpp(64) : error C2446: '!=' : no conversion from 'const char *' to 'int'
1> There is no context in which this conversion is possible
1>c:\documents and settings\matt\m y documents\visua l studio 2008\projects\a ssign4\assign4\ assign4.cpp(64) : error C2040: '!=' : 'int' differs in levels of indirection from 'const char [2]'
1>c:\documents and settings\matt\m y documents\visua l studio 2008\projects\a ssign4\assign4\ assign4.cpp(70) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
1>c:\documents and settings\matt\m y documents\visua l studio 2008\projects\a ssign4\assign4\ assign4.cpp(71) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
1>Build log was saved at "file://c:\Documents and Settings\Matt\M y Documents\Visua l Studio 2008\Projects\a ssign4\assign4\ Debug\BuildLog. htm"
1>assign4 - 3 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
So it seems me comparing the char value to "/" and " " is creating some errors, any idea's on how to solve this?
This is the void function that I am using to get these ratio's.
Code:
void CommentEvaluation(/* in */ string codeArray[], int size, ofstream& dataOut) //This function computes the ratio of total lines of code //to the number of comment lines, and ratio of total number //of non comment and non blank lines to the total //number of code. //Preconditions:codeArray is defined and has values //Postconditions: output is sent to file { int counter=0; int arrayNumber=0; char firstChar; int commentTotal=0; int lineTotal=0; int codeTotal=0; float commentRatio; float codeRatio; while (counter != size) { codeArray[arrayNumber]=firstChar; if (firstChar = "/") { commentTotal++; lineTotal++; } else { lineTotal++; if (firstChar != " ") codeTotal++; } counter++; } commentRatio= commentTotal/lineTotal; codeRatio= codeTotal/lineTotal; dataOut << "Ratio of total number of comment lines to the total number of source code lines: " << commentRatio; dataOut << "Ratio of total number of non-comment lines and non-blank lines to the total number of source code lines in file. " << codeRatio; }
The error report is :
1>c:\documents and settings\matt\m y documents\visua l studio 2008\projects\a ssign4\assign4\ assign4.cpp(56) : error C2440: '=' : cannot convert from 'const char [2]' to 'char'
1> There is no context in which this conversion is possible
1>c:\documents and settings\matt\m y documents\visua l studio 2008\projects\a ssign4\assign4\ assign4.cpp(64) : error C2446: '!=' : no conversion from 'const char *' to 'int'
1> There is no context in which this conversion is possible
1>c:\documents and settings\matt\m y documents\visua l studio 2008\projects\a ssign4\assign4\ assign4.cpp(64) : error C2040: '!=' : 'int' differs in levels of indirection from 'const char [2]'
1>c:\documents and settings\matt\m y documents\visua l studio 2008\projects\a ssign4\assign4\ assign4.cpp(70) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
1>c:\documents and settings\matt\m y documents\visua l studio 2008\projects\a ssign4\assign4\ assign4.cpp(71) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
1>Build log was saved at "file://c:\Documents and Settings\Matt\M y Documents\Visua l Studio 2008\Projects\a ssign4\assign4\ Debug\BuildLog. htm"
1>assign4 - 3 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
So it seems me comparing the char value to "/" and " " is creating some errors, any idea's on how to solve this?
Comment