Advertisement
Limited_Ice

Fibonacci code

Oct 13th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. // This is the Week 7 Quiz
  2.  
  3. // This program will tell you if a number between 1 and 100 are in the Fibonacci sequence.
  4.  
  5. // David Hawkins
  6.  
  7. //=============================================================================================//
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11.  
  12. using namespace std;
  13.  
  14. void fibonacci(int); // This function tests the integer you choose against the fibonacci sequence and outputs the results
  15. int numcheck(); // This function ensures that you you input an integer between 1 and 100
  16. void goagain(); // This function lets you test different numbers.
  17.  
  18. //----------------------------------------MAIN------------------------------------------------//
  19. int main()
  20. {
  21.     while (true)
  22.     {
  23.         int number;
  24.         cout << setw(80) << setfill('*') << '*' << endl;
  25.         cout << "This program will tell you whether or not an integer between 1 and\n100 is part of the Fibonacci Sequence.\n\n" << endl;
  26.         cout << "What is your number?: ";
  27.         number = numcheck();
  28.         fibonacci(number);
  29.         goagain();
  30.     }
  31. }
  32.  
  33. //------------------------------------numcheck----------------------------------------------//
  34. int numcheck()
  35. {
  36.     int numchoice;
  37.     int timer = 0; // I always use the "timer" variable to keep a program from looping endlessly.
  38.     cin >> numchoice;
  39.     cout << setw(80) << setfill('*') << '*' << endl;
  40.     while (0 > numchoice || 100 < numchoice)
  41.     {
  42.         timer += 1;
  43.         if (timer > 3)
  44.             exit(0);
  45.         else
  46.         {
  47.             cout << "Please choose a number between 0 and 100\nYou have " << 3 - timer << " attempts remaining\n\nWhat is your number?: ";
  48.             cin >> numchoice;
  49.             cout << setw(80) << setfill('*') << '*' << endl;
  50.         }
  51.     }
  52.     return numchoice;
  53. }
  54.  
  55. //---------------------------------fibonacci------------------------------------------------//
  56. void fibonacci(int numtest)
  57. {
  58.     int fibonum1 = 1; // This variable is tested again the number you chose to verify if it is in the fibonacci sequence
  59.     int fibonum2 = 0; // fibonum2 and fibonum3 are place holders to help move the fibonacci sequence forward.
  60.     int fibonum3 = 1;
  61.     int i;
  62.     while (fibonum1 <= 100) // This while loop makes sure that all number in the fibonacci sequence are accounted for and also that the program doesn't calculate the forever.
  63.     {
  64.         if (fibonum1 == numtest)
  65.         {
  66.             break;
  67.         }
  68.         else // The statements in this block of code are what move the variable "fibonum1" through the fibonacci sequence.
  69.         {
  70.             fibonum1 = fibonum2 + fibonum3;
  71.             fibonum2 = fibonum3;
  72.             fibonum3 = fibonum1;
  73.         }
  74.     }
  75.     if (fibonum1 == numtest)
  76.     {
  77.         cout << "Your number is in the Fibonacci sequence." << endl;
  78.         cout << setw(80) << setfill('*') << '*' << endl;
  79.     }
  80.     else
  81.     {
  82.         cout << "Your number is NOT within the Fibonacci sequence." << endl;
  83.         cout << setw(80) << setfill('*') << '*' << endl;
  84.     }
  85. }
  86.  
  87. //----------------------------goagain-------------------------------------------------//
  88. void goagain()
  89. {
  90.     char cont;
  91.     int timer = 1;
  92.     cout << "would you like to go again?\ny or n?: ";
  93.     cin >> cont;
  94.     cont = tolower(cont);
  95.     if (cont == 'y')
  96.         system("CLS");
  97.     else if (cont == 'n')
  98.         exit(0);
  99.     else
  100.     {
  101.         while (true)
  102.         {
  103.             cout << "USE CORRECT INPUTS\nyou have " << 3 - timer << " more attempts before exiting\nWould you like to go again?\nY or N?  ";
  104.             cin >> cont;
  105.             cont = tolower(cont);
  106.             cout << endl;
  107.             timer += 1;
  108.             if (timer > 3)
  109.                 exit(0);
  110.             else if ('y' == cont)
  111.             {
  112.                 system("CLS");
  113.                 break;
  114.             }
  115.             else if ('n' == cont)
  116.                 exit(0);
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement