// Purpose: Counts the number of non-blank lines and non-whitespacecharacters fo
ID: 3617181 • Letter: #
Question
// Purpose: Counts the number of non-blank lines and non-whitespacecharacters found in the input.
// Note: A line is non-blank if it has at least one non-whitespacecharacter appearing in it.
// A character is anon-whitespace character if it is not ' ', ' ', or' '.
//include files...
#include <iostream>
using namespacestd;
int main()
{
intLineCounter; // countsthe number of lines
intCharCounter; // countsthe number of chars
charletter; // letter that isread
boolflag; // check for non-blank line
LineCounter =0; //initialize the line counter
CharCounter =0; //initialize the char counter
cin.get(letter); // read thefirst letter
while ( cin ) // loop until end of thefile
{
// starting a new line whenever the control reaches here
flag = ___________ // no non-whitespacecharacters read yet
while ( cin && letter != ' ' ) // read a line andprocess
{
// if the letter is a non-whitespace character
// change the value of flag to indicate that
// and update CharCounter
//Write an if statement here
cin.get(letter); // read the next letter
} // end of inner while loop
// If flag has beenset, update the non-blank LineCounter
Write an if statement here
Explanation / Answer
EDITED:I have coded something that accomplishes what your lookingfor. If you are able to use what I am pasting here let meknow and I will post it as an answer so I can get rated. Compiled and tested with g++ ------------------------------------------------- // Find Non-Whitespace // March 3rd 2010 // Created By Nic Raboy
#include <iostream> #include <string> #include <fstream>
using namespace std;
int main() { string str; ifstream myFile("data.txt"); // File that contains lines oftext int lineCount = 0; int charCount = 0; // If the file opened correctly if(myFile.is_open()) { // Read line by line until the end of the file while(!myFile.eof()) { // Get the current line and store as a string getline(myFile, str); // If the line is not empty if(str != "") { // Loop through every character to make sure your line hassomething that is not whitespace for(int i = 0; str[i] != ''; i++) { // If your line does not have just whitespace characters if(str[i] != ' ' && str[i] != ' ' && str[i]!= ' ') { lineCount++; // Increase the line count break; // You have proven the line is not only whitepsace sogo ahead and exit the loop } } // Loop until the termination character of the string so thatway you // check every character for(int i = 0; str[i] != ''; i++) { // Check to make sure the current character is not awhitespace character if(str[i] != ' ' && str[i] != ' ' && str[i]!= ' ') { charCount++; // Increase the character count } } } } myFile.close(); // Close the file because you are done } // Print out the calculations cout << "Line Count: " << lineCount <<endl; cout << "Character Count: " << charCount <<endl; return 0; } --------------------------------------------
Nic Raboy // Find Non-Whitespace // March 3rd 2010 // Created By Nic Raboy
#include <iostream> #include <string> #include <fstream>
using namespace std;
int main() { string str; ifstream myFile("data.txt"); // File that contains lines oftext int lineCount = 0; int charCount = 0; // If the file opened correctly if(myFile.is_open()) { // Read line by line until the end of the file while(!myFile.eof()) { // Get the current line and store as a string getline(myFile, str); // If the line is not empty if(str != "") { // Loop through every character to make sure your line hassomething that is not whitespace for(int i = 0; str[i] != ''; i++) { // If your line does not have just whitespace characters if(str[i] != ' ' && str[i] != ' ' && str[i]!= ' ') { lineCount++; // Increase the line count break; // You have proven the line is not only whitepsace sogo ahead and exit the loop } } // Loop until the termination character of the string so thatway you // check every character for(int i = 0; str[i] != ''; i++) { // Check to make sure the current character is not awhitespace character if(str[i] != ' ' && str[i] != ' ' && str[i]!= ' ') { charCount++; // Increase the character count } } } } myFile.close(); // Close the file because you are done } // Print out the calculations cout << "Line Count: " << lineCount <<endl; cout << "Character Count: " << charCount <<endl; return 0; } --------------------------------------------
Nic Raboy
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.