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..
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)
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.
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.
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!!..