error: invalid conversion from 'const char*' to 'char*' [-fpermissive]

I want to change the characters in a string passed by user, converted into a C-style string and passed as an argument to a function with a char * argument:

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
#include <ctype.h>
#include <string>
#include <cstring>
#include <iostream>
#include <stdlib.h>

 void functoupper(char *myString)
      {
          int i=0;
          char z;
          do {
              z= myString[i];
              myString[i]=toupper(z);
              ++i;
          } while(myString[i]!=0);
      }

int main() {

	std::string name;

  	std::cout << "Please, enter your full name in small caps: ";
  	std::getline (std::cin,name);
        const char *myString = name.c_str();
 	std::cout << "Hello, " << functoupper(myString) << "!\n";

	return 0;	
}


I get error error: invalid conversion from 'const char*' to 'char*' [-fpermissive] when calling function functoupper(myString) in main().
Last edited on
you cannot modify the data; it is a true pointer to the string's internal data and modification outside the string is unsafe and prevented via const. One of a few reasons.. what if you changed its size via the pointer, the string object would not know this and its .length() method would become corrupt.

you can make a copy, and modify that, or you can just modify the c++ string directly.

consider

std::transform(name.begin(), name.end(),name.begin(), ::toupper);//calls toupper on each letter

you can also bypass the issue, but again, if you do the wrong kinds of things you WILL break something, this is NOT SAFE (however you may learn something from it). C++ strings are not always zero terminated, so you probably want to add name.length() as a parameter if you mess with this idea.
functoupper(&name[0])
Last edited on
I fail to see how you could output anything on line 25 if functoupper is a void function.
Is the function prototype mandated by your homework assignment (presuming it is one)?

If it IS...
then you need to change main() to use a character array, alas.

18
19
20
21
22
23
24
25
26
int main() {

	char name[100];

	std::cout << "Please, enter your full name in small caps: ";
	std::cin.getline( name, sizeof(name) );
	functoupper(name);
	std::cout << "Hello, " << name << "!\n";
}


If it is NOT:
...then change the function to take a std::string as argument.

1
2
3
4
void functoupper(std::string s)
{
  ...
}

The inside of the function will be very much the same, except you can use a for loop:

for (int n = 0; n < s.size(); n++)

Hope this helps.
Is the function prototype mandated...?

Is the code itself mandated? If the string is empty (first char is '\9') then the do/while test reads one past that (which may not be '\0'!).
@dutch
If this is a homework, the point is to have dopazoma use what he learned about loops and accessing and modifying arrays to write a strupper function.
(*facepalm*) Right. I really wasn't thinking there!
No worries. Happens to me all the time. :O)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cctype>
#include <iostream>
using namespace std;

void functoupper( char *c )
{
   while ( *c ) { *c = toupper( *c );  c++; }
}

int main()
{
   char myString[100];
   cout << "Please, enter your full name: ";
   cin.getline( myString, sizeof myString );
   functoupper( myString );
   cout << "Hello, " << myString << "!\n";
}


Please, enter your full name: julius caesar
Hello, JULIUS CAESAR!
Last edited on
Topic archived. No new replies allowed.