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
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.get(); // pauses the console
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.
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!!..