se argument

Write your question here.

#include <iostream>
#include <string>
using namespace std;
int main()
{
double int_rate;
double years;
double amount;
double days;
Last edited on
Something along these lines:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>

// http://en.cppreference.com/w/cpp/language/main_function
int main( int argc, char* argv[] )
{
    const int RULE72 = 72;
    const int YDAYS = 365;

    if( argc != 4 )
    {
        std::cerr << "usage: " << argv[0] << " <amount> <interest rate> <years>\n" ;
        return 1 ;
    }

    try
    {
        // http://en.cppreference.com/w/cpp/string/basic_string/stol
        // http://en.cppreference.com/w/cpp/string/basic_string/stof
        const double amount = std::stod( argv[1] ) ;
        const double int_rate = std::stod( argv[2] ) ;
        const int years = std::stoi( argv[3] ) ;

        // validate: for example, amount is positive etc.
        // if validation fails, quit with an error message

        std::cout << "amount: " << amount << '\n'
                  << "interest rate: " << int_rate << '\n'
                  << "number of years: " << years << '\n' ;

        // compute stuff, display results
    }

    catch( const std::exception& )
    {
        std::cerr << "badly formed fields in command line\n" ;
        return 1 ;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>

// http://en.cppreference.com/w/cpp/language/main_function
// example of expected command line: program_name 10000 5.3 7
// argc == 4 the number of command line arguments; the first is the name of the program
// argv[0] == "program_name", argv[1] == "10000", argv[2] == "5.3", argv[3] == "7"
int main( int argc, char* argv[] )
{
    // not very serious because these are constants (with internal linkage),
    // but in general, it is a good idea to limit the visibility of names
    // to the smallest scope in which they are needed.
    // here, we need these values only inside main; they need not be at namespace scope.
    const int RULE72 = 72;
    const int YDAYS = 365;

    if( argc != 4 ) // if the program was invoked with wrong number of command line arguments
    {
        // inform the user, with a message indicating how the program should be invoked
        // std::cerr sends output to stderr, this is where error messages usually go
        // see: https://en.wikipedia.org/wiki/Standard_streams#Standard_error_.28stderr.29
        std::cerr << "usage: " << argv[0] << " <amount> <interest rate> <years>\n" ;
        return 1 ; // return from main (return to the host environment) with status 1
                   // by convention, if the value returned by main is non-zero,
                   // it indicates unsuccessful termination of the program
    }

    try
    {
        // try to convert the command line arguments (null-terminated strings)
        // to the numeric values that we need. eg. the amount argv[1] ( eg. "10000" ) to double
        // this attempt at conversion may fail; for example if argv[1] == "hello"
        // if the attempted conversion fails, an exception would be thrown
        // so we put the conversion code inside a try block
        // see: http://www.cplusplus.com/doc/oldtutorial/exceptions/
        //      (ignore the section 'Exception specifications'. It gives wrong information.)

        // http://en.cppreference.com/w/cpp/string/basic_string/stol
        // http://en.cppreference.com/w/cpp/string/basic_string/stof
        // the links above have more information on these functions
        const double amount = std::stod( argv[1] ) ;
        const double int_rate = std::stod( argv[2] ) ;
        const int years = std::stoi( argv[3] ) ;

        // validate: for example, amount is positive etc.
        // if validation fails, quit with an error message

        std::cout << "amount: " << amount << '\n'
                  << "interest rate: " << int_rate << '\n'
                  << "number of years: " << years << '\n' ;

        // compute stuff, display results
    }

    catch( const std::exception& )
    {
        // this block is executed only if an exception was thrown
        // when we attempted the conversion of command line arguments to numeric values
        std::cerr << "badly formed fields in command line\n" ;
        return 1 ;
    }

    // The main function need not have a return statement:
    // if control reaches the end without encountering a return statement,
    // the effect is as if we had written return 0;.at the end
}
Topic archived. No new replies allowed.