#include <iostream.h> #include <stdlib.h> main(int argc,char *argv[]) { float a,b,answer; // ensure correct number of arguments if (argc != 3) { cout << "Wrong number of arguments for program\n"; exit(1); } /* convert argument string to float */ a = atof(argv[1]); b = atof(argv[2]); answer = a + b; cout << "Answer = " << answer << "\n"; }
#include <iostream.h> #include <stdlib.h> main(int argc,char *argv[]) { int add_flag = 1; int sub_flag = 0; int mult_flag = 0; float a,b; argc--; argv++; while ((argc > 0)&&(**argv == '-')){ switch(*++*argv){ case 'a' : // add add_flag = 1; break; case 's' : // subtract sub_flag = 1; add_flag = 0; break; case 'm' : // multiply mult_flag = 1; add_flag = 0; break; default : cout << "Unknown flag\n"; exit(1); } argc--; argv++; } if (argc != 2) { cout << "Wrong number of arguments for program\n"; exit(1); } a = atof(argv[0]); b = atof(argv[1]); if (add_flag == 1) cout << "answer = " << a+b << "\n"; else if ( mult_flag == 1) cout << "answer = " << a*b << "\n"; else if (sub_flag ==1) cout << "answer = " << a-b << "\n"; }
Back to Main Page for Lecture 7 |