c++ main函数基本操作(1)-- 解析参数argv、string转double、double 转 string、cout 示例/printf 示例、暂停控制台

/*
 1 解析参数,argc表示参数数量,argv表示参数内容
     //cmd命令行:c:/Users/lenonvo/>./main.exe -s 3.24567 -p 7.4566
 2 string 转 double
 3 cout 示例/printf 示例
 4 double 转 string

 4.5  int 转 string
 5 暂停控制台,不退出

*/
#include <iostream>
#include <string>
#include <stdio.h>
#include <windows.h>
using namespace std;

void print_help()
{
    std::cout << "eye_tracking" << std::endl;
}

string dtostr(double value) {
    char buffer[20];
    _gcvt_s(buffer, 20, value, 15);
    string str = buffer;
    return str;
}

int main(int argc, char ** argv) {

    double slant;
    double pitch;
    int s, p;

    //1 解析参数,argc表示参数数量,argv表示参数内容
    //cmd命令行:c:/Users/lenonvo/>./main.exe -s 3.24567 -p 7.4566
    for (int i = 1; i < argc; i++) 
    {
        printf("Argument %d is %s\n", i, argv[i]);
        if (strcmp(argv[i], "-s") == 0)
        {
            string slanttemp = argv[i + 1];
            //2 string 转 double
            slant = atof(slanttemp.c_str());
        }
        else if (strcmp(argv[i], "-p") == 0)
        {
            string pitchtemp = argv[i + 1];
            //3 string 转 double
            pitch = strtod(pitchtemp.c_str(), NULL);
        }        
        else if (strcmp(argv[i], "--help") == 0)
        {
            print_help();
            return -1;
        }    

       // int 转string

           LPCSTR temp = to_string(dh).c_str();
    }
    // 5 cout 示例
    std::cout <<"slant:"<< slant << std::endl;
    std::cout <<"pitch:"<< pitch << std::endl;

    // 4 double 转 string
    std::cout << "slant:"+to_string(slant) << std::endl;
    std::cout << "pitch:" + to_string(pitch) << std::endl;

    // 6 printf 示例
    printf("Argument is %f\n", slant);
    printf("Argument is %f\n", pitch);

    s = slant;
    p = pitch;

    printf("Argument is %d\n", s);
    printf("Argument is %d\n", p);
    
    // 7 暂停控制台,不退出。
    system("pause");
    //getchar(); 
    //Sleep(5000000);
    //while (1)
    //{
    //    int x;
    //    cin >> x;
    //}
    //char a;
    //while(cin>>a);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值