basic C++ question

Duckers

Platinum Member
Mar 30, 2000
2,089
1
0
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?
 

StageLeft

No Lifer
Sep 29, 2000
70,150
5
0
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.
 

Duckers

Platinum Member
Mar 30, 2000
2,089
1
0
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);
}

 

StageLeft

No Lifer
Sep 29, 2000
70,150
5
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!
 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
Try using the CString class with the Left() and Mid() functions to pull them out.
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
_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.
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
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;


}
--------------------------
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
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.
 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
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?
 
Apr 5, 2000
13,256
1
0
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;
 

jmcoreymv

Diamond Member
Oct 9, 1999
4,264
0
0
Its weird, there is no CString.h file, however there is a CString file with no extension like .xxx, so any ideas?
 

Sephy

Platinum Member
Dec 21, 1999
2,035
0
0
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
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
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...
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
jmcoreymv: You need to include the header file...it's not usually named after the classname though. CString requires Afx.h
 

Pretender

Banned
Mar 14, 2000
7,192
0
0
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)
 

yata

Senior member
Jun 2, 2000
746
0
0

#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 :)

 

Duckers

Platinum Member
Mar 30, 2000
2,089
1
0
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++ )