简体   繁体   中英

C++: How to swap integers in text files?

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;

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";
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM