Hi I want to know : 1 how to add a persons name to the end of a dynamic array?
2 how to check if the array is full, and if so to print a constant string?
3 how to search in a dynamic array for a specific name and replace it?
4 Returning a name at a specif index in the dynamic array

Dani AI

Generated

A compact, practical summary tied to the existing replies. As suggested, prefer std::vector<std::string> for a dynamic list; as noted, indexing looks the same as a C array but vector adds safe helpers; and as said, whether an array is "full" is an application-level definition. The examples below show common idioms (works in C++98/C++11) and a reference: std::vector reference.

Basic add / pre-allocate / "full" check (application-defined):

std::vector<std::string> names;
const std::size_t MAX = 100;           // application limit
const char FULL_MSG[] = "FULL";

// add a name
names.push_back("Alice");

// check if reached the application limit
if (names.size() >= MAX)
    std::cout << FULL_MSG << '\n';

// reserve to avoid repeated reallocations
names.reserve(MAX);

Search and replace (first match or all matches):

#include <algorithm>

// find first occurrence and replace
auto it = std::find(names.begin(), names.end(), oldName);
if (it != names.end())
    *it = newName;

// replace every occurrence
std::replace(names.begin(), names.end(), oldName, newName);

Return by index (bounds and safety): use names.at(i) for bounds-checked access (throws std::out_of_range) or names[i] if an unchecked fast access is acceptable. Prefer const std::string& to avoid copies when only reading. Note that push_back may reallocate and invalidate pointers/iterators/references into the vector; if stable references are required consider other containers (e.g., std::list, std::deque) or maintain indices/ an unordered_map for fast lookup.

Recommended Answers

All 3 Replies

Use .

Hi I want to know : 1 how to add a persons name to the end of a dynamic array?
2 how to check if the array is full, and if so to print a constant string?
3 how to search in a dynamic array for a specific name and replace it?
4 Returning a name at a specif index in the dynamic array

Exactly as you would a regular array. There is no real difference.

1-Ensure there is room for that and assign it
2-Full is something that you define. If the items in the array is equal to limit, it is full
3-Iterate over each element and check if it equals the name
4-Add index to the array pointer and dereference it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.