首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何实现c++电话号码格式:(000)-000-000?

如何实现c++电话号码格式:(000)-000-000?
EN

Stack Overflow用户
提问于 2017-02-19 03:47:10
回答 3查看 747关注 0票数 0
代码语言:javascript
运行
复制
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

std::wstring inputfield;

void function(uint32_t val)
{
    std::wstring keystring;
    keystring = std::to_wstring(val);    
    inputfield = inputfield + keystring; 
    std::wstringstream s;
    s << L"(" << inputfield << L") ";
    std::wstring str = s.str();
    std::wcout << str << "\n";
    return;
}

int main()
{
    uint32_t x ;
    while(cin>>x)
    {
        function(x);
        cout<< *(&x)<< endl;
    }
    return 0;
}

我正在尝试字符串格式,以获得类似(000)-000-000或美国电话号码格式的输出,但我无法实现,请帮助,谢谢

EN

回答 3

Stack Overflow用户

发布于 2017-02-19 04:17:12

如果您想更改字符串000000000或simillar (您确实这样做了,即使输入是数字,也只需使用std::to_string将其转换),那么只需在适当的位置添加几个字符即可。

代码语言:javascript
运行
复制
std::string format_number(std::string str) {
    if(str.size() != 9) {
        throw std::logic_error{"Phone number is not 9-characters long"};
    }

    str.insert(str.begin(), '(');
    str.insert(str.begin() + 4, ')');
    str.insert(str.begin() + 5, '-');
    str.insert(str.begin() + 9, '-');

    return str;
}
票数 3
EN

Stack Overflow用户

发布于 2017-02-19 04:23:33

你可以分配输入的电话号码,然后像这样传递结构;

代码语言:javascript
运行
复制
struct numbers {     
     int A;
     int B;
     int C;
} // then get (A)-B-C 
票数 0
EN

Stack Overflow用户

发布于 2020-05-19 18:02:10

代码语言:javascript
运行
复制
#include <iostream>
#include <thread>
#include <string>
//---- The following function will be invoked by the thread library
void thread_proc(std::string msg)
{
    std::cout << "ThreadProc msg:" << msg;
}
int main()
{
    // creates a new thread and execute thread_proc on it.
    std::thread t(thread_proc, "Hello World\n");
    // Waiting for the thread_proc to complete its execution
    // before exiting from the program
    t.join();
}  



How to resolve this error
  $ g++ Dummy.cpp -o Dummy -std=c++1z -pthread
    Dummy.cpp: In function 'int main()':
    Dummy.cpp:12:10: error: 'thread' is not a member of 'std'
       12 |     std::thread t(thread_proc, "Hello World\n");
          |          ^~~~~~
    Dummy.cpp:3:1: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
        2 | #include <thread>
      +++ |+#include <thread>
        3 | #include <string>
    Dummy.cpp:15:5: error: 't' was not declared in this scope; did you mean 'tm'?
       15 |     t.join();
          |     ^
          |     tm
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42319907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档