0

I'm working on a problem which calls for masking out certain parts of the digits being read in. My Code is below. I got the problem correct, but I couldn't shake the feeling that there was a better way to deal with the last mask (noted in comments as problematic).

#include <iostream>
#include <iomanip>
using namespace std;
#include <conio.h>

struct phone_num
{
int a_code;
int prefix;
int postfix;
};


int main(int argc, char* argv)
{
	
	phone_num num1={111, 222, 3333};
	phone_num num2;
	char ch='a';
	cout << setprecision(10);
	cout << "Enter phone number:";
	double total=0;
	double temp=0;
	do
	{
	ch=getche();
	if( (ch-48) >= 0 && (ch-48) <= 9)
		total=total*10+(ch-48);
	}while( ch != '\r');

	temp = static_cast<int>(total/10000);  // sets temp variable to mask out first 7 digits
	num2.postfix = (total-(temp*10000)); 
	
	temp = static_cast<int>(total/10000000);  // masks last 7 digits to get area code
	num2.a_code = temp;
	
	
	temp = (total - (static_cast<double>(num2.a_code)*10000000));  // problematic part masks last 4 and first 3 digits leaving prefix
	system("cls");
		num2.prefix = static_cast<int>(temp/10000);
	
	cout << "(" << num2.a_code << ")" << num2.prefix << "-" << num2.postfix << endl;
	system("PAUSE");
return 0;
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Ancient Dragon
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.