0

Write a program that will read the two input files "input1.txt" and "input2.txt", which contain only integers. The program should form one output file "output.txt". The program should combine both input files into one output file. The program should compare the numbers read from both files. Write a smaller number in the first column and a larger number in the second. In the third column, write in which file there was a smaller number. If they were the same, write "same" in the third column.

I'm having problems with this program, because first of all I'm a beginner and I'm still learning. Here is my code so far, it's not complete but I don't know what to do next.

int number1, number2, counter1 = 0, counter2 = 0, pom = 0;
ifstream input1;
ifstream input2;
ofstream output;
while (input1 >> number1) {
    counter1++;
    output << number1 << " ";
}
while (input2 >> number2) {
    counter2++;
    output << number2 << " ";
}
if (counter1 > counter2)
    pom = number1;
for (int i = 0; i < counter1; i++) {
    input1 >> number1;
    input2 >> number2;
    if (number1 < number2)
    {
        output << number1 << number2;
        output << "First" << "\n";
    }
    else if (number1 > number2)
    {
        number1 = number2;
        number2 = pom;
        output << number1 << number2;
        output << "Second" << "\n";
    }
    else
    {
        output << number1 << number2;
        output << "Same" << "\n";
    }
        
}


input1.close();
input2.close();
output.close();
return 0;
  • Start with something simpler. Write a program that writes “Hello, world.” to the console. After you’ve got a simple program working you can take on more complicated programs. – Pete Becker Dec 13 '20 at 19:14
  • What is the format of your file? – crackaf Dec 13 '20 at 19:22
  • First two input files are txt and the output file is txt also –  Dec 13 '20 at 19:30
  • @FancyPantsGeneral I wrote the correct way to handle files. You can see your mistakes by comparing them. – crackaf Dec 13 '20 at 19:38

2 Answers2

0

Here's a simple example:

while ((input1 >> number1) && (input2 >> number2))
{
    if (number1 == number2)
    {
        output << number1 << "\t" << number2 << "\t" << "Same\n";
    }
    else
    {
         if (number1 < number2)
         {
             output << number1 << "\t" << number2 << "\t" << "First\n";
         }
         else
         {
             output << number2 << "\t" << number1 << "\t" << "Second\n";
         }
    }
}

In the above example, both numbers are read. The while loop has begun.

I like to start with the == condition first, usually ends up with 2 comparisons and can nest well.

Edit 1: output simplification
There is an alternative, which has only one output statement:

while ((input1 >> number1) && (input2 >> number2))
{
    int largest;
    int smallest;
    std::string result;
    if (number1 == number2)
    {
        smallest = number1;
        largest  = number1;
        result   = "Same";
    }
    else
    {
         if (number1 < number2)
         {
             smallest = number1;
             largest  = number2;
             result   = "First";
         }
         else
         {
             smallest = number2;
             largest  = number1;
             result   = "Second";
         }
    }
    output << smallest << "\t" << largest << "\t" << result << "\n";
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

Your code has many errors. Like you didn't give the input to the stream functions and you are first counting the numbers of digits and then comparing. This code is fully functional.

[input1.txt]
1 2 4 5 6 7 8 8
[input2.txt]
1 9 8 7 6 5 3 4
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int number1, number2;

    ifstream f1("input1.txt");
    ifstream f2("input2.txt");
    ofstream output("output.txt");
    if (!f1.is_open() || !f2.is_open() || !output.is_open())
    {
        cout << "One of your file isn't opening.";
        return 0;
    }
    while ((f1 >> number1) && (f2 >> number2))
    {
        if (number1 > number2)
            output << number1 << " " << number2 << " First\n";
        else if (number2 > number1)
            output << number2 << " " << number1 << " Second\n";
        else
            output << number1 << " " << number2 << " same\n";
    }
    f1.close();
    f2.close();
    output.close();
    return 0;
}
[output.txt]
1 1 same
9 2 Second
8 4 Second
7 5 Second
6 6 same
7 5 First
8 3 First
8 4 First
crackaf
  • 492
  • 2
  • 11
  • 1
    Required reading: [Why eof() is bad practice.](https://stackoverflow.com/questions/5837639/eof-bad-practice) – Thomas Matthews Dec 13 '20 at 19:41
  • @ThomasMatthews thanks for the advice. I have improved the code. – crackaf Dec 13 '20 at 19:54
  • It this functional only for text files that have equal number of integers or can I use it if i have one text file with 7 integers and the other text files with 10 integers? –  Dec 13 '20 at 20:48
  • There should be equal numbers of integers as you are comparing two numbers. If there are 7 in one file and 10 integers in another. The output file will only contain 7 only 7 rows. – crackaf Dec 13 '20 at 21:20