1

The following is the problem code.

#include <iostream>
#include <cctype>
using namespace std;

void convertToUppercase(char *);

int main()
{
    char phrase[] = "characters and $32.98";

    cout << "The phrase before conversion is : " << phrase;
    convertToUppercase(phrase);
    cout << "\nThe phrase after conversion is : " << phrase << endl;
    return 0;
}

void convertToUppercase(char *sPtr)
{
    while (*sPtr != "\0")
    {
        if (islower(*sPtr))
            *sPtr = toupper(*sPtr);

        cout << "sPtr (before) = " << sPtr;
        sPtr++;
        cout << "\nsPtr (after) = " << sPtr << endl;
    }
}

The output window shows me that :

1>d:\sync files\converting lowercase letter to uppercase letters\converting lowercase letter to uppercase letters\main.cpp(19): error C2446: '!=' : no conversion from 'const char *' to 'int'
1>          There is no context in which this conversion is possible
1>d:\sync files\converting lowercase letter to uppercase letters\converting lowercase letter to uppercase letters\main.cpp(19): error C2040: '!=' : 'int' differs in levels of indirection from 'const char [2]'

Where is the mistake I made? I have exactly copied the code from my book but the problem still exists.

Sorry, I cannot find it out. need Help.

Thank you for your attention.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Casper
  • 4,435
  • 10
  • 41
  • 72
  • Your convert function should be a single `std::transform` call or ranged-for loop. You don't need to check that it's lower. – chris Nov 30 '13 at 17:10

6 Answers6

4

Simple, your comparison is wrong.

while (*sPtr != "\0")

should be

while (*sPtr != '\0')
turnt
  • 3,235
  • 5
  • 23
  • 39
3

Try

while (*sPtr != '\0')

You may also get a seg fault for modifying a string constant

IRocks
  • 89
  • 1
  • 5
3

The condition of your while loop is wrong, you need to write while(*sPtr != '\0') with single quotes.

Explanation: '\0' is a single character, "\0" is a string constant, i. e. an array of two characters in this case.

There are also shorter ways to write your loop condition:

  1. You can just use a plain zero instead of '\0': while(*sPtr != 0)

  2. You can even omit the comparison since you are comparing to zero: while(*sPtr)

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
3

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<unsigned char>(*sPtr))

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

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
2

All you need to do is change while (*sPtr != "\0") to while (*sPtr != '\0'). You're trying to compare a char to a string. I agree with @chris, though, you don't need to check if it's lower case. It's just messy to do so, and it won't decrease the running time of your algorithm.

2
  1. Your while condition should check *sPTR != '\0', "\0" is of type const char*
  2. If toupper is passed a negative value, you invoke UB, so make sure to call it with static_cast<unsigned char> (Details)
  3. You do not need to check islower, toupper already does that
  4. Make your life easier, use std::string:

    void convertToUpper (std::string& str) {
     for (auto& v:str)
         v = toupper(static_cast<unsigned char>(v));
    }
    
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182