first of all, use the #include <stdio.h> instead of #include <cstdio> , next, you haven't declared a variable for n in main. Next, for your question, you have to call the function Hailstone and lengthHail with those numbers that your user inputted (which by the way you need to do twice). When doing so, do not include line 37
@Atrain
Since you are programming in C++, you should be using the C++ I/O facilities.
1 2 3 4 5 6 7 8
int main()
{
int n;
cout << "Enter a number: ";
cin >> n;
int length = lengthHail(n);
cout << "The Hailstone sequence is " << length << ".\n";
If you wish to also show the hailstone sequence, you should print each calculated value. You can do this by simply adding a cout statement somewhere (like between lines 13 and 14, or between lines 23 and 25).
Other possibilities are:
use a vector
1 2 3 4 5 6 7 8 9 10 11 12 13
vector<int> generate_hailstone_sequence(int n)
{
vector<int> seq;
do
{
seq.push_back(n);
n = Hailstone(n);
} while (n != 1);
return seq;
}
Conveniently, the vector knows its length, which is also the length of the generated Hailstone sequence.
without vectors (or arrays)
You can also avoid storing the sequence itself, but simply print it to the screen. The cleanest way to do this is to create another function that does it:
1 2 3 4 5 6 7 8 9 10
void print_hailstone_sequence(int n)
{
do
{
cout << n << " ";
n = Hailstone(n);
} while (n != 1);
cout << "\n";
}
Your main() can then use it:
1 2 3 4 5 6 7 8 9 10 11
int main()
{
int n;
cout << "Enter a number: ";
cin >> n;
cout << "The Hailstone sequence is: ";
print_hailstone_sequence(n);
int length = lengthHail(n);
cout << "The Hailstone sequence is " << length << " values long.\n";
Final words: notice that I made a change in the loop in your function -- the sequence terminates with 1, but that 1 should not be excluded from the sequence. Hence, lengthHail(1) should be 1, not 0.
No no, what I meant Duoas, was, as i forgot to mention in my previous post, is including stdio.h is better since you then don't need the std namespace and can terminate line 2.
I myself have been very explicit that usingnamespace std; should not be used in specific contexts, but otherwise that it is fine. It is true that there are members here who disagree with me. Nevertheless, if you really think you can disabuse me of hypocrisy concerning my true position, best put up with examples. Here're mine. (Good luck refuting them.)
Further, I am not the one who made an issue of usingnamespace std; here -- you did. You have argued that using #include <cstdio> requires that you also avoid usingnamespace std;, for unstated (and actually unrelated) reasons of efficiency, and you stated it as if something I said validated your assertion.
What about using namespace std. If you are correct, then that line should be removed and everything should be prefixed with std:: for efficiency.
I am correct, but that does not validate your assertion ("should").
And you're still trying to wiggle the words around, and claim, arrogantly, that you are smarter than the people who designed the language, because "by means that are better" (which you have consistently argued means #include <stdio.h> ) you can obviate usingnamespace std;.
Accept that you made a mistake and move on.
(Or, for more hilarity, keep trying...)
Circular reasoning (Latin: circulus in probando, "circle in proving"; also known as circular logic) is a logical fallacy in which the reasoner begins with what they are trying to end with. The components of a circular argument are often logically valid because if the premises are true, the conclusion must be true.