Docsity

Notes on Data Structure and Algorithms | EECS 281, Study notes of Algorithms and Programming

Toggle sidebar
bg1
1C:\Ubuntu\Shared\PA 1\getoptDemo.c
#include <iostream>
using namespace std;
int main(int noofarguments, char**argvalues)
{
int opt;
bool pflag = false,
rflag = false,
dflag = false,
errflag = false;
int p_option, r_option;
// The following switch and getopt statements execute the p, r and d flag processing of
data
while((opt = getopt(noofarguments, argvalues, "p:r:d")) != EOF) {
switch(opt)
{
case 'p':
pflag = true;
if(optarg == NULL)
{
cerr << "INVALID ARGUMENT" << endl;
}
else{
p_option = atoi(optarg); // Integer after -p is stored as an character
cout << "P option argument was " << p_option << endl;
}
break;
case 'd':
dflag = true; // If d flag is set, then set the boolean value of d_flag to true
break;
case 'r':
rflag = true;
if (optarg == NULL)
{
errflag = true;
}
else{
r_option = atoi(optarg); // Since there is an integer after -r
cout << "R option argument was " << r_option << endl;
}
break;
default:
errflag = true;
}
}
return 0;
}

Partial preview of the text

Download Notes on Data Structure and Algorithms | EECS 281 and more Study notes Algorithms and Programming in PDF only on Docsity!

C:\Ubuntu\Shared\PA 1\getoptDemo.c 1

#include using namespace std;

int main(int noofarguments, char**argvalues) { int opt; bool pflag = false, rflag = false, dflag = false, errflag = false;

int p_option, r_option;

// The following switch and getopt statements execute the p, r and d flag processing of data

while((opt = getopt(noofarguments, argvalues, "p:r:d")) != EOF) {

switch(opt) { case 'p': pflag = true;

if(optarg == NULL) { cerr << "INVALID ARGUMENT" << endl; } else{ p_option = atoi(optarg); // Integer after -p is stored as an character cout << "P option argument was " << p_option << endl; } break;

case 'd': dflag = true; // If d flag is set, then set the boolean value of d_flag to true break;

case 'r':

rflag = true;

if (optarg == NULL) { errflag = true; }

else{

r_option = atoi(optarg); // Since there is an integer after -r cout << "R option argument was " << r_option << endl;

}

break;

default: errflag = true; } }

return 0; }