• 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

Duckers

Platinum Member
This might not be the right forum to ask this.... 😉

I found this problem:

Write a program that displays each of the digits on a separate line. The program output should look similar to:

Enter an integer: 56372
5
6
3
7
2

I haven't been able to figure this one out!
Thanks!, btw, what's a good programming message board?
 
Without remembering any C++ my guess would be to change the integer into a string and then just go through the string one char at a time, with a newline for each item in the string until the string length is used up.
 
thanks 🙂
can anyone provide some code 🙂

I was attempting to do somethign similar to this but I couldn't get it to work:

#include <iostream.h>
int main ()
{
int integer;
cout<<&quot;Enter an integer: &quot;;
cin>>integer;


for (int x=(integer /10); x<= (integer%10); x++)
cout<<x<<endl;

return (0);
}

 
I can't remember any syntax for C++...

WHat you'll need to do is actually cast or type-cast (whichever it is technically) the integer into a string. It should be somewhere in the book you're probabaly reading. There will be a function or method in C++ that allows you to do this directly and its not hard, then once its a string you need to go something like


for (int x, x<string.length, x++)
cout<<[stringposition.x]
cout<<[newline]


How is that for some code that won't work (kinda remembering from looking at yours hehe) - but thats the idea! This steps through each character in the string in order and outputs that string, then a newline, until its done with. There will also be a function in your book which allows you to pass in a string variable and another parameter, like x, where x is the position of the string, then passing in the variable and that position number the function will return what that particular position is...ie:


blah = &quot;Hi there&quot;
blah_position = StringPosition(blah, 4)


And in this case blah_position now = h (since the string probabaly starts at an index of 0 as opposed to 1, thus the 4 is actually the 5th character in the string.

Going to bed now!
 
_itoa would work, but it's a Win95/NT specific function

char *_itoa( int value, char *string, int radix )

value is obviously the number, string is the string result, radix is the base (e.g. if you wanted it in normal numbers, it would be 10, for binary 2, etc etc). It's return value is also the string.

Required header: stdlib.h
Libraries: libc.lib, libcmt.lib, msvcrt.lib

If you using Microsoft Visual C++ and your teacher doesn't care (I'm guessing this is for a class), use this. Otherwise, you can do it with the modus operator (%)...I'll quickly whip up that code and post it up.


BTW, this would belong in the software forum, but I think you'd have better luck here.
 
This was sloppy, but i did it in 2 mins and it works. Yes, there will be a few problems (e.g. if a digit is 0 it won't get printed), and it can be done much neater, but I didn't feel like spending time optimizing it.


---------------------------

#include <stdio.h>
#include <iostream.h>

void main(){
int number;
int remainder, int quotient;

cin >> number;
// Can handle numbers < 10 million
if (number > 999999) {
quotient = number/1000000;
remainder = number%1000000;
number = remainder;
printf(&quot;%d\n&quot;,quotient); }
if (number > 99999) {
quotient = number/100000;
remainder = number%100000;
number = remainder;
printf(&quot;%d\n&quot;,quotient); }

if (number > 9999) {
quotient = number/10000;
remainder = number%10000;
number = remainder;
printf(&quot;%d\n&quot;,quotient); }

if (number > 999) {
quotient = number/1000;
remainder = number%1000;
number = remainder;
printf(&quot;%d\n&quot;,quotient); }

if (number > 99) {
quotient = number/100;
remainder = number%100;
number = remainder;
printf(&quot;%d\n&quot;,quotient); }

if (number > 9) {
quotient = number/10;
remainder = number%10;
number = remainder;
printf(&quot;%d\n&quot;,quotient); }
printf(&quot;%d\n&quot;, number);

//to pause the program
cin >> number;


}
--------------------------
 
If you have any questions, feel free to ask. 🙂 I wish the forums would let you keep text formatting such as spaces and tabs...that code looks horrible right now.
 
I know i can do this real quick, but I am a new coder and i dont know how to use a class. How do i call a class such as the CString class?
 
At the top of your file, under or above your #include <iostream.h> put:

#include <classname.h>

OR if its a user defined class, use:

#include &quot;classname.h&quot;
 
Its weird, there is no CString.h file, however there is a CString file with no extension like .xxx, so any ideas?
 
I think this would work too...
string integer;
cout << &quot;Enter an integer:&quot;;
cin >> integer;
for (int i=0; i < integer.length(); i++)
cout << integer(i) << endl; // these parenthese should be brackets

Someone hit me if I'm off, I haven't done this for awhile
 
Sephy: That just looks like VB + C++ + Java all combined into one weird block of code. I'm not even going to try to judge that in right and wrong...
 
jmcoreymv: You need to include the header file...it's not usually named after the classname though. CString requires Afx.h
 
sephy: Actually, now that I look at it, I see what you're trying. Instead of integer.length() you'd need to use strlen(integer)
 

#include <iostream.h>
#include <iomanip.h>
using namespace std;
size_t strlen( const char s );

int main ()
{
char input [ 80 ];
cout << &quot;Welcome to program. Please enter your number: &quot;;
cin.getline( input, 80 );
for ( int counter = 0; counter <= strlen( input ); counter++ )
cout << input[counter] << &quot;\n&quot;;
return 0;
}



Try this one, it works. Make sure the number has less than 80 digits 🙂

 
Thanks a lot guys 🙂 I didn't expect all these replies.

Pretender, your program didnn't work 🙂
I am using Turbo C++ 4.5 so it might be on the program.

Yata, when I try to run your program I keep getting a message saying Cannot convert 'char*' to 'char' in function main() for the line for ( int counter = 0; counter <= strlen( input ); counter++ )

 
Back
Top