HOW TO DISPLAY OUTPUT IN VERTICAL IN C++ PROGRAMMING

Hi guys! Good day! I'm a Begginer of C Programming.. Well i know a little but one time that they give a laboratory exercise.. i can get it.. can you help me displaying the program in vertical..

HERE'S THE PROBLEM:

Write a program that prompt the user to input four-digit positive integer. The Program then outputs the digits of the number, one digit per line.

FOR EXAMPLE:

if input is 3245,
the output is:
3
2
4
5

Well i have a program for that but it only displays in horizontal when i compile & run the program in TURBO C++.

MY PROGRAM:

#include <stdio.h>

int main()
{
int a;
clrscr();

printf("Enter four-digit integers:\n");
scanf("%d" &a); //THIS IS MY PROBLEM!
printf("\nThe numbers are:%d\n%d\n%d\n%d", a);

getch();
return 0;
}




MY OUPUT:

Enter four-digit integers:
The numbers are: >> THE OUTPUT BUT STILL IN HORIZONTAL!!


That's my PROGRAM BUT I NEED SOME HELP! ABOUT THIS RUNNING THE PROGRAM IN VERTICAL..

THANK YOU!

Are you learning c or c++? If you're learning c++, which is what this forum is about, then don't use scanf and printf, use cin and cout on a string variable as shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main()
{
    std::string str; // declares a string variable
    std::cin >> str; // takes input and stores it in the variable str
    std::cout << str << "\n"; // outputs str and then a new line
    std::cout << "press enter to exit...\n";
    std::cin.ignore(256,'\n');
    std::cin.get(); // pauses the console
    return 0;
}


to get one character from the string, use str.at(whatever number character you want)
give him the c codes.. see clrscr(); there's no clrscr in c++ :P
He did say he was using C, not C++.

Also, no one answered his question.


There is no function in C (or C++) to split a number into pieces.

Remember, a number is an atomic thing -- unlike the textual representation of a number, which is composed of a list of things (characters -- usually digits).

Hence, the string "123" is not the same as the integer 123.

The scanf() function turns a string (whatever the user enters) into an integer.
The printf() function turns an integer into a string.

Your assignment is to take an atomic integer (your variable 'a') and split it into digits.
Remember your math classes, and use division and remainder (/ and %) to get the pieces of your number. After you get the pieces, you can printf() them.

Unless you are learning to use recursion, I would stick the pieces into an array. Your array doesn't need to be longer than twenty ints long: int digits[ 20 ]; (That will accomodate a 64 bit integer.)

Remember also that you can only split off the least significant digit at a time, meaning that if you print them in the order you get them then your results will be backwards. Part of the assignment is to print them in the correct order.

Hope this helps.
give him the c codes.. see clrscr(); there's no clrscr in c++ :P


To my knowledge, there's no clrscr in C, either.

He did say he was using C, not C++.


His subject said C++ though. ;P

</unhelpful>
Hmm, it seems I did not read the title of his post...
/me runs and hides


His code is pure C though...
Here it's my idea..in C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

long int x,q;
int nr=0;
long int p=1;
int par;

int main()
{
    cout<<"The number is..?"<<endl;
    cin>>x;
    q=x;
    while (x!=0)
    {
        nr=nr+1;
        x=x/10;
    }
    for (int i=1;i<nr;i++)
    {
        p=p*10;
    }
    while (q!=0)
    {
        par=q/p;
        q=q%p;
        p=p/10;
        cout<<par<<endl;
    }

    return 0;
}

Nice solution, but it is beyond the OP's current level, and it is just giving him an answer that he doesn't really understand. Let him work out his own homework.
here's my simple solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
 int a,b,c,d;

 cout << "Enter Four Digit positive number: ";
 a = getch();
 cout << char(a);
 b = getch();
 cout << char(b);
 c = getch();
 cout << char(c);
 d = getch();
 cout << char(d);

 cout << "\nOutput:\n";
 cout << char(a) << "\n";
 cout << char(b) << "\n";
 cout << char(c) << "\n";
 cout << char(d) << "\n";

 return 0;
}


http://codewall.blogspot.com
Last edited on
Thank you guys for sharing nice ideas! from now i have a reference . . to do this laboratory exercise I'm last to pass this exercise.. its just my cousin tell me about this website.. and im totally welcomed! Thank you GUYS!!

sorry for c & c++, my instructor said that we used c or c++.. my question and body is confusing.. LOL.. but THANK YOU GUYS!!..
Last edited on
In C++ (c will follow);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream>
using namespace std;

int main()
{
    cout << "give a number ffs: ";
    string number;
    cin >> number;
    cout << number[0] << endl;
    cout << number[1] << endl;
    cout << number[2] << endl;
    cout << number[3] << endl;
    cin.get();
    cin.get();
    cin.get();
    cin.get();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include <stdio.h>

int main(){
       printf ("Give a number ffs: ");
       printf("\n%c", getchar());
       printf("\n%c", getchar());
       printf("\n%c", getchar());
       printf("\n%c", getchar());
       return 0;
       }
       

A contest of terseness huh? Perl wins the day!
1
2
#!/usr/bin/perl -n
print join "\n", split //, $_; 

$ ~/code/vertical.pl
34324234
3
4
3
2
4
2
3
4
You can make it shorter by embedding the one-liner code as part of the -e argument for perl executable. No need any program.pl script at all.

E.g

cat <file> | perl -n -e '<one-liner code here>'

thanks all for sharing!



________________
http://tandaiduong.com.vn
Last edited on
thank you guys for the codes!

Really works!
Topic archived. No new replies allowed.