Can someone help me with digit extraction. You have to define a variable to store user input, including appropriate comments. Prompt the user for a number and use cin to place the value in the variable you defined in the preceding step and then display each digit seperately.
Enter Five Digit number: 12345
Ones digit is: 5
Tens digit is: 4
Hundreds digit is: 3
Thousands digit is: 2
Ten-thousands digit is: 1
Press any key to continue
This implementation will allow you to enter a variable number of digits, I've shown you the code to get 5 digits working, you can expand on the code to make it variable by playing around with the number of digits entered variable....:
#include <iostream>
#include <string>
#include <vector>
#include <conio.h> //for _getch();
#include <sstream>
usingnamespace std;
int main() {
string number_string;
int no_digits;
cout << "Please enter a number: ";
cin >> number_string;
//number of digits = the length of the string entered
no_digits = number_string.length();
if (no_digits == 5) { //for instance the length is 5 as in your example
cout << "The ones digit is: " << number_string[4] << endl;
cout << "The tens digit is: " << number_string[3] << endl;
cout << "The hundreds digit is: " << number_string[2] << endl;
cout << "The thousands digit is: " << number_string[1] << endl;
cout << "The ten-thousands digit is: " << number_string[0] << endl;
}
cout << "\nPress any key to continue..."; //number of digits = the length of the string entered
_getch();
return 0;
}
Hint use stringstream to type cast string -> integer. It's in the tutorial on this site if you don't know what it is yet. .
Louisa pretty much gave you what you need to know...
You can extract all numbers (as long as you know their length) with modeulus operator and division together..
so ex. "2534"
2534/1000 = 2.
2534%1000 = 534;
534/100 = 5;
534%100 = 34;
34/10 = 3;
34%10 = 4
4/1 = 4;
this works with any number ofcourse just change the division and mod from 1000 to whatever you are working with. You mentioned 5 digits so just move it to 10000. and work down from there.
EDIT: The division works as long as its all in int btw because that does the favor of discarding the remainder
Hi vlad61; This is an intro to programming and the code has to be written like a beginner, can you take a look at my code and tell me if I have it correctly. It does work but it doesn't display the words
Screen should output this:
Enter Five Digit number: 12345
Ones digit is: 5
Tens digit is: 4
Hundreds digit is: 3
Thousands digit is: 2
Ten-thousands digit is: 1
Press any key to continue
My code:
#include <iostream> // required to perform C++ stream I/O
using namespace std; // for accessing C++ Standard Library members
// function main begins program execution
int main () {
int num ;
int rem ;
cout << " Input a five-digit number : ";
cin >> num;;
for (int i = 0; i < 5; i++) {
rem = num %10;
num = num / 10;
cout << rem << endl;
}
return 0; // indicate that program ended successfully
#include <iostream> // required to perform C++ stream I/O
#include <string>
usingnamespace std; // for accessing C++ Standard Library members
// function main begins program execution
int main () {
int num, rem;
string decimal_place = "TEST";
cout << " Input a five-digit number : ";
cin >> num;
cin.ignore();
for (int i = 0; i < 5; i++)
{
rem = num %10;
num = num /10;
if(i == 0)
decimal_place = "Ones";
elseif(i == 1)
decimal_place = "Tens";
elseif(i == 2)
decimal_place = "Hundreds";
elseif(i == 3)
decimal_place = "Thousands";
elseif(i == 4)
decimal_place = "Tens Thounsand";
cout<<decimal_place<<" digit is: "<< rem << endl;
}
cin.ignore();
return 0; // indicate that program ended successfully
} // end function main
Why not just put an if statement? I mean its not as clean i guess but it works fine for this
BTW: I like your little loop for seperating numbers its much more simplified than mine :)
Okay, does it actually need to print out words, i.e. "Ones digit is: 5", or can it print numbers like "1's digit is: 5", because that would be much easier for you.
I've changed and/or added to your code at line 2 and lines 19-23.
#include <iostream> // required to perform C++ stream I/O
#include <math.h> //used for the pow function (and other math things like sin, arctan, and log)
usingnamespace std; // for accessing C++ Standard Library members
// function main begins program execution
int main () {
int num ;
int rem ;
cout << " Input a five-digit number : ";
cin >> num;;
for (int i = 0; i < 5; i++) {
rem = num %10;
num = num / 10;
cout << pow(10, i) << "'s digit is: " << rem << endl;
/* ^^^^^^^^^^ This is the number place we are in, and this function is read as 10i
* Note: we are using the for-loop's index i
* (This pow function comes from the <math.h> header file)
*/
}
return 0; // indicate that program ended successfully
} // end function main