No conversion from “const char” to “int”

前端 未结 6 1151
野性不改
野性不改 2021-01-25 11:01

The following is the problem code.

#include 
#include 
using namespace std;

void convertToUppercase(char *);

int main()
{
    cha         


        
6条回答
  •  不思量自难忘°
    2021-01-25 11:29

    You are trying to compare a char to a char const* (well, really a char const[2] but that's just a detail). You probably meant to use

    while (*sPtr != '\0')
    

    or

    while (*sPtr)
    

    Note that your use of islower() and toupper() isn't guaranteed to work: the argument to these functions has to be positive but char may have negative values. You need to first convert the char to unsigned char:

    toupper(static_cast(*sPtr))
    

    The test for islower() isn't needed: on non-lower chars toupper() just returns the argument. Omitting the trst is improving the oerformance.

提交回复
热议问题