#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//---Do not edit the following
//StartProvided
const int ERROR_CONVERSION = 1;
/*
Preconditions: Parameter is positive
Output stream is not in an error state
Postconditions: Ouput stream contains the lucas sequence up to given number of terms
Output stream is not in an error state
*/
void genLucasSequence(int intTerms);
/*
Preconditions: Parameter is a non-empty string
Postconditions: Boolean value indicating validity is returned
*/
bool isValidBinaryString(string strNumber);
/*
Preconditions: Parameter 2 and 3 are variable (non-literal, non const) integers
Postconditions: Parameter 2 and 3 are modified to contain appropriate boolean values
*/
void isPositiveAndEven(int intValue, bool& blnPositive, bool& blnEven);
/*
Preconditions: Parameter is positive
Postconditions: Vector containing terms of sequence is returned
*/
vector<int> genCollatzSequence(int intValue);
/*
Preconditions: Output stream is not in an error state
Postconditions: Converted value is returned if valid or program is terminated
*/
int promptForNumber();
int main()
{
bool blnContinue = true; //Loop control variable
do
{
while(cin.fail()) //Purge stream if needed
{
cin.clear();
string strJunk = "";
getline(cin, strJunk);
}
cout << "a) Lucas Sequence" << endl
<< "b) Validate binary string" << endl
<< "c) Test for positive and even" << endl
<< "d) (Bonus) Generate Collatz sequence" << endl
<< "e) Quit" << endl
<< "Enter selection: ";
char chChoice = '\0'; //Capture menu option
cin >> chChoice;
//Actual parameters for functions
string strBinaryNumber = "";
int intValue = 0;
bool blnPositive = false;
bool blnEven = false;
//Variable to hold return values
vector<int> vecSequence;
switch(chChoice) //Menu selection
{
case 'a':
case 'A':
intValue = promptForNumber(); //Prompt user
genLucasSequence(intValue); //Generate sequence
break;
case 'b':
case 'B':
cout << "Enter binary string: ";
getline(cin, strBinaryNumber); //Prompt user
if(isValidBinaryString(strBinaryNumber)) //Output based on result
cout << endl << "Number is a binary string" <<endl;
else
cout << endl << "Number is not a binary string" << endl;
break;
case 'c':
case 'C':
intValue = promptForNumber();
isPositiveAndEven(intValue, blnPositive, blnEven); //Pass by reference
if(blnPositive) //Output based on result
cout << "Number is positive" <<endl;
else
cout << "Number is not positive" << endl;
if(blnEven)
cout << "Number is even" <<endl;
else
cout << "Number is odd" << endl;
break;
case 'd':
case 'D':
{
intValue = promptForNumber(); //Prompt user
vecSequence = genCollatzSequence(intValue); //Generate sequence
cout << "Collatz Sequence: ";
for(int k = 0; k < vecSequence.size(); k++)
cout << vecSequence[k] << endl;
}
break;
case 'e':
case 'E':
blnContinue = false; //Quit by ending main loop
break;
default: //Inform user of invalid option being selected
cerr << "Invalid option, try again" << endl;
}
} while(blnContinue); //End of menu loop
return 0;
}
//An example of a value returning function implementation
int promptForNumber()
{
int intValue = 0;
cout << "Enter number: ";
cin >> intValue;
if(cin.fail())
{
cerr << "Invalid conversion" << endl;
exit(ERROR_CONVERSION);
}
return intValue;
}
//EndProvided
//--Edit after this point.
//Your code for the function implementations goes here
void genLucasSequence(int intTerms)
{
int a = -1;
int b = 2;
intTerms = 0;
cout << "Enter the number of terms" << endl;
cin >> intTerms;
for (int i=1; i<=intTerms; i=i+1)
{
int c = a;
a = b;
b = c + b;
}
cout << b << endl;
}
bool isValidBinaryString(string strNumber);
{ // this is where my error is.
strNumber = " ";
{
if(strNumber = "0") | (strNumber = "1")
return (strNumber true)
else
return (strNumber false)
}
}
void isPositiveAndEven(int intValue, bool& blnPositive, bool& blnEven);
{
intValue = 0;
cout << "Enter a number" << endl;
cin >> intValue;
blnPositive = intValue > 0;
blnEven = intValue / 2;
}
vector<int> genCollatzSequence(int intValue);
{
vector<int>vecSequence
vecSequence.push_back(intValue);
while(intValue>1)
{
if (intValue%2==0);
intValue = intValue / 2
else
intValue = intValue*3+1;
vecSequence.push_back(intValue);
}
return vecSequence;
}
bool OptionE(bool blnContinue)
{
return (blnContinue false)
}