• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

basic C++ question

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.
Duckers
I had to tweak my code too. It may be the different compiler I'm using. I'm using the newest Metrowerks Codewarrior. ANSI/ISO standard just came out recently too. Don't know how up to date your Turbo C++ is.

Shoot. Here, take out the "size_t..." function proto line. And add another #include:

#include <string.h>


try this.



 
Ok.. here we go.. this only taking in an integer (not a string) and not requiring anything else besides iostream.h. Depending on your compiler you may need to use &quot;#include <iostream.h>&quot; or change the namespace. Unchecked, should work, at worst .. err.. doesn't work. But the method is sound, and doesn't require anything special.

--

#include <iostream>

void recurseprint(int i)
{
if ( i == 0 )
return;
else {
recurseprint( i / 10 );
cout << i % 10 << endl;
}
}

int main()
{
int input;
cout << &quot;Enter an integer: &quot;;
cin >> input;
recurseprint(input);
return 0;
}
 
arcain;
your program works!
but it won't work with large numbers though!
I finally figured out how to get this to work; it took me several hours though 🙂
Here is the code I used:


#include <iostream.h>
int main()
{
long integer, digit;
long max = 1000000000;

cout<<&quot;Enter an integer: &quot;;
cin>>integer;

while (max > integer)
max = (max / 10);

while (max >=1){
digit = (integer / max);
cout<<digit<<endl;
integer = (integer % max);
max = (max / 10);
}
return (0);
}
 
The only reason I could see it failing with large numbers is if the inputted values overflow the integer. On modern 32 bit systems, an integer and a long are both 32 bits, so you can enter a number up to 2^32. If you enter something larger, I'm not sure how cin will handle that. Glad to hear you were able to work it out. It only get harder.. but luckily it gets easier with practice 🙂
 
Back
Top