> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/core-curriculum/rank04/module00/megaphone.md).

# megaphone

The goal is to create a program that converts command-line arguments to **uppercase** and prints them. If no arguments are provided, it outputs a default loud noise message. Here's how it works:

1. **Check for Arguments**:
   * `argc` (argument count) determines how many arguments are passed.
   * If `argc > 1`, process the arguments (since `argv[0]` is the program name).
   * If `argc == 1`, print the default message.
2. **Convert to Uppercase**:
   * Iterate over each argument (`argv[i]`).
   * For each character in the argument, use `toupper()` to convert it to uppercase.
3. **Output**:
   * Print all converted arguments consecutively, followed by a newline.

***

#### Code with Detailed Comments

```cpp
#include <iostream>   // For input/output (std::cout, std::endl)
#include <cctype>     // For toupper() function
#include <string>     // For std::string

int main(int argc, char **argv) {
    if (argc > 1) {   // Check if arguments exist (excluding program name)
        // Loop through each argument starting from index 1
        for (int i = 1; i < argc; ++i) { 
            // Loop through each character in the current argument
            for (int j = 0; argv[i][j] != '\0'; ++j) { 
                // Convert character to uppercase and print it
                std::cout << static_cast<char>(toupper(argv[i][j]));
            }
        }
        std::cout << std::endl; // End the output line after all arguments
    } else {
        // Default message when no arguments are provided
        std::string text = "* LOUD AND UNBEARABLE FEEDBACK NOISE *";
        std::cout << text << std::endl;
    }
    return 0; // Indicate successful execution
}
```

***

#### Key Notes

* **Avoid `using namespace std`**:\
  Directly prefix standard library components with `std::` (required by the problem rules).
* **Character Conversion**:\
  `toupper()` returns an `int`, so we cast it to `char` for proper printing.
* **Null-Terminated Strings**:\
  The inner loop runs until `argv[i][j]` hits the null terminator (`\0`).

***

#### Compilation & Execution

1. **Compile**:

   ```bash
   c++ -Wall -Wextra -Werror -std=c++98 megaphone.cpp -o megaphone
   ```

   Ensures strict C++98 compliance and error checking.
2. **Run Examples**:

   ```bash
   $ ./megaphone "Hello World!"  
   HELLOWORLD!  

   $ ./megaphone  
   * LOUD AND UNBEARABLE FEEDBACK NOISE *  
   ```

***

#### Why This Works

* **Efficiency**: No dynamic memory allocation; processes input directly.
* **Clarity**: Uses standard C++ functions (`toupper`, `std::cout`) for readability.
* **Edge Cases**: Handles no-argument scenarios gracefully.

This solution adheres to C++98 standards and follows best practices for clean, maintainable code.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/core-curriculum/rank04/module00/megaphone.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
