-
C++ problem
How can I write a program that when I input a five-digit number, it'll help me to separates the number into its individual digits and prints the digits separated from one another by three spaces each by using the integer division and modulus operators? thx!~
-
this will get you started. I'm not sure what you wanted with the modulas division. But if you want to operate on these values you'll have to use the atoi c runtime function.
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
char szNum[6];
cout << "Please enter a 5 digit number and <enter> : ";
cin >> szNum;
for( int i = 0; i < 5;i++ )
{
cout << szNum[ i ] << " ";
}
cout << endl;
return 0;
} // end
-
For error detection I would write code to ensure that all the digits are initilized into the array. Example if input is 123 the char array should be filled with 00123\n instead of 123/n(random value) (random value).