如何从命令行运行我的代码? [英] How do I run my code from the command line?

查看:159
本文介绍了如何从命令行运行我的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

#include <iostream>
using namespace std;
int main(int argc,char arg[]){

    int a=arg[1];
    int b=arg[2];
    int c=a+b;
    cout<<c<<endl;


     return 0;
}

我使用windows 7 microsoft visual c ++ 2010
如何运行从命令行?

i am using windows 7 microsoft visual c++ 2010 how run it from command line?

推荐答案

导航到可执行文件(.exe)所在的目录。然后键入可执行文件的名称,后跟两个整数参数。

Navigate to the directory where the executable (.exe) is located. Then type the executable's name followed by two integer parameters.

C:\TestProg\> TestProg 5 6

原始示例中的问题已更正:

The problems in your original example are corrected here:

#include <iostream>
#include <sstream>

int main(int argc, char *arg[])
{
    std::stringstream sa;
    std::stringstream sb;
    int a;
    int b;
    int c;

    if (argc >= 3)
    {    
        // Convert string parameter into an integer.
        sa.str(arg[1]);
        sa >> a;

        if (!sa)
        {
            return 1;    // error
        }

        // Convert string parameter into an integer.
        sb.str(arg[2]);
        sb >> b;

        if (!sb)
        {
            return 1;    // error
        }
    }
    else
    {
        return 1;    // error
    }

    c = a + b;
    std::cout << c << std::endl;
    return 0;
}

这篇关于如何从命令行运行我的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆