You are here:

C++/hailstone numbers

Advertisement


Question
i cant get my program to run correctly in C++, it only prints out two numbers of the sequence, then stops.




#include "stdafx.h"

#include <iostream.h>

void exercise23()
{
int iVal;
int compOne;
int SecNum;
cout<<endl;
cout<<"Enter a number that is greater than 1: ";
cin>>iVal;
cout<<endl;
compOne = iVal;
cout<<iVal<<", ";
do
{
if (compOne%2 != 0)
{
SecNum = ((compOne*3)+1);
cout<<SecNum<<", ";
compOne = SecNum;
}
if (compOne%2 == 0)
{
SecNum = compOne/2;
cout<<SecNum<<", ";
compOne = SecNum;
}
}while(compOne == 1);
}

int main(int argc, char* argv[])
{
exercise23();
return 0;
};  

Answer
Reid Bretherton , Thank you for your question.

I regret that I am unable to do debugging for people due to the volume of questions received. This kind of problem is usually easy to solve by stepping through the code with your debugger, looking at the values of the variables until you see something wrong. This should show you the exact line of the bug.

David Spector

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


David Spector

Expertise

Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.

Experience

I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Publications
Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

Education/Credentials
ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 About.com. All rights reserved.