在程序中添加区号
Adding Area code in program
基本上,我得到了这个程序来完成一项作业。它要求提供任何 7 位数字长的电话号码,例如 930-1892。然后它会说你的数字作为输出。现在,我决定将区号添加到 7 位数字中,总共最多 10 位数字。我希望它以 (505)123-4567 这样的格式输出。
我出人意料地挣扎着,我无法让程序输出包括区号在内的 10 位数字,而不仅仅是 7 位数字。但是现在的程序应该编译并运行并输出您添加到其中的 7 位电话号码。
#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;
}
首先,您采用的方法存在一些小问题。首先,您使用了很多幻数,这使得您的代码难以维护和发展,因为您必须担心更新更多代码会增加引入错误的机会。其次,实际上没有必要将电话号码转换为单个号码,从长远来看,这只会使值更难以管理。您还希望连字符位于特定位置,再次使用幻数,这将使添加对区号的支持变得更加困难。以下方法消除了幻数的使用以及索引和数组的使用,并利用了迭代器。
#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;
}
我还建议您不要将using namespace std
语句放在命名空间范围内。这只是糟糕的形式,可能导致名称冲突。
相关文章:
- 尝试将带有值的不同变量添加到一小串程序C++中
- 为什么如果我添加这一行,我的程序会不断询问值
- 在没有管理员权限的情况下,在 c++ 中以编程方式将程序添加到启动
- 我正在尝试制作一个程序,在添加 n 天(整数)后告诉一个人什么是一天(例如星期一等)
- 如何在添加文件的情况下在VSCode中调试C++程序
- 在不添加进位的情况下添加数字的C++程序
- 如何将我的程序添加到文件和文件夹的macOS右键单击菜单?
- 需要循环帮助以迭代方式添加到程序集中的总和变量
- 如何修复此教科书程序以在C++中添加多项式?
- 如何在数据库程序中添加"reduce the quantity of an item"功能?
- 如何在控制台应用程序中添加滚动功能以显示更大的输入
- Wxwidgets - 如何添加调整大小事件处理程序以使"X"绘图随窗口调整大小?
- 如何将信号处理程序添加为方法
- 将 std::文件系统头文件添加到我的程序时出现编译错误
- 添加类型名会导致程序编译失败
- 为什么在此C++程序上添加负整数会导致正整数?
- 如何将 Bullet 物理库添加到 c++ 程序中
- 当您下载其他库并将其功能添加到C ++程序中时,您是否能够在另一台PC上运行您的软件(没有库)?
- 将编译器添加到程序中进行运行时编译的好方法是什么?
- Visual Studio在试图向MFC窗体上的控件添加处理程序时崩溃