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
  • Here is the skeleton of the password checking program. We have the character counting function (you...

    Here is the skeleton of the password checking program. We have the character counting function (you will need to finish it), and a function to check if the password is good. Change the password algorithm so that (three of the four) two uppercase, two lowercase, two digits, or one other is present. Also change the required length of the password to 10. Bonus points for making the program loop until a good password is entered. You will need to read...

  • i am having issues correcting an error in my program here is my code: #include <iostream>...

    i am having issues correcting an error in my program here is my code: #include <iostream> // define libraries #include <fstream> using namespace std; // Variable definitions: ifstream infp; // file handler enum Tokens {INT_LIT, IDENT, ASSIGN_OP, ADD_OP, SUB_OP, MUL_OP, DIV_OP, LEFT_PAREN, RIGHT_PAREN, LETTER, DIGIT, UNKNOWN,ENDFILE}; Tokens nextToken; // nextToken read from the file. int charClass; // char class char lexeme [100]; // number of characters per line char nextChar; // next char read from the file int lexLen; //...

  • 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...

  • It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #inclu...

    It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) {    this->type = type;    this->name = name; } //returns the type. string File::getType() {    return type; } //returns the name of file. string File::getName() {    return name; } File.h #ifndef __FILE_H__ #define...

  • In this lab, you will need to implement the following functions in Text ADT with C++...

    In this lab, you will need to implement the following functions in Text ADT with C++ language(Not C#, Not Java please!): PS: The program I'm using is Visual Studio just to be aware of the format. And I have provided all informations already! Please finish step 1, 2, 3, 4. Code is the correct format of C++ code. a. Constructors and operator = b. Destructor c. Text operations (length, subscript, clear) 1. Implement the aforementioned operations in the Text ADT...

  • 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 using visual studio for it. Make a copy of the Rational class you...

    C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...

  • 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...

  • 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...

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