By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,110 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

Comparing Array values

P: 12
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.
Expand|Select|Wrap|Line Numbers
  1. void CommentEvaluation(/* in */ string codeArray[], int size, ofstream& dataOut)
  2. //This function computes the ratio of total lines of code
  3. //to the number of comment lines, and ratio of total number
  4. //of non comment and non blank lines to the total
  5. //number of code.
  6. //Preconditions:codeArray is defined and has values
  7. //Postconditions: output is sent to file
  8. {
  9.     int counter=0;
  10.     int arrayNumber=0;
  11.     char firstChar;
  12.     int commentTotal=0;
  13.     int lineTotal=0;
  14.     int codeTotal=0;
  15.     float commentRatio;
  16.     float codeRatio;
  17.     while (counter != size)
  18.     {
  19.         codeArray[arrayNumber]=firstChar;
  20.         if (firstChar = "/")
  21.         {
  22.             commentTotal++;
  23.             lineTotal++;
  24.         }
  25.         else
  26.         {
  27.             lineTotal++;
  28.             if (firstChar != " ")
  29.                 codeTotal++;
  30.         }
  31.         counter++;
  32.  
  33.     }
  34.     commentRatio= commentTotal/lineTotal;
  35.     codeRatio= codeTotal/lineTotal;
  36.  
  37.     dataOut << "Ratio of total number of comment lines to the total number of source code lines: " << commentRatio;
  38.     dataOut << "Ratio of total number of non-comment lines and non-blank lines to the total number of source code lines in file. " << codeRatio;
  39. }
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\my documents\visual studio 2008\projects\assign4\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\my documents\visual studio 2008\projects\assign4\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\my documents\visual studio 2008\projects\assign4\assign4\assign4.cpp(64) : error C2040: '!=' : 'int' differs in levels of indirection from 'const char [2]'
1>c:\documents and settings\matt\my documents\visual studio 2008\projects\assign4\assign4\assign4.cpp(70) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
1>c:\documents and settings\matt\my documents\visual studio 2008\projects\assign4\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\My Documents\Visual Studio 2008\Projects\assign4\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?
Nov 30 '08 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 181
Can you post your entire code.
Dec 1 '08 #2

P: 12
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
Dec 1 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.