I have an assignment question that requests that i take my code where i have declared a normal char array and change it to a dynamic char array but when my code runs there are no errors but there is also no output...
#include <iostream>
#include <cctype>
#include <cstring>
usingnamespace std;
constint MAX_STRING_LGTH = 25; //Constant int for max length of string.
void toUppercase(char inputString[]); //Function prototype changes all
//lowercase characters to uppercase.
int main()
{
char stringToUpper[MAX_STRING_LGTH]; //Character array to store string.
cout << "Enter a string: ";
cin.getline (stringToUpper, MAX_STRING_LGTH);
cout << endl;
toUppercase(stringToUpper); //Function call that changes all lowercase
//characters to uppercase.
return 0;
}//end of main
void toUppercase(char inputString[]) //Function definition that changes all
//lowercase characters to uppercase.
{
for (int count = 0; count < strlen(inputString); count++) //Range based for loop
//to output character array.
{
cout << (char)toupper(inputString[count]);
}
}//end of function toUppercase
you need to get the memory for the pointer BEFORE you assign it a value.
Also, please use code tags to format your code so its readable.
..
cin.getline (stringToUpper, '/n');
...
stringToUpper = new char[arraySize];
basically, reverse the above with the appropriate doctoring of the in between statements etc. Note that it is also really important that arraysize HAS A VALUE before you get that many bytes. And, don't forget, "the" needs 4 characters, not 3.
#include <iostream>
#include <cctype>
#include <cstring>
usingnamespace std;
//const int MAX_STRING_LGTH = 25; //Constant int for max length of string.
void toUppercase(char inputString[]); //Function prototype changes all
//lowercase characters to uppercase.
int main()
{
char *stringToUpper;
int arraySize;
//char stringToUpper[MAX_STRING_LGTH]; //Character array to store string.
cout << "Enter a string: ";
cin.getline (stringToUpper, '/n');
cout << endl;
stringToUpper = newchar[arraySize];
toUppercase(stringToUpper); //Function call that changes all lowercase
//characters to uppercase.
return 0;
}//end of main
void toUppercase(char inputString[]) //Function definition that changes all
//lowercase characters to uppercase.
{
for (int count = 0; count < strlen(inputString); count++) //Range based for loop
//to output character array.
{
cout << (char)toupper(inputString[count]);
}
}//end of function toUppercase
#include <iostream>
#include <cctype>
#include <cstring>
int main()
{
//Constant int for max length of string.
// if you want 25 characters you need to add one space for the '\0' terminator
constint MAX_STRING_LGTH = 26;
//Character array to store string.
char* str = newchar[ MAX_STRING_LGTH];
std::cout << "Enter a string: ";
std::cin.getline(str, MAX_STRING_LGTH);
std::cout << '\n';
// sanity check, let's confirm you actually entered a string.
std::cout << str << '\n';
// not really needed since the heap memory will go away when the program ends
delete[] str;
}
OR
You ask the user how many characters they want to enter. Add 1 to that for the null terminator ('\0').
#include <iostream>
#include <cctype>
#include <cstring>
#include <limits>
int main()
{
int MAX_STRING_LGTH;
std::cout << "How many characters do you want to enter? ";
std::cin >> MAX_STRING_LGTH;
// ignore to the end of line
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
MAX_STRING_LGTH++; // add an extra space for '\0'
char* str = newchar[ MAX_STRING_LGTH ];
std::cout << "Enter a string: ";
std::cin.getline(str, MAX_STRING_LGTH);
std::cout << '\n';
// sanity check, let's confirm you actually entered a string.
std::cout << str << '\n';
// not really needed since the heap memory will go away when the program ends
delete[] str;
}
How many characters do you want to enter? 5
Enter a string: 12345678
12345
Also as inputString is null-terminated, you don't need to pass it's size (or use strlen() to obtain it's size). Just iterate until you find \0. Consider: