Comparing Array values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TamaThps
    New Member
    • Oct 2008
    • 12

    Comparing Array values

    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.
    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;
    }
    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?
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    Can you post your entire code.

    Comment

    • TamaThps
      New Member
      • Oct 2008
      • 12

      #3
      I've figured out some of my problems but have run into more on the way, the new question can be found here http://bytes.com/answers/c/857099-ha...ng#post3438862

      thanks

      Comment

      Working...