Char array to dynamic char array

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...

below original code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cctype>
#include <cstring>

using namespace 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[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 
Last edited on
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.
The updated code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cctype>
#include <cstring>

using namespace 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 = new char[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 
Thank you for the feedback, for some reason i could not get it to include code tags on the original message...
if the length of the string cannot be predetermined, how do i set the array size before cin.getline?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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
    const int MAX_STRING_LGTH = 26;

   //Character array to store string.
   char* str = new char[ 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').
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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 = new char[ 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
Last edited on
Ok thank you, much appreciated everyone!
Last edited on
Ok i think i get it now, thank you George P (4908). Much appreciated!
for some reason i could not get it to include code tags on the original message...

You can edit your original post and add them.

https://cplusplus.com/articles/jEywvCM9/
Ok awesome, i used the manual input and it worked perfect!
Ok awesome, i used the manual input and it worked perfect!

Nice!

I prefer to point people in the right direction instead of just giving the entire code to solve the assignment. :)
Really appreciate it George!
For some reason my code doesnt enter the function call?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

void toUppercase(int arrSize, char* &inputString); //Function prototype changes all
                                                   //lowercase characters to uppercase.

int main()
{
    char *stringToUpper;
    int arraySize;

    cout << "Enter the length of the string: ";
    cin >> arraySize;
    cout << endl;

    arraySize++;

    cin.ignore();

    cout << "Enter a string: ";
    cin.getline (stringToUpper, arraySize);
    cout << endl;

    stringToUpper = new char[arraySize];

    toUppercase(arraySize, stringToUpper); //Function call that changes all lowercase
                                           //characters to uppercase.

    delete [] stringToUpper;

    return 0;
}//end of main

void toUppercase(int arrSize, char* &inputString) //Function definition that changes all
                                                  //lowercase characters to uppercase.
{
    for (int count = 0; count < arrSize; count++) //Range based for loop
                                                  //to output character array.
    {
        cout << (char)toupper(inputString[count]);
    }
}//end of function toUppercase 
Your function's 2nd parameter is whacked. Don't use * and & to pass a C string/char array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cctype>
#include <cstring>

void toUppercase(size_t arrSize, char* inputString);

int main()
{
   int arraySize;

   std::cout << "Enter the length of the string: ";
   std::cin >> arraySize;
   std::cout << '\n';

   arraySize++;

   char* stringToUpper = new char[arraySize];

   std::cin.ignore();

   std::cout << "Enter a string: ";
   std::cin.getline(stringToUpper, arraySize);
   std::cout << '\n';

   // get the actual size of  the string, not the total possible
   size_t str_size = strlen(stringToUpper);

   toUppercase(str_size, stringToUpper);

   delete[] stringToUpper;
}

void toUppercase(size_t arrSize, char* inputString)
{
   for ( size_t count { }; count < arrSize; ++count )
   {
      std::cout << (char) ::toupper(inputString[ count ]);
   }
   std::cout << '\n';
}
Enter the length of the string: 5

Enter a string: test

TEST
Last edited on
Irony: the original function would have been "fine":
1
2
3
4
5
6
7
void toUppercase(char inputString[])
{
  for (int count = 0; count < strlen(inputString); count++)
  {
    cout << (char)toupper(inputString[count]);
  }
}

Why? These two are synonymous to compiler:
1
2
void toUppercase(char  inputString[]);
void toUppercase(char* inputString);

and it handles only strlen(stringToUpper) elements from the array.

For the record, that loop is not "Range based for loop". Since C++11 the "range-based" means something different with a different syntax.
std::toupper() requires an unsigned char as its param
https://en.cppreference.com/w/cpp/string/byte/toupper

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cctype>
#include <limits>

void toUppercase(const char* inputString);

int main() {
	size_t arraySize {};

	std::cout << "Enter the length of the string: ";
	std::cin >> arraySize;
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	const auto stringToUpper { new char[++arraySize] };

	std::cout << "Enter a string: ";
	std::cin.getline(stringToUpper, arraySize);

	toUppercase(stringToUpper);
	delete[] stringToUpper;
}

void toUppercase(const char* inputString) {
	for (; *inputString; std::cout << static_cast<char>(std::toupper(static_cast<unsigned char>(*inputString++))));

	std::cout << '\n';
}

Sadly, std::string isn't taught as first "dynamic array of char" for beginners.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cctype>
#include <string>

void toUppercase( std::string );

int main()
{
   std::string str;
   std::cout << "Enter a string: ";
   getline( std::cin, str );
   toUppercase( str );
}

void toUppercase( std::string input )
{
   for ( char& c : input ) c = toupper( c ); // Range-based for loop
   for ( char& c : input ) c = std::toupper( static_cast<unsigned char>(c) );
   std::cout << input << '\n';
}


[EDIT] So many details for the devil to hide in ...
Last edited on
1
2
// https://en.cppreference.com/w/cpp/string/byte/toupper#Notes
for ( char& c : input ) c = std::toupper( static_cast<unsigned char>(c) );
Topic archived. No new replies allowed.