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

After completing Exercise 8.1.1, test it by experimenting with different limitat

ID: 3892458 • Letter: A

Question

After completing Exercise 8.1.1, test it by experimenting with different
limitations for the str string. It helps if you replace the number 600 with the sym-
bolic constant STRMAX, putting the following #define directive at the beginning
of the program. During preprocessing, this directive causes the compiler to replace
occurrences of STRMAX in the source code with the indicated text (600).
#define STRMAX 600
You can then use STRMAX to declare the length of str
char str[STRMAX];
and then use STRMAX to determine how many bytes to copy:
strncpy(str, " My name is ", STRMAX);
strncat(str, name, STRMAX - strlen(str));
The beauty of this approach is that if you need to change the maximum string
size, you need to change only one line of code (the line containing the #define
directive) and then recompile.
c++

Explanation / Answer

Below is your program. Please comment if you have any issues


#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
#define STRMAX 600 // The macro STRMAX is defined to 600

int main() {

char str[STRMAX];
char name[STRMAX];
char address[STRMAX];
char work[STRMAX];

cout << "Enter name and press ENTER: ";
cin.getline(name, STRMAX);
cout << "Enter address and press ENTER: ";
cin.getline(address, STRMAX);
cout << "Enter workplace and press ENTER: ";
cin.getline(work, STRMAX);
strncpy(str, " My name is ",STRMAX);
strcat(str, name);
strncat(str, ", I live at ",STRMAX);
strncat(str, address, STRMAX - strlen(str));
strncat(str, ", and I work at ",STRMAX);
strcat(str, work);
strcat(str, ",");

cout << str << endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote