Question

In: Computer Science

Hi! I am trying to compare 2 files, and figure out which lines from infile2 are...

Hi!

I am trying to compare 2 files, and figure out which lines from infile2 are also in infile1, and output the line index, from infile2, of common lines. I would also want to see which lines (actual string line) are common.

Below is my c++ program, and I am not sure what I am doing wrong. Can someone tell me what I am doing wrong and how it can be fixed?

_________________________________________________________main.cpp

#include<iostream>

#include<fstream>

#include<vector>

#include<string>

using namespace std;


int main(int argc, char *argv[]){

string myString1, myString2;

vector<string> string1, string2;

int i = 0;

int y = 0;

ifstream infile1("LeftShift2.txt");

ifstream infile2("LeftShift3.txt");

if(infile1.is_open() && infile2.is_open())

{

while(!infile1.eof() && !infile2.eof())

{

getline(infile1, myString1);

string1.push_back(myString1);

getline(infile2, myString2);

string2.push_back(myString2);

if(string1.at(i) != string2.at(y))

{

y++;

}

else if(string1.at(i) == string2.at(y))

{

i++;

}

}

cout<<"common line is at: " << y << endl;

cout<<"The common lines are: "<< string2.at(y) << endl;

}

infile1.close();

infile2.close();

return 0;

}

__________________________________________LeftShift2.txt

Adios amiga mia
Bella ciao
Si le monde etait aussi petit
On ne serait pas aussi grandiose
Pays du lait et du miel
Alors, on danse?!

-------------------------------------------------------------LeftShift3.txt

Pays du froid et des montagnes
Burundi
Pays du lait et du miel
Adios amiga mia

-------------------------------------------------------------Output (assume count line start at 0)

common line is at: 2
The common lines are: Pays du lait et du miel

common line is at: 3
The common lines are: Adios amiga mia

Solutions

Expert Solution

If you have any doubts, please give me comment....

#include <iostream>

#include <fstream>

#include <vector>

#include <string>

using namespace std;

int main(int argc, char *argv[])

{

    string myString1, myString2;

    vector<string> string1;

    int i = 0;

    int y = 0;

    ifstream infile1("LeftShift2.txt");

    ifstream infile2("LeftShift3.txt");

    if (infile1.is_open() && infile2.is_open())

    {

        while (!infile1.eof())

        {

            getline(infile1, myString1);

            string1.push_back(myString1);

        }

        int i=0;

        while(!infile2.eof()){

            getline(infile2, myString2);

            for(int j=0; j<string1.size(); j++){

                if(myString2.compare(string1.at(j))==0){

                    cout << "common line is at: " << i << endl;

                    cout << "The common lines are: " << myString2 << endl;

                }

            }

            i++;

        }

    }

    infile1.close();

    infile2.close();

    return 0;

}


Related Solutions

I am trying to figure out which test analysis to use for my research questions. I...
I am trying to figure out which test analysis to use for my research questions. I was thinking about think about multivariate because of the number of variable being addressed in the study but there is also the possibility to use univariate to address each question. What are the current levels of police satisfaction in CMPD jurisdictions? What is the public’s perception of crime in CMPD jurisdictions? Does “hot spot” policing reduce crime in CMPD jurisdictions? How does broken windows...
I am trying to figure out the social security tax for the employees in this problem,...
I am trying to figure out the social security tax for the employees in this problem, becuase I have to post it all in a chart. I have tried multiplying by the 6.2 tax rate but my program says my answer is wrong. So, is there a different way to calculate social security tax or is there something that I am missing that the problem is asking me to do? I am not looking for answers, but just a clear...
I am trying to figure out the best way to solving a problem in the language...
I am trying to figure out the best way to solving a problem in the language python. I have some but have no clue if I am even going in the right direction. Here are the instructions: Write a program that calculates the shopping list for a birthday party with the minimum amount of leftovers. The program should ask the user for the number of kids attending the party. Assume each kid will cook (but not necessarily eat) 2 hot...
This is a java program I am trying to figure out how to add a couple...
This is a java program I am trying to figure out how to add a couple methods to a program I am working on. I need to be able to: 1. Remove elements from a binary tree 2. Print them in breadth first order Any help would appreciated. //start BinaryTree class /** * * @author Reilly * @param <E> */ public class BinaryTree { TreeNode root = null; TreeNode current, parent; private int size = 0, counter = 0; public...
I am trying to figure out the probability, expected value, variance, and standard deviation for a...
I am trying to figure out the probability, expected value, variance, and standard deviation for a series of dice rolls. For example, if I roll a six-sided die in an attempt to roll a 1, and it takes me 7 rolls before a 1 appears, what are those answers? I have figured out the probability equation: P(P-1)^x where x is the number of rolls - 1 so for 7 rolls the probability would be: 1/6(1-1/6)^6 = 0.05581632... Further where I...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
I am having problems trying to figure out what information to pull to prepare budgets
I am having problems trying to figure out what information to pull to prepare budgets
I am trying to figure out how to properly execute a while loop and a do/while...
I am trying to figure out how to properly execute a while loop and a do/while loop in the same program using java. I am stuck at just writing the while loop. I made a condition but I can't figure out how to get it to loop if the condition is true?? Also I have to write a do/while loop in the same program. here are the details: This lab demonstrates the use of the While Loop and the Do...
I am trying to figure out how to calculate the sustainable earnings: Permanent Versus Transitory Earnings...
I am trying to figure out how to calculate the sustainable earnings: Permanent Versus Transitory Earnings Entrust, Inc., is a global provider of security software; it operates in one business segment involving the design, production, and sale of software products for securing digital identities and information. The consolidated statements of operations for a three-year period (all values in thousands) follows. On January 1, Year 1, the Entrust common shares traded at $10.40 per share; by year end Year 3, the...
Hi, I am trying to write a php file which control the text size, color and...
Hi, I am trying to write a php file which control the text size, color and the parameters of a registration page. My code is not working, the registration page always stay in the default color. Could I get some help ? My code is below. display file <?php session_start() ?> <!DOCTYPE html> <html> <head>    <meta charset="UTF-8">    <title>User display window</title>    <style>        .center { text-align:center}        </style>    </head> <body>    <fieldset>        <legend...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT