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); }
);