3

I know you can access individual characters stored in a character array:

example[] = 'HeLlo";
for (int i = 0; i < 6; i++)
{
    if (example[i] >= 65 || example[i] <= 90)
    {
         example[i] += 32;
    }
}

Unfortunately this won't work for my code as I have to store it in a character pointer (I am taking input from the user). How can I check each individual character in a char pointer in a for loop? This is where I am at right now:

// Store input in mode
char* mode;
std::cin >> mode;
    

// Convert string to lowercase
for (int i = 0; i < std::strlen(mode); i++)
{
    //if statement
}
RudyGoburt
  • 291
  • 1
  • 11
  • 2
    why do you use char pointer? – bolov Aug 04 '20 at 19:28
  • I can’t figure out a way to store the users input in a character array – RudyGoburt Aug 04 '20 at 19:29
  • Your string uses the wrong quote at the start. – tadman Aug 04 '20 at 19:29
  • 1
    Hint: Read into a `std::string`. You can't read into an *uninitialized pointer*. – tadman Aug 04 '20 at 19:30
  • Are you trying to compare strings, as in `input == "test"`? If so, `std::string` can do it. – tadman Aug 04 '20 at 19:31
  • 2
    The only thing you can *store* in a pointer is an address. That pointer then points to that memory location - that's all the pointer tells you. You don't store arrays of *anything* in pointers, you use pointers to point to things. How many elements of *whatever* are at that location is something you need to keep track of besides the pointer. The pointer just points to a memory location, that's *all*. A pointer is just like a sign saying "there's something *over there*", nothing else. – Jesper Juhl Aug 04 '20 at 19:32
  • You need to allocate memory for `mode` before you can read anything into it. – Barmar Aug 04 '20 at 19:39

2 Answers2

2

You can't store user input into an uninitialized pointer. You need to allocate a character array of sufficient space, and then read into that array, eg:

#include <iostream>
#include <iomanip>
#include <cstring>

// Store input in mode
char mode[256] = {};
std::cin >> std::setw(256) >> mode;
or:
std::cin.get(mode, 256);

// Convert string to lowercase
for (size_t i = 0; i < std::strlen(mode); ++i)
{
    if (mode[i] >= 'A' || mode[i] <= 'Z')
    {
         mode[i] += 32;
    }
}

Note that if the user tries to enter more than 255 characters, the input will be truncated to fit the array. Using a std::string instead would avoid that, eg:

#include <iostream>
#include <string>

// Store input in mode
std::string mode;
std::cin >> mode;

// Convert string to lowercase
for (size_t i = 0; i < mode.size(); ++i)
{
    if (mode[i] >= 'A' || mode[i] <= 'Z')
    {
         mode[i] += 32;
    }
}

Either way, whether you use a char[] array or a std::string, you can use std::transform() to lowercase the characters without using a manual loop (see How to convert std::string to lower case?), eg:

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <cstring>

// Store input in mode
char mode[256] = {};
std::cin >> std::setw(256) >> mode;
or:
std::cin.get(mode, 256);

// Convert array to lowercase
std::transform(mode, mode + std::strlen(mode), mode,
    [](unsigned char ch){ return std::tolower(ch); }
);
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

// Store input in mode
std::string mode;
std::cin >> mode;

// Convert string to lowercase
std::transform(mode.begin(), mode.end(), mode.begin(),
    [](unsigned char ch){ return std::tolower(ch); }
);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • If std::string is in a function and I want to return the string to a variable main, what data type do I use to declare the function? – RudyGoburt Aug 04 '20 at 19:46
  • 1
    @RudyGoburt Declare it as returning a `std::string`... – underscore_d Aug 04 '20 at 19:54
  • @RudyGoburt if you want to return a `std::string` then you declare the function to return a `std::string`, just like you do with any other data type, eg: `std::string theFunction() { std::string s; ... return s; }` `int main() { std::string s = theFunction(); ... }` – Remy Lebeau Aug 04 '20 at 19:56
2

You need to allocate some memory to your pointer before you get the input. The below change will help to run the code :

// Store input in mode
char* mode = new char[max_length];
std::cin >> mode;

// Convert string to lowercase
for (int i = 0; i < std::strlen(mode); i++)
{
    //if statement
}

You just need to be cautious as you need to delete the memory at the end of the program which is being allocated by new. Also, the cin >> mode will only take a single word as input.

Jasmeet
  • 1,315
  • 11
  • 23
  • If you use `operator>>` with a `char[]`/`char*` buffer, you should use `std::setw()` to specify the size of the buffer so `operator>>` can't overflow it. – Remy Lebeau Aug 04 '20 at 19:51