You are here:

# C++/Loop

Question
I wrote this code which tries to get the user to input 2 positive single digit numbers then the next number displayed is the sum of the first 2 numbers saving only the ones-digit.  This continues until the first 2 numbers come up again somewhere in the sequence.  Example: 1 8 9 7 6 3 9 2 1 3 4 7 1 8

Why doesn't my code work correctly?
(only body is included)

int first;
int second;
cout<<"Enter first number: \n";
cin>>first;
cout<<"Enter second number: \n";
cin>>second;
int x = first;
int y = second;
int s;
cout<<first<<" "<<second<<" ";

do
{
s = (x + y)%10;
x = s-x;
if (x<0)
{
x = -1*x;
}
y = s;
cout<<s<<" ";

} while ((x !=first) || (y!=second));

Well I only had a quick look but it seems to me that your implementation of the algorithm is incorrect, and in fact more complex than your description and example imply.

I think in your loop you should substitute for x and y as follows:

Next x = y and next y = s

x = 1
y = 8

s = 9

then:

x = y = 8
y = s = 9

s = 7

then:

x = y = 9
y = s = 7

s = 6

then:

x = y = 7
y = s = 6

s = 3

etc.

If you took the time to work out the process before you coded it up or if you sat down with a pencil and paper and ran the numbers through your code (or do it in your head) you would also have seen the problems.
Questioner's Rating
 Rating(1-10) Knowledgeability = 10 Clarity of Response = 10 Politeness = 10 Comment No Comment

C++

Volunteer

#### Ralph McArdell

##### Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

##### Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials