Question

c++ please and please put explanation ! Write a function that determines if there are three...

c++ please and please put explanation ! Write a function that determines if there are three of the same line in a row in a file. The function returns a bool and is passed a string which contains the filename.

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// dataTxt.txt

Hello this is rahul
Hello this is rahul
Hello am sachin
Hello Mahi
Hello this is rahul
Hello am sachin
Hello am sachin
Hello am sachin
Hello Mahi

______________________


#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

bool readFile(string filename);
int main() {
  

  
  

   bool b=readFile("dataTxt.txt");
   if(b)
   {
       cout<<"There is three consecutive same lines"<<endl;
   }
   else
   {
       cout<<"There is no three consecutive same lines"<<endl;
   }

  
  
return 0;
}
bool readFile(string filename)
{
   //defines an input stream for the data file  
ifstream dataIn;
  
   //Opening the input file
dataIn.open(filename.c_str());
//checking whether the file name is valid or not
if(dataIn.fail())
{
   cout<<"** File Not Found **";
   exit(0);
}
else
{
   string line;
   int cnt=0;
   while(getline(dataIn,line))
   {
       cnt++;
   }
   dataIn.close();
  
   // Creating array dynamically
string* lines = new string[cnt];
  
//Opening the input file
dataIn.open(filename.c_str());
for(int i=0;i<cnt;i++)
{
   getline(dataIn,line);
   lines[i]=line;
   }
   dataIn.close();
   for(int i=0;i<cnt-2;i++)
   {
       if(lines[i].compare(lines[i+1])==0 && lines[i+1].compare(lines[i+2])==0)
       return true;
   }
  
}
return false;
}

_______________________________

Output: (Console)

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
c++ please and please put explanation ! Write a function that determines if there are three...
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
  • In C++, create a recursive bool function which determines if a string is symmetrical or not....

    In C++, create a recursive bool function which determines if a string is symmetrical or not. A symmetrical string can be read the same front to back (ex. kayak). Function Blueprint --------------------- bool isSymmetrical(string s) if (s is the empty string or s is of length 1) return true else if (s's first and last characters are the same letter)   return isSymmetrical (s minus its first and last characters) else return false

  • Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and...

    Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and write the output file; the output file should be the same as the input file, except that it has no e’s. For example, if the input file had the line. Streets meet in the deep. Then the output file would have...

  • python program please Write a function called backwards which takes two parameters. The first is a...

    python program please Write a function called backwards which takes two parameters. The first is a file object that has already been opened in read mode called essayfile. The second is called filename, which contains the filename for the output file that will need to be properly opened and closed in your function. The function should read from already opened file object line by line. Each line in the file should have the words on the line reversed. For example,...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • C++ 1) Write a function named WordCount, which determines the number of words in a “C...

    C++ 1) Write a function named WordCount, which determines the number of words in a “C type” string. The function will have one parameter (the address of the string) and will return the number of words (words are separated by one or more spaces). (15 points) 2) Write a function named VowelConsonant which will have three parameters - all “C type” strings. The function will place all vowels (the characters a, e, i, o, u) from the first string into...

  • In C++ Design a format for storing graphs in files. This will store your graph into a file with t...

    in C++ Design a format for storing graphs in files. This will store your graph into a file with the following requirements: The first line will contain the number of Vertices. The second line will have a ‘U’ or a ‘D’ to tell the system if it is Undirected or Directed. The rest of the lines will be here for each of the edges. Each edge will contain three pieces of information: An integer containing the first Vertex An integer...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • python Write the function getSpamLines(filename) that read the filename text file and look for lines of the form &#...

    python Write the function getSpamLines(filename) that read the filename text file and look for lines of the form 'SPAM-Confidence: float number'. When you encounter a line that starts with "SPAM-Confidence:" extract the floating-point number on the line. The function returns the count of the lines where the confidence value is greater than 0.8. For example, if 'spam.txt' contains the following lines along with normal text getSpamLines('spam.txt') returns 2. SPAM-Confidence: 0.8945 Mary had a little lamb. SPAM-Confidence: 0.8275 SPAM-Confidence: 0.7507 The...

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