Question: write your code to allow users to use command-line arguments to specify number X to be printed Y times. Make sure your check possible wrong

write your code to allow users to use command-line arguments to specify number X to be printed Y times. Make sure your check possible wrong inputs of X and Y (including X not as a number or Y as negative or non-integer). Your source code can be written either inside Ubuntu using vi, pico, nano, etc. or in your host system then shared to Ubuntu. Run your program with different (correct and incorrect) inputs and show that your code works as needed and take snapshots of these.

Change the code so that users can specify an integer X to be printed out Y times on screen. So the command should be

./ch1 X Y

and the result

X

X

X

Make sure you check possible wrong inputs in X and Y.

#include

#include

#include

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

{

int max_num;

//check number of arguments. This number includes the command

if (argc != 2) {

printf("Usage: %s max_num (must be an integer) ", argv[0]);

return 1;

}

//convert argument string to integer

max_num = atoi(argv[1]);

for(int i = 0; i < counter; i++){

//counter is what Y was in the example.

printf("X");

}

printf("max_num=%d ", max_num);

return 0;

}

So I cannot figureout what wrong with this code.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!