Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is the Week 7 Quiz
- // This program will tell you if a number between 1 and 100 are in the Fibonacci sequence.
- // David Hawkins
- //=============================================================================================//
- #include <iostream>
- #include <iomanip>
- using namespace std;
- void fibonacci(int); // This function tests the integer you choose against the fibonacci sequence and outputs the results
- int numcheck(); // This function ensures that you you input an integer between 1 and 100
- void goagain(); // This function lets you test different numbers.
- //----------------------------------------MAIN------------------------------------------------//
- int main()
- {
- while (true)
- {
- int number;
- cout << setw(80) << setfill('*') << '*' << endl;
- cout << "This program will tell you whether or not an integer between 1 and\n100 is part of the Fibonacci Sequence.\n\n" << endl;
- cout << "What is your number?: ";
- number = numcheck();
- fibonacci(number);
- goagain();
- }
- }
- //------------------------------------numcheck----------------------------------------------//
- int numcheck()
- {
- int numchoice;
- int timer = 0; // I always use the "timer" variable to keep a program from looping endlessly.
- cin >> numchoice;
- cout << setw(80) << setfill('*') << '*' << endl;
- while (0 > numchoice || 100 < numchoice)
- {
- timer += 1;
- if (timer > 3)
- exit(0);
- else
- {
- cout << "Please choose a number between 0 and 100\nYou have " << 3 - timer << " attempts remaining\n\nWhat is your number?: ";
- cin >> numchoice;
- cout << setw(80) << setfill('*') << '*' << endl;
- }
- }
- return numchoice;
- }
- //---------------------------------fibonacci------------------------------------------------//
- void fibonacci(int numtest)
- {
- int fibonum1 = 1; // This variable is tested again the number you chose to verify if it is in the fibonacci sequence
- int fibonum2 = 0; // fibonum2 and fibonum3 are place holders to help move the fibonacci sequence forward.
- int fibonum3 = 1;
- int i;
- 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.
- {
- if (fibonum1 == numtest)
- {
- break;
- }
- else // The statements in this block of code are what move the variable "fibonum1" through the fibonacci sequence.
- {
- fibonum1 = fibonum2 + fibonum3;
- fibonum2 = fibonum3;
- fibonum3 = fibonum1;
- }
- }
- if (fibonum1 == numtest)
- {
- cout << "Your number is in the Fibonacci sequence." << endl;
- cout << setw(80) << setfill('*') << '*' << endl;
- }
- else
- {
- cout << "Your number is NOT within the Fibonacci sequence." << endl;
- cout << setw(80) << setfill('*') << '*' << endl;
- }
- }
- //----------------------------goagain-------------------------------------------------//
- void goagain()
- {
- char cont;
- int timer = 1;
- cout << "would you like to go again?\ny or n?: ";
- cin >> cont;
- cont = tolower(cont);
- if (cont == 'y')
- system("CLS");
- else if (cont == 'n')
- exit(0);
- else
- {
- while (true)
- {
- cout << "USE CORRECT INPUTS\nyou have " << 3 - timer << " more attempts before exiting\nWould you like to go again?\nY or N? ";
- cin >> cont;
- cont = tolower(cont);
- cout << endl;
- timer += 1;
- if (timer > 3)
- exit(0);
- else if ('y' == cont)
- {
- system("CLS");
- break;
- }
- else if ('n' == cont)
- exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement