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; } 
  • "a digit character cannot be leveled at a digit, according to VS logic" ... Have you ever seen a C ++ compiler with a different logic? Tell us, please. - AnT
  • Also, why do you even stick to the function ifstream::get . No, this function cannot read int as you are trying to do it - it is not intended for this. But how did this get even appear in your program? How did you suddenly start using this rare, highly specialized function? - AnT
  • @AnT Where constructive. If I were so smart, and if I knew that ifstream :: get function is not relevant here, I would not ask this question. Secondly, the question is no longer relevant as the comments above. What are you doing here, trying to help, or what, zero sense. - Vovenishe
  • You, apparently, do not understand something. The questions I asked above are not rhetorical. And the answers to them - this is a step towards "constructive". But you, it seems to me, do not want a "constructive", but that they do everything for you. Then my questions are really irrelevant. It happens ... - AnT
  • @AnT If I wanted to do everything for me, I would not write this relatively small program. Regarding the constructive, I do not need any puzzles, if you have answers to the questions, you can provide them. - Vovenishe

1 answer 1

If you intend to get the number from just one digit - let Prov be char , and after reading add

 Prov -= '0'; 

But if it should be a multi-digit number, then it’s better to work as

 int Prov; while (fin >> Prov)... 
  • Thank you Harry, Everything began to work, but it came out on the other hand, as it turned out I did not have the correct formula for identifying the Fibonacci number. I found something similar, it works correctly, but I think this is another language, I don’t know how to stick it in C ++, if I have any ideas, I’ll be happy. - Vovenishe