cin.getline(name, 100); where 100 (in this example) is the size of the character array.getline(cin, name);strcpy(blank, name); // "common" version
strcpy_s(blank, 100, name); // Windows safe/secure version
blank = name;
n + '0' is the ASCII code for a character n places beyond '0.' The calculation fails of n is not with the specified range. The cast operator, (char), casts the ASCII code to a character.'A' = x 'B' = x + 1 'C' = x + 2 'D' = x + 3We can now restate the problem and its solution as follows:
'D' - 'A' = x + 3 - x = 3illustrating that the solution is independent of the actual ASCII codes.
A program is named my_program is executed from the command line as
my_program file1 file2 file3 file4If the program is written in C++ and the arguments are passed in to main, argc 5 and argv[2] is "file2"
Unlike Java, C++ counts the program name as a part of the command line arguments. There are five elements on the command line. So argc stores the value 5. But array indexes begin at zero as follows:
argv[0] is my_program argv[1] is file1 argv[2] is file2 argv[3] is file3 argv[4] is file4The example programs, name_box.cpp (version 2) and pyramid.cpp (version 2), demonstrate how to use the command line arguments.
When you answer questions like these, focus on what is given rather than speculating about code that is not given. This suggestion implies that it's also essential to read the question carefully and note all the information provided. For example, question 28 stated, "assume that the dynamic memory is deallocated elsewhere." So, answering that dynamic memory is not deleted or deallocated is incorrect. Questions like these make sense in the context of the assigned reading - these questions relate to a specific problem. If the question doesn't make sense, be sure you understand the related concepts from the reading assignment.
All four questions are about memory allocation, not scope. Scope is the place in a program where an identifier (i.e., a name), typically a variable, can be accessed or used. So, while it's true in the case of automatic or local variables that scope and memory allocation are tightly coupled, scope and memory allocation are not the same thing. We can easily see the distinction between scope and memory allocation in the case of "static" variables. When the function returns, the variable name goes out of scope, but the memory holding the variable is not deallocated.
Each solution described in the textbook comes with some consequences. I wouldn't call the consequences "errors" or even "problems." Much of the code that we write has advantages and disadvantages that we must match to the needs of the whole program and the specific problem that it solves.
char* read()
{
static char line[100];
cin.getline(line, 100);
return line;
}char* read()
{
char* line = new char[100];
cin.getline(line, 100);
return line;
}char* read(char* line)
{
cin.getline(line, 100);
return line;
}
void my_function()
{
char data[100];
read(data);
// do something with line
}
my_function ignores the value read returns, which is perfectly legal in C++. Pass-by-pointer is both an input and an output passing mechanism, which means that read still works here if we rewrite it as void read(char* line)
{
cin.getline(line, 100);
}
char* read()
{
char line[100];
cin.getline(line, 100);
return line;
}
Simply answering:
static, so it always has the same memory address and it retains the last name entered. Whenever the program calls get_name, getline overwrites the last name saved in name. The problem is illustrated in Consequences of static data (b). There are a couple of ways for the programmer to correct the problem:
#include <iostream>
using namespace std;
char* get_name()
{
|
int main()
{
char* array[3];
for (int i = 0; i < 3; i++)
array[i] = get_name();
for (int i = 0; i < 3; i++)
cout << array[i] << endl;
for (int i = 0; i < 3; i++)
delete array[i];
return 0;
} |
#include <iostream>
#include <cstring>
using namespace std;
char* get_name()
{
static char name[100];
cout << "Please enter a name: ";
cin.getline(name, 100);
return name;
}
|
int main()
{
char array[3][100];
for (int i = 0; i < 3; i++)
strcpy(array[i], get_name());
|