Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

-So my source code runs on UNIX but it says segment fault. -so are -e & -d the v

ID: 3771188 • Letter: #

Question

-So my source code runs on UNIX but it says segment fault.

-so are -e & -d the variable for the decoder?

-I added them but they are not responding

-The code works but running it, it is missing the encoder decoder part

HOW CAN I FIX THIS?

(Please answer using C++ language)

(strcmp(argv[1], "-d") == 0)
(strcmp(argv[1], "-e") == 0);


#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[])
{
system("clear");
long bin, dec = 0, rem, num, base = 1;
int i=0;
ifstream myReadFile;
myReadFile.open("text.txt");
char *output;
char mystr[999];
// if (myReadFile.is_open())
{
if (strcmp(argv[1], "-d") == 0)
i=0;
while (!myReadFile.eof()) {


myReadFile >> output;

num = atol(output);


bin = num;
cout << bin;
while (num > 0)
{
   rem = num % 10;
   dec = dec + rem * base;
   base = base * 2;
   num = num / 10;
}

if(dec > 64 || dec < 91 || dec > 96 || dec <123)
(strcmp(argv[1], "-e") == 0);
{ mystr[i] = (char) dec;
   i++ ;}
* output =' ';
dec=0; base =1;
}
}
myReadFile.close();
cout << mystr;
return 0;
}

Explanation / Answer

int main(int argc, char * argv[]) {

if (argv[1] == "yes"); // Wrong, compares two pointers
if (strcmp(argv[1], "yes") == 0); // This compares what the pointers point to
if (std::string(argv[1]) == "yes"); // Works fine
if (argv[1] == std::string("yes")); // Works fine

// Easy-mode   
std::vector<std::string> args(argv, argv+argc);
for (size_t i = 1; i < args.size(); ++i) {
      if (args[i] == "yes") {
          // do something
      }
}

}