473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Having char variable access parts of a string

I have an array of string values, and I need to access each character of the string individually. For instance I want my char variable to equal the first character in the string, and then compare it to character values using if statements. Later in the program I will need to not only access the first character but all of them individually in each string. How can I set that up?

Here is my code for trying to have the charcter variable equal the first character in the string.

Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void CommentEvaluation(string codeArray[], int size, ofstream& dataOut);
  8.  
  9. int main()
  10. {
  11.     int size=0;
  12.     ifstream dataIn;
  13.     ofstream dataOut;
  14.     string codeArray[1000];
  15.     string temp;
  16.  
  17.     dataIn.open("someProgram.cpp");
  18.     dataOut.open("someProgram.txt");
  19.  
  20.     while(!dataIn.eof())
  21.     {
  22.         while (size<1000)
  23.         {
  24.             getline(dataIn, temp);
  25.  
  26.             codeArray[size]=temp;
  27.             size++;
  28.         } 
  29.     }
  30.  
  31.  
  32.     CommentEvaluation(codeArray, size, dataOut);
  33.     cout << codeArray[size];
  34.     return 0;
  35. }
  36.  
  37. void CommentEvaluation(/* in */ string codeArray[], int size, ofstream& dataOut)
  38. //This function computes the ratio of total lines of code
  39. //to the number of comment lines, and ratio of total number
  40. //of non comment and non blank lines to the total
  41. //number of code.
  42. //Preconditions:codeArray is defined and has values
  43. //Postconditions: output is sent to file
  44. {
  45.     int counter=0;
  46.     int arrayNumber=0;
  47.     char firstChar;
  48.     int commentTotal=0;
  49.     int lineTotal=0;
  50.     int codeTotal=0;
  51.     float commentRatio;
  52.     float codeRatio;
  53.     while (counter != size)
  54.     {
  55.         firstChar=codeArray[arrayNumber];
  56.         if (firstChar == '/')
  57.         {
  58.             commentTotal++;
  59.             lineTotal++;
  60.         }
  61.         else
  62.         {
  63.             lineTotal++;
  64.             if (firstChar != ' ')
  65.                 codeTotal++;
  66.         }
  67.         counter++;
  68.         arrayNumber++;
  69.  
  70.     }
  71.     commentRatio= commentTotal/lineTotal;
  72.     codeRatio= codeTotal/lineTotal;
  73.  
  74.     dataOut << "Ratio of total number of comment lines to the" << endl; 
  75.     dataOut << "total number of source code lines: " << commentRatio << endl;;
  76.     dataOut << "Ratio of total number of non-comment lines and" << endl; 
  77.     dataOut << "non-blank lines to the total number of source" << endl;
  78.     dataOut << "code lines in file: " << codeRatio << endl;
  79. }
The part I am talking about is in the void function where firstChar=codeArray[arrayNumber] it cannot convert the string into a char. In a larger sense this program is made to get the lines of code from another .cpp file and store them into an array. For my void function it is supposed to compare comment lines and blank lines to the total number of lines.

This is the error I am getting:
c:\documents and settings\matt\my documents\visual studio 2008\projects\assign4\assign4\assign4.cpp(55) : error C2440: '=' : cannot convert from 'std::string' to 'char'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Thanks for any help.
Dec 1 '08 #1
3 2558
vmpstr
63
Because codeArray is an array of strings, codeArray[arrayNumber] is a string, not a char. You were probably thinking something along the lines of codeArray[arrayNumber][0]

That is your problem.

Also, just skimming through your code, I noticed that line 26 will probably cause a segmentation fault, because size at that point is 1000 and that's more than you have allocated.

Also, from a logic point of view
Expand|Select|Wrap|Line Numbers
  1. float a = 10.0, b = 20.0 c;
  2.  
  3. c =
  4. /
  5. b;
  6.  
is code with no comments, which will compile and work fine. Your code (at least from a brief look) will count at least one comment.
Dec 1 '08 #2
I know that the array has multiple strings, not character type. I need to know how to give the first character in each string to the variable firstChar so that it is a character so I can compare it to '/' and ' '. Which I don't know how to do.

From what you said above, "codeArray[arrayNumber][0]" will give me the first character?

Thanks for replying

EDIT: ok cool, that is working it counted 48 comments in the program. Now I've run into another problem though (they never end! lol)

In the main function I have it set up so it takes in the lines of code into the array until it reaches the end of the .cpp file (this is what I want it to do). Instead it counts all the way to 1000 which is the size variable. Size is supposed to stop incrementing once it reaches the end of file which is around 290 lines. I'm thinking my error is in the while statement
Expand|Select|Wrap|Line Numbers
  1. while(!dataIn.eof())
  2.     {
  3.         while (size<1000)
  4.         {
  5.             getline(dataIn, temp);
  6.  
  7.             codeArray[size]=temp;
  8.             size++;
  9.         } 
  10.     }
where the !dataIn.eof() isn't doing anything? I want it to stop after it recieves the last line of code from the .cpp.
Thanks again for the help before
Dec 1 '08 #3
boxfish
469 Expert 256MB
I don't think you want a nested loop here. You want it to stop when size gets too big or when you reach the end of the file; whichever happens first, right? So you can use
Expand|Select|Wrap|Line Numbers
  1. while(size<1000 && !dataIn.eof())
I hope this works.
Dec 1 '08 #4

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

Similar topics

12
by: LongBow | last post by:
Hello all, From doing a google serach in the newsgroups I found out that a string can't be returned from a function, but using a char* I should be able to do it. I have spent most of the day...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
23
by: Nikhil Patel | last post by:
Hi all, How can I convert an integer to its equivalent ascii character without using Microsoft.VisualBasic dll or any other dll(I want to reference only System.dll). Thanks. -Nikhil
5
by: mwebel | last post by:
Hi!, im trying to copy the middle part of a dinamycally created char* string with memcopy but all i get is rubbish... I understand, that malloc can allocate memory wherever it wants and it does...
7
by: Jack | last post by:
Hi, I am having some problems with char and string datatypes. Here is an example int main(){ string str; cout << "enter string: "; cin >str; }
8
by: sabby | last post by:
I want to use the getline() so that i can enter a entire name in on line. (with spaces) The prob is that i am initializing the variable as "N/A" and saving it to a text file. it is declared as a...
3
by: ommail | last post by:
Hi I wonder if regular expressions are in general sower than using classes like String and Char when used for validating/parsing text data? I've done some simple test (using IsMatch()) method...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.