Hello,
I am trying to figure out how I can count characters in a line, but not include the spaces in this count. I realize that cin won't see "whitespace", but it isn't being very kind to me. I first tried cin.get(), but rather than going through the while loop, it just read the first line, then stopped. So far using the code I have, I can count the characters with the spaces included of each line, and display the line number, the line text itself and how many characters were in that line. Now if I could just get rid of those pesky spaces!
Oh yes, one other troubling feature of my code is an annoying C4267 warning concerning the variable i. That variable is being set equal to line.size(). When I give "i" type size_t, then I get: C4267: 'argument' : conversion from 'size_t' to 'unsigned int', possible loss of datang. When I assign "i" as an unsigned int, then the warning remains the same. I am not sure what to do in this case. Any ideas?
Thank you for your time.Code:#include <iostream> #include <fstream> #include <string> #include <cassert> using namespace std; int charCount = 0; int main() { string fileName; cout << "Enter name of file: "; getline(cin, fileName); ifstream inFile(fileName.data() ); assert(inFile.is_open()); unsigned lineCount = 0; unsigned i = 0; string line; while (!inFile.eof()) { getline(inFile, line); lineCount++; cout << "\nline #" << lineCount; cout << " " << line << "\n"; i = line.size(); cout << i << " characters in this line\n\n"; } inFile.close(); return 0; }