C++ 在程序中添加区号
基本上,我在一次作业中得到了这个程序。它要求输入任何7位数的电话号码,如930-1892。然后它会把你的数字作为输出。 现在,我决定将区号添加到7位数字中,总计10位。我希望它以(505)123-4567这样的格式输出 令人惊讶的是,我无法让程序输出包括区号在内的10位数字,而不是7位数字。 但是现在的程序应该编译、运行并输出您添加到其中的7位电话号码C++ 在程序中添加区号,c++,arrays,phone-number,C++,Arrays,Phone Number,基本上,我在一次作业中得到了这个程序。它要求输入任何7位数的电话号码,如930-1892。然后它会把你的数字作为输出。 现在,我决定将区号添加到7位数字中,总计10位。我希望它以(505)123-4567这样的格式输出 令人惊讶的是,我无法让程序输出包括区号在内的10位数字,而不是7位数字。 但是现在的程序应该编译、运行并输出您添加到其中的7位电话号码 #include<iostream> #include<string> using namespace std; /*
#include<iostream>
#include<string>
using namespace std;
/* Global Variables */
int prefix[3];
int number[4];
string phoneNumber;
// main routine
int main()
{
// local variables
int input_done = 0; // flag to control input loop
int ascii_0 = 48;
int i = 0;
while (input_done == 0)
{
// Ask the user for input
cout << "Please enter your 7 digit phone number (for example: 123-4567)" << endl;
getline(cin, phoneNumber);
// split up the input and check it for validity
if (phoneNumber.length()==8)
{
input_done = 1; // assume number is correct until otherwise
for (i = 0; i <= 7; i++)
{
if (i == 3)
{
if (phoneNumber[3] != '-')
input_done = 0;
}
else
{
if ( (phoneNumber[i] < '0') || (phoneNumber[i] > '9') )
input_done = 0;
}
}
// assign values to individual array elements if checked out okay
if (input_done == 1)
{
for (i = 0; i <= 2; i++)
{
prefix[i] = phoneNumber[i] - ascii_0;
number[i] = phoneNumber[i+4] - ascii_0;
}
number[3] = phoneNumber[7] - ascii_0;
}
}
if (input_done != 1)
cout << "There is a problem with what you entered, please try again.\n";
}
// report
cout << "I have your phone number as: ";
for (i = 0; i<3; i++)
cout << prefix[i];
cout << "-";
for (i = 0; i<4; i++)
cout << number[i];
cout << endl;
return 0;
}
#包括
#包括
使用名称空间std;
/*全局变量*/
int前缀[3];
整数[4];
字符串电话号码;
//主要程序
int main()
{
//局部变量
int input_done=0;//控制输入循环的标志
int ascii_0=48;
int i=0;
while(输入_done==0)
{
//请求用户输入
cout首先,您所采用的方法存在一些小问题。首先,您使用了很多神奇的数字,这使得您的代码很难维护和发展,因为您必须担心更新更多的代码会增加引入错误的机会。第二,没有必要将电话号码转换为单独的数字呃,从长远来看,这只会使值更难管理。您还希望连字符位于特定位置,再次使用幻数,这将使添加区号支持更加困难。以下方法消除了幻数的使用以及索引和数组的使用,并充分利用了它埃拉托斯
#include <iostream>
#include <string>
using namespace std;
// main routine
int main()
{
// local variables
bool isValidNumber = false;
string phoneNumber;
while (isValidNumber == false)
{
// Ask the user for input
cout << "Please enter your 10 digit phone number (i.e. 900-976-8008)" << endl;
std::getline(cin, phoneNumber);
isValidNumber = true;
// Validate and clean up the phone number
for (std::string::iterator it = phoneNumber.begin(); it != phoneNumber.end();)
{
// Check for characters we want to ignore
if (*it == '(' || *it == ')' || *it == '-' || *it == ' ')
{
it = phoneNumber.erase(it);
}
// Check for numbers since we really want to keep them
else if (*it >= '0' || *it <= '9')
{
++it;
}
// Check characters that are considered invalid
else
{
isValidNumber = false;
}
}
// Make sure the number of required digits are present. Add an additional
// check for numbers without area codes if you like.
if (phoneNumber.size() != 10)
{
isValidNumber = false;
}
if (isValidNumber == false)
{
cout << "There is a problem with what you entered, please try again.\n";
}
}
// Split the number and print it
std::string areacode;
std::string prefix;
std::string number;
areacode = phoneNumber.substr(0, 3);
prefix = phoneNumber.substr(3, 3);
number = phoneNumber.substr(7, 4);
std::cout
<< "I have your phone number as: ("
<< areacode
<< ") "
<< prefix
<< '-'
<< number
<< '\n';
return 0;
}
#包括
#包括
使用名称空间std;
//主要程序
int main()
{
//局部变量
bool isValidNumber=false;
字符串电话号码;
while(isValidNumber==false)
{
//请求用户输入
你的问题是什么?什么不起作用,以什么方式?它本身工作正常,但我想添加区号。现在它只输出7位数的电话号码,如123-4567。但我想让它输出10位数的长度,如(505)123-4567.但如果不弄乱程序,我无法做到这一点。程序应该很容易修改。为区号添加一个3字符的数据。提取区号,类似于前缀。将第一个“for”循环终止符从7更改为10。在这个代码块中:for(I=0;I ahh我明白了,好的,我现在明白了。谢谢!