473,804 Members | 3,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing Array values

12 New Member
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\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?
Nov 30 '08 #1
2 2745
svlsr2000
181 Recognized Expert New Member
Can you post your entire code.
Dec 1 '08 #2
TamaThps
12 New Member
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

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

Similar topics

12
885
by: Elijah Bailey | last post by:
I have two char arrays of size k. I want to know which one is bigger (exactly like for instance I compare two ints/longs/etc.). What is the fastest way to do this? k <= 10 usually for my application. I tried bcmp / a loop for comparison, but it seems they are very slow compared to comparing longs...Any ideas? I tried splitting the array into longs and comparing, but then I face the high endian/low endian problem on some machines.
19
2659
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor = Color.Empty then..... or if mycolor is Color.Empty then .......
2
2065
by: darrel | last post by:
I have two comma delimted strings that I need to compare individual values between the two. I assume the solution is likely to put them into an array? If so, do I need to loop through one, comparing the other, or is there some sort of compare function already in .net? For instance, I have these strings: str1: ,3,6,12,17, str2: ,7,8,12,
1
7699
by: psmahesh | last post by:
Hi folks, I am comparing two arrays and removing matches from the second array from the first array. Can someone take a look at this code below and mention if this is okay and perhaps if there is a better way to achieve it for(i=0;i<arrayA.length;i++){ for(j=0;j<arrayB.length;j++){ if(arrayA==arrayB)
20
2171
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is mmapped to a single page. I want to determine if the second is on the page. I'd like to do: #include "platform_appropriate_definition_of_PAGESIZE.h" int compare1(const char *a, const char *b)
3
8677
by: riauvision | last post by:
Hello, I have problem in writing ms Access vba code to compare two array taken from two different recordset. One is from table: Detail Orders (PK:,POID,Product.Code,Qty) and the other is from table (PK:InventoryID, POID, ProductID.Code, Qty). I retrive the and store its values in an array and also the in another array. My problem is How to compare the two arrays so that I can update the inventory? My code :
25
5721
by: galapogos | last post by:
Hi, I'm trying to compare an array of unsigned chars(basically just data without any context) with a constant, and I'm not sure how to do that. Say my array is array and I want to compare it with the constant 0x00010203040506070809 where array == 0x00, array == 0x01, etc. How do I do that. Obviously I can't use memcmp() with the 0x00010203040506070809 since the compiler will use that constant as an address and give me a segfault. Other...
2
3390
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function checkInteger(&$value, $checks) { $err = ''; if (!is_numeric($value) || (floatval($value) != intval($value))) { $err .= 'Input must be an integer. ';
25
13064
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any suggestions?
5
1896
by: SneakyElf | last post by:
I need to write a function where the third parameter points to an address whose dereferenced value equals one of the array element's value, then the the function returns the index of the first matching array element. Otherwise -1 is returned. Here's what I have but I am going obviously wrong somewhere: #include <iostream> using namespace std; // This function compares values
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.