My assignment is this.
The purpose of this assignment is to reinforce the concepts types, I/O, and the process of decomposing
problems into smaller units that can be implemented as functions and tested independently. Your program should begin by asking the user if they want to find the longest sequence in a range of
numbers, asking the user for their name, then asking the user for a positive integer number (n). Your program should then generate n sequences of numbers (one for every value between 1 and n, inclusive)
based on the following two rules. Starting with a number k, the next number in the sequence is k/2 if k is even; otherwise it is (3*k)+1. The sequence continues using these rules until a number less than
or equal to 5 is generated. When a number less than or equal to 5 is generated, the sequence ends
without counting that number as part of the sequence length. Therefore any number entered that has
a value of 5 or less has a sequence length of 0. For example, the sequence starting at 17 is
17 52 26 13 40 20 10 5
and its length is 7.
Your program should keep track of the length of each sequence generated and output initial number
of the longest sequence generated as well as its length. The program should repeat this behavior
(requesting if the user wants to find a sequence, requesting a name, computing the longest sequence)
until the user enters n or N for the first question.
You may notice that depending upon the starting number of the sequence, the length of the sequence can vary dramatically. For example, a sample run of your program may look like the following:
Would you like to find the longest sequence of a range of numbers? (Y or N) y
Please enter your name: Scott
Please enter value: 10
Scott entered value: 10
The initial number with the longest sequence is: 9
The sequence length for 9 is: 14
Would you like to find the longest sequence of a range of numbers? (Y or N)N
Thank you.
We get this response because the sequence starting with 9 is: 9 28 14 7 22 11 34 17 52 26 13 40 20 10 and it is longer than the sequences that start with all the other numbers between 1 and 10.
Here is another sample run:
Would you like to find the longest sequence of a range of numbers? (Y or N)y
Please enter your name: Scott
Please enter value: 1000
Scott entered value: 1000
The value with the longest sequence is: 871
The sequence length for 871 is: 173
Observe that you should not print out any of the sequences. Furthermore your program should accept the input exactly as described here.
To solve this problem we want you to use a particular set of functions. In this way you'll gain experience
with functions and the process of decomposing a problem into smaller parts that can be implemented with functions. These are the functions you must implement:
1. PromptUserToFindLongestSequence - This function asks the user if they want to find a longest sequence. The only valid input is Y, y, N, and n If any other character is entered, the user should
be informed that the input is invalid, and then re-prompted to enter their decision. This function returns the information indicating whether the user wishes to continue or not.
2. GetUserName - This function prompts the user for their name and returns this string value back to the calling program. The name cannot contain spaces.
3. GetUserInputValue - This function prompts the user for a positive integer number and validate it. The function repeatedly prompts the user for a positive integer number until one is entered.The function returns this value.
4. NextValueInSequence - This function takes a number as a parameter and returns the next number in the sequence using the rules given above.
5. SequenceLength -This function takes a positive number as a parameter and returns the length of the sequence starting with this number.
6. NumberWithLongestSequenceLength - This function takes a positive number n and returns the number between 1 and n that starts the sequence with longest length.
7. DisplayAndRecordResults - This function displays the initial number (n), the number which has the longest sequence length between 1 and n, and the length of the longest sequence found. This function also creates a file results.txt that stores the person's name on the first line,
the initial number n on the second line, the number with the longest sequence on the third line,
and finally the sequence length of that number on the fourth line. (NOTE: if the user runs this
multiple times, the file must contain only the data from the last run.)
***************************************************
so far I have written this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string getUserName () {
string name;
cout << "Please enter your name: ";
cin >> name;
return name;
}
bool promptUserToFindLongestSequence () {
char userInput;
cout << "Would you like to find the longest sequence of a range of numbers? (Y or N)" << endl;
cin >> userInput;
if (userInput == 'y' || 'Y')
return false;
if (userInput == 'n' || 'N')
return true;
}
int main () {
char userAnswer;
userAnswer = promptUserToFindLongestSequence ();
if (userAnswer = true)
getUserName ();
if (userAnswer = false)
cout << "Thank you." << endl;
return 0;
}
|
The only things I care about for now are, promptUserToFindLongestSequence and getUserName.
now my problem is that no matter what I type in for promptUserToFindLongestSequence, it still asks me to input a name where it should just say "thank you" and the program should terminate.