The program should get the numbers in a text file, and output the numbers that are Fibonacci numbers. Before the last, just a problem if, there are two cycles. It may seem that they are superfluous, but one is responsible for the list of the numbers themselves from the file, the second is responsible for comparing with each Fibonacci number, (in this case until the thirtieth) Again, the problem is in the condition of the cycle "while (fin.get (Prov) ) "when Prov was of type char there were no errors, but it did not work correctly, the digit symbol could not be leveled by the digit, according to VS logic. Now type Prov, replaced by int. !!!!!!!! Here is the formula described in the comments.
#include <iostream>
using namespace std;
int Fib (int i)
{
int value = 0;
if (i <1) return 0;
if (i == 1) return 1;
return Fib (i-1) + Fib (i - 2);
}
int main ()
{
int i = 0;
while (i <47)
{
cout << Fib (i) << endl;
i ++;
}
return 0;
}
Затем идёт код... (уже не так важен код как эта формула) Проблема в том, что бы эту Функцию воплотить в цикле, что бы было возможно проверить каждое число в файле. Мало кому это понадобится, хотя как видно мне пригодилось бы. #include "pch.h" #include "windows.h" #include "string.h" #include <iostream> #include "fstream" #include "locale.h" using namespace std; int main() { setlocale(0, ""); string chis = "MyProgFile.txt"; ofstream fout; fout.open(chis, ofstream::app); if (!fout.is_open()) { cout << "ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR"; } else { int i,j, chislo,Rozmer; cout << "Укажите количество чисел - "; cin >> Rozmer; cout << "Заполните текстовый файл"<<endl; for (i = 0;i < Rozmer;i++) { cin >> chislo; fout << chislo<<endl; } } ifstream fin; fin.open(chis); int x0 = 0, x1 = 1,j=0; if(!fin.is_open()) { cout << "ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR"; } else { cout << "Файл открыт"<<endl; int Prov; int MF[] = { 0,1 }; while(fin.get(Prov)) { while (j>30) { if (Prov == (MF[x0] + MF[x1])) { x0++; x1++; cout << Prov; } else cout << "ERROR ERROR ERROR"; } } } fin.close(); return 0; }
ifstream::get
. No, this function cannot readint
as you are trying to do it - it is not intended for this. But how did thisget
even appear in your program? How did you suddenly start using this rare, highly specialized function? - AnT