如何在sublime中编写c++方法

How to write c++ methods in sublime 2

本文关键字:c++ 方法 sublime      更新时间:2023-10-16

这是我写的简单的c++程序。我正试图从命令行传入一个参数。当我尝试从命令行编译它时,它说有一个未定义的标识符'kgToLbs'。我认为这是一个可以接受的c++程序。我必须在Sublime 2中以不同的方式编写方法吗?

#include <iostream>
#include <string>
#include <iomanip>
/*
This program takes a numerical number from the 
command line and converts it into a numberical 
representation in lbs.
Then it displays the number to std output with
a precision of 2 decimal places
*/
using namespace std;
const double CONVERSION = 2.2046226218487757;
/*
    Convert Method
    This method converts from kg to lbs
    Parameter: A double
    Returns: A double 
*/
double kgToLbs(double num)
{
    double resultInPounds = num * CONVERSION;
    return resultInPounds;
}
int main(int argc, char *argv[])
{
    double numInLbs = 0;
    //Take the number passed in from the command line
    //Convert it to pounds
    numInLbs = kgToLbs(argv[0]);
    //Output the converted number to cout with a precision of 2 
    //decimal places
    cout << setprecision(2) << numInLbs << endl;
}

argv[0]char*,而kgToLbsdouble。您需要将输入转换为double,然后才能在该上下文中使用它。