Program with dynamic char array just skips past function call

This is an assignment question, i had to include a dynamic array in the below program. Since i have included the dynamic char array it skips past the function call and i dont know why? Can anyone help?

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
#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 
Last edited on
The code doesn't use a dynamic array.

I'll give you a hint, when passing a regular char array/C string into a function you can't use strlen to find out it's length. That information is lost when the array devolves to a pointer.

You should figure out in main how big the actual string is, and then pass that information along with the array into your function.
Apologies George, i pasted the wrong code...
I have 2 versions open in the compiler.

Please see updated code...
You need to allocate the array before using getline.

No need to have the second argument being passed by reference.
Last edited on
You really shouldn't start a second topic when you already have one dealing with the same code. You can "unclose" the other topic.

As I replied in the other topic:

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
Thanks everyone. It is working perfectly now!
Heh, you said the code was working perfectly earlier, and then you started a new topic on the same code and announced things were NOT perfect.
George P wrote:
when passing a regular char array/C string into a function you can't use strlen to find out it's length.

If what you're interested in is the length of the string (not the length of the array) then there is no problem using strlen (assuming it's a null-terminated string).
My comment was made on a code version now changed that used a static sized C string, passed that static C string into a function and then attempted to get the string size on a devolved pointer. *BUZZ!*

Yes, it works, until one day it doesn't. Best to not rely on that.

The currently shown code from the OP has a similar error with passing the size of the array into the function, not the actual size of the C string.
Last edited on
Apologies George,

I marked that previous topic as solved and was unaware I could re-open it. I am new here and am learning fast... As for the repost, the code was working fine as we had discussed but when I tried to include the function, that is when I was having problems again... I seem to be a little trigger happy in my posting so i apologize.
Take some time to TEST, TEST, TEST! before declaring The War Is Over, Victory Is US! :D

I've done that before and got caught with inadequate testing. 'Tis a fact of being a diligent programmer. :)

The number of tests I do with even minor revisions on code that worked previously. Oy!
And honestly putting a green check-mark to a topic should be the least of your concerns. Most people, even the experienced folks here, don't bother. Everyone just does an Elvis and leaves.
Yes I have realized its too easy to just post my code in this forum when I am having problems and then it ends up just being some silly syntax error that I missed...

Nevertheless, thanks for the help George, much appreciated!
George P wrote:
My comment was made on a code version now changed that used a static sized C string, passed that static C string into a function and then attempted to get the string size on a devolved pointer. *BUZZ!*

Yes, it works, until one day it doesn't. Best to not rely on that.

I haven't seen the original code but to me it sounds like you're saying strlen is unreliable. A "C string" implies that it is null-terminated so I don't see what could go wrong. Of course the user could accidentally pass someting that is not a "C string" but that is really the fault of the caller and no C string functions will handle that correctly, not even std::cout<<.

Are you perhaps confusing strlen with sizeof?
Last edited on
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
#include <iostream>
#include <cctype>
using namespace std;

void toUppercase( char *inputString );

int main()
{
   int arraySize;
   cout << "Enter the length of the string: ";
   cin >> arraySize;   cin.ignore();
   arraySize++;
   char* str = new char[arraySize];

   cout << "Enter a string: ";
   cin.getline ( str, arraySize );
   toUppercase( str );
   cout << str;

   delete [] str;
}

void toUppercase( char *inputString )
{
   for ( ; *inputString; inputString++ ) *inputString = toupper( *inputString );
}
Topic archived. No new replies allowed.