fdsfafsdfasd
fdsfafsdfasd

Reputation: 9

My c++ file doesn't output values into txt fiels correctly

Everytime I run my code, it is supposed to take the new values and put it their respective files. the variable inf is supposed to be put into the text file everytime it runs through the if statement.

#include <iostream>
#include <unistd.h>
#include <fstream>
using namespace std;
#include <time.h>
long long int inf = 0;
long long int count = 0;
int main()
{

    system("clear");
    fstream m;
    m.open("x.txt", ios::out | ios::trunc);
    m.close();
    m.open("y.txt", ios::out | ios::trunc);

    m.close();
    cout << "Enter any value: ";
    cin >> inf;
    while (inf != 1) {
        if (inf % 2 == 1) {
            cout << "Number was odd, 3x + 1\n";
            inf = 3 * inf;
            inf = inf + 1;
            cout << inf << " \n";
            count = count + 1;
            cout << count;
        }
        else if (inf % 2 == 0) {
            cout << "Number was even, x/2 \n";

            inf = .5 * inf;
            cout << inf << "\n";
            count = count + 1;
        }
        m.open("x.txt");
        m << count << "\n";
        m.close();
        m.open("y.txt", ios::out);
        m << inf << "\n";
        m.close();
        usleep(500);
    }
}

It should put in the values 130,65, and so on, but it only outputs 1

Upvotes: 0

Views: 51

Answers (1)

Pavel Skipenes
Pavel Skipenes

Reputation: 369

I've refactored your code a little bit. Here are the changes:

  • Use descriptive variable names. Using m as a variable name for a filestream does not make any sence.
  • Open and close files outside loops. You're stil using them.
  • You're working with two files so use two file streams.
  • Simplify the odd / even check. A value can only be even or odd so check it once.
  • then you see the problem at the commented line. The issue is that you try to add 0.5 into input_value. Input value is an int. Int can store only integers (whole numbers). ei -1, -2 , 2, 4 and so on...

I've not tested these changes but i think your issue might be solved by just replacing input_value's data type from int to float.

#include <iostream>
#include <unistd.h>
#include <fstream>
using namespace std;
#include <time.h>

int main()
{
    int inf = 0;
    int count = 0;
    system("clear");
    fstream x_file;
    fstream y_file;
    x_file.open("x.txt", ios::out | ios::trunc);
    y_file.open("y.txt", ios::out | ios::trunc);

    // get userinput
    cout << "Enter any value: ";
    cin >> input_value;

    while (input_value != 1) {
        // check if value is even
        if (!(input_value % 2)) {
            input_value = 3 * input_value + 1;
            cout << "Number was odd, 3x + 1\n" << input_value << " \n";
            cout << ++count;
            continue;
        } else {
            input_value = .5 * input_value; // input_value is an int. Passing in  a float will not change input value into a float data type
            cout << "Number was even, x/2 \n" << input_value << "\n";
            count++;

        }
        
        x_file << count << "\n";
        y_file << input_value << "\n";
    }
    x_file.close();
    file_stream.close();
}

Upvotes: 1

Related Questions