Question

My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...

My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it.

-----------------------------------------------------------------HEADER FILE - Text.h---------------------------------------------------------------------------------------------

/* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/
#ifndef TEXT_H
#define TEXT_H
#include <string.h>


class Text {
private:
   //string line; // Only primitive datatypes are allowed in a Header file. String class objects are not allowed in Header files.
   char ch;
   int upperCasecount, lowerCaseCount, digitCount;

public:
   //Default constructor that initializes all 3 counters to 0.
   Text();

   //Function that reads the argument file and updates the upperCasecount, lowerCaseCount, and digitCount variables during each iteration of the loop
   void updateCounters();
  
   //Function that displays the values stored in all 3 counters
   void displayCounters() const;

};
#endif // !TEXT_H


-------------------------------------------------------------------------------CLASS FILE - Text.cpp -----------------------------------------------------------------------------------------------------

#include<iostream>
#include <string>
#include <fstream>
#include "Text.h"

using namespace std;

class Text {
private:
   string line;
   char ch;
   int charCount;
   int upperCasecount, lowerCaseCount, digitCount;

public:
   //Default constructor that initializes all 4 counters to 0.
   Text()
   {
       charCount = 0;
       upperCasecount = 0;
       lowerCaseCount = 0;
       digitCount = 0;
   }

   //Setter Function that reads the argument file and updates the upperCasecount, lowerCaseCount, and digitCount variables during each iteration of the loop
   void updateCounters(string fname)
   {
       ifstream inputFile;
       inputFile.open(fname);

       if (!inputFile)
       {
           cout << "Error opening file. Make sure the spelling of the filename matches the spelling of the actual file." << endl;
           cout << "Or It may not exist where indicated" << endl;
           system("pause");
       }

       while (!inputFile.eof())
       {
           //Read each line of the file into a string variable called line
           getline(inputFile, line);
           //Get the total number of characters in the line variable using string.length() method and that count to the charCount variable
           charCount = charCount + line.length();

           //Now read each character of the line and store it in the ch variable
           inputFile >> ch;

           //if the character being read is upper case, lower case or a digit, then increment the corresponding counter variables
           if (isupper(ch))
               upperCasecount = upperCasecount + 1;
           if (islower(ch))
               lowerCaseCount = lowerCaseCount + 1;
           if (isdigit(ch))
               digitCount = digitCount + 1;
       }
       inputFile.close();
   }

   //Getter function that displays the values stored in all 3 counters
   void displayCounters() const
   {
       cout << "Total Character count: " << charCount << endl << endl;
       cout << "Uppercase characters: " << upperCasecount << endl;
       cout << "Lowercase characters: " << lowerCaseCount << endl;
       cout << "Digits: " << digitCount << endl << endl;
   }
};

------------------------------------------------------------------------------MAIN PROGRAM - Source.cpp ----------------------------------------------------------------------------------------------------

#include<iostream>
#include <string>
#include <fstream>
#include "Text.h"

using namespace std;

int main()
{
   //Create an object of the type Text
   Text textobj;

   //Call the setter function that counts all the upper case, lower case digit characters in the file
   textobj.updateCounters();

   //Call the Getter function that displays ll the upper case, lower case digit characters in the file
   textobj.displayCounters();

   system("pause");
   return 0;
}

----------------------------------------------------------------------------------COMPILE TIME ERRORS -------------------------------------------------------------------------

text.cpp(8): error C2011: 'Text': 'class' type redefinition

text.h(7): note: see declaration of 'Text'

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The issue here was that the class Text was redefined in Text.cpp even though it was defined in Text.h.There was no need to define class Text in cpp file.

Other comments are provided inline with the code.

Text.h

----------------------------------

#ifndef TEXT_H
#define TEXT_H
#include <string.h>


class Text {
private:
   std::string line; // Only primitive datatypes are allowed in a Header file. String class objects are not allowed in Header files.
   char ch;
   int upperCasecount, lowerCaseCount, digitCount, charCount;

public:
   //Default constructor that initializes all 3 counters to 0.
   Text();

   //Function that reads the argument file and updates the upperCasecount, lowerCaseCount, and digitCount variables during each iteration of the loop
   void updateCounters(std::string fname);

   //Function that displays the values stored in all 3 counters
   void displayCounters() const;

};
#endif // !TEXT_H

--------------------------------------

Text.cpp

-----------------------------------------

#include<iostream>
#include <string>
#include <fstream>
#include "Text.h"

using namespace std;

//No need to define the class Text again as it is being already defined in the header file.

//Default constructor that initializes all 4 counters to 0.
Text::Text()
{
   charCount = 0;
   upperCasecount = 0;
   lowerCaseCount = 0;
   digitCount = 0;
}

//Setter Function that reads the argument file and updates the upperCasecount, lowerCaseCount, and digitCount variables //during each iteration of the loop

void Text::updateCounters(string fname)
{
   ifstream inputFile;
   inputFile.open(fname);

   if (!inputFile)
   {
       cout << "Error opening file. Make sure the spelling of the filename matches the spelling of the actual file." << endl;
       cout << "Or It may not exist where indicated" << endl;
       system("pause");
   }

   while (!inputFile.eof())
   {
       //Read each line of the file into a string variable called line
       getline(inputFile, line);
       //Get the total number of characters in the line variable using string.length() method and that count to the charCount variable
       charCount = charCount + line.length();

       //Now read each character of the line and store it in the ch variable
       //Earlier we were reading the character from file
       //inputFile >> ch which was wrong
       for (size_t i = 0; i < line.length(); i++)
       {
           ch = line[i];

           //if the character being read is upper case, lower case or a digit, then increment the corresponding counter variables
           if (isupper(ch))
               upperCasecount = upperCasecount + 1;
           if (islower(ch))
               lowerCaseCount = lowerCaseCount + 1;
           if (isdigit(ch))
               digitCount = digitCount + 1;
       }
   }
   inputFile.close();
}

//Getter function that displays the values stored in all 3 counters
void Text::displayCounters() const
{
   cout << "Total Character count: " << charCount << endl << endl;
   cout << "Uppercase characters: " << upperCasecount << endl;
   cout << "Lowercase characters: " << lowerCaseCount << endl;
   cout << "Digits: " << digitCount << endl << endl;
}

---------------------------------------

Source.cpp

---------------------------------------

#include <iostream>
#include <string>
#include <fstream>
#include "Text.h"

using namespace std;

int main()
{
   //Create an object of the type Text
   Text textobj;

   //Call the setter function that counts all the upper case, lower case digit characters in the file
   //File name given is input.txt which is present in the same folder as the cpp file.
   textobj.updateCounters("input.txt");

   //Call the Getter function that displays ll the upper case, lower case digit characters in the file
   textobj.displayCounters();

   system("pause");
   return 0;
}

OUTPUT:

Total Character count: 170 Uppercase characters: 15 Lowercase characters: 36 Digits: 45 Press any key to continue .. .

Add a comment
Know the answer?
Add Answer to:
My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am getting a seg fault with my huffman tree. I'm not sure if it's my...

    I am getting a seg fault with my huffman tree. I'm not sure if it's my deconstructors but also getting some memory leaks. Can some one help me find my seg fault? It's in c++, thanks! //#ifndef HUFFMAN_HPP //#define HUFFMAN_HPP #include<queue> #include<vector> #include<algorithm> #include<iostream> #include <string> #include <iostream> using namespace std; class Node { public:     // constructor with left and right NULL nodes     Node(char charTemp, int c) {       ch = charTemp;       count = c;       left...

  • I am trying to run this program in Visual Studio 2017. I keep getting this build...

    I am trying to run this program in Visual Studio 2017. I keep getting this build error: error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". 1>Done building project "ConsoleApplication2.vcxproj" -- FAILED. #include <iostream> #include<cstdlib> #include<fstream> #include<string> using namespace std; void showChoices() { cout << "\nMAIN MENU" << endl; cout << "1: Addition...

  • Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2...

    Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2 - Stack around the variable 'string4b' was corrupted. I need some help because if the arrays for string4a and string4b have different sizes. Also if I put different sizes in string3a and string4b it will say that those arrays for 3a and 3b are corrupted. But for the assigment I need to put arrays of different sizes so that they can do their work...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • I need to update this C++ code according to these instructions. The team name should be...

    I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...

  • C++ Help. PLEASE include detailed comments and explanations. PLEASE follow the drections exactly. Thank you. Define...

    C++ Help. PLEASE include detailed comments and explanations. PLEASE follow the drections exactly. Thank you. Define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member...

  • In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits&gt...

    In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits> #include <iostream> #include <string> // atoi #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ const unsigned int DEFAULT_SIZE = 179; // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() {...

  • Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from...

    Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from lab 4.1, add the ability to read from a file. Modify the input function:       * Move the input function out of the Cargo class to just below the end of the Cargo class       * At the bottom of the input function, declare a Cargo object named         temp using the constructor that takes the six parameters.       * Use the Cargo output...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT