Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:21:11+00:00 2026-06-14T02:21:11+00:00

I have a problem with my code. There is a Fibonacci’s function which I

  • 0

I have a problem with my code. There is a Fibonacci’s function which I hope you know what does. And there two files: In0201.txt and Out0201.txt. As well, the programme should get the value from file “In0201.txt” and write the results to Out0201.txt.

Some value is being written but instead writing sequence of numbers ( to file ), it writes a value like it was a sum of all this numbers from sequence. Does anybody know why it happens ?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//Fibonacci
long double fib(int n) {
    if(n == 0)
    {
        return 0;
    }

    if(n == 1)
    {
        return 1;
    }

    return fib(n-1) + fib(n-2);
}

int main()
{
    int a;
    int tmp;

    ifstream inputFile("In0201.txt");
    if (inputFile.is_open()) {
        inputFile >> a;
        cout << "Loaded the value 'n' from file: " << endl;
        cout << a << " " << endl;
        inputFile.close();
    }

    ofstream outputFile("Out0201.txt");
    if (outputFile.is_open()) {
        tmp = fib(a);
        cout << "Fibonacci's sequence number: " << tmp << endl;
        outputFile << tmp << ", ";
        outputFile.close();
    }
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:21:12+00:00Added an answer on June 14, 2026 at 2:21 am
    if (outputFile.is_open()) {
        tmp = fib(a);
        cout << "Fibonacci's sequence number: " << tmp << endl;
        outputFile << tmp << ", ";
        outputFile.close();
    }
    

    This code will output to the file a single integer number followed by a comma. If you want to output each of the return values from fib(int n) then you’ll need to restructure your code so that the string you wish to write to file is appended inside the recursive loop.

    Solution

     long double fib(int n, ofstream &openFile) {
         if(n == 0)
         {
             return 0;
         }
    
         if(n == 1)
         {
             openFile<<1<<", ";
             return 1;
         }
         ofstream dummyStream;
         long double nextFib = fib(n-1, openFile) + fib(n-2, dummyStream);
         openFile<< nextFib <<", ";
         return nextFib;
     }
    
    
    int main()
    {
        int a;
    
        ifstream inputFile("In0201.txt");
        if (inputFile.is_open()) {
            inputFile >> a;
            cout << "Loaded the value 'n' from file: " << endl;
            cout << a << " " << endl;
            inputFile.close();
        }
    
        ofstream outputFile("Out0201.txt");
        if (outputFile.is_open()) {
            outputFile << 0 << ", ";
            fib(a, outputFile);
            outputFile.close();
        }
        return 0;
    }
    

    The purpose of dummyString is to disregard half of the results since they are being duplicated by calling fib twice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any solution to overcome case-sensitive problem for contains method. I have code
I have a code problem which stems from the fact that I am using
I have one little problem...Here is my code..Is there a way to distribute weights
i have a problem with this code: var par = []; $('a[name]').each(function() { if
I have a bizarre problem: Somewhere in my HTML/PHP code there's a hidden, invisible
In my code there is a structure which have padding issues. I fixed them
I have problem. I don't know what is the code to call Matlab by
Today, i have very weird problem with trim function. Here is my code. =and
  There is some problem code in a website I don't have source access
In my C++-code there are several fprintf-statements, which I have used for debugging. Since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.