conversion to 'char' from 'int' warning


 
Thread Tools Search this Thread
Top Forums Programming conversion to 'char' from 'int' warning
# 1  
Old 12-07-2010
conversion to 'char' from 'int' warning

Hi,

I wrote a simple code in C++ converting from UpperToLower case characters. However, my compiler gives me a warning:
"warning: conversion to 'char' from 'int' may alter its value".
Any tips?
I would like to stress, I don't want to load my string into char array.
Code:
int ToLower(string my_input)
  {
         int string_length = my_input.length();
         for (int i=0; i<string_length; i++) {
    my_input[i]=tolower(my_input[i]);
      }
  return 0;
  }

Thanks.
Code:
nt i=0;   char str[]="Test String.\n";   char c;   while (str[i])   {     c=str[i];     putchar (tolower(c));     i++;


Last edited by Scott; 12-07-2010 at 07:59 AM.. Reason: Code tags, please...
# 2  
Old 12-07-2010
Use the itoa() / atoi() function ?
# 3  
Old 12-07-2010
I will try.
Thanks

---------- Post updated at 06:56 AM ---------- Previous update was at 06:20 AM ----------

Hi,

It looks that itoa is not supported by my compiler.
I tried with casting. However, I am getting an error:
Code:
error: invalid conversion from ‘char’ to ‘const char*’
error:   initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&)
 [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]’

Code:
int ToLower(string my_input)
  {
         int string_length = my_input.length();
         for (int i=0; i<string_length; i++) {
// here trying to convert from int to string
my_input = (string) my_input[i];
    my_input[i]=tolower(my_input[i]);
      }
  return 0;
  }

Thanks.

---------- Post updated at 07:04 AM ---------- Previous update was at 06:56 AM ----------

Ok, I found, my mistake. I am converting NOT from string but from char to *char... so my_input[i] is a char.
working on that...

---------- Post updated at 07:28 AM ---------- Previous update was at 07:04 AM ----------

Hi,

I googled it.
This should work but I am not sure in 100%.
The problem is that the server I am using has crashedSmilie)
I would be grateful for your opinion.
Code:
int ToLower(string my_input)
  {
         int string_length = my_input.length();
         for (int i=0; i<string_length; i++) {
// here trying to convert from int to string
    typedef char charType; 
    charType MyChar = my_input[i];
    my_input[i]=tolower(my_input[i]);
      }
  return 0;
  }

---------- Post updated at 07:39 AM ---------- Previous update was at 07:28 AM ----------

I am sorry.
My post was to chaotic.
Greetings for all.

Last edited by Scott; 12-07-2010 at 08:34 AM.. Reason: Code tags
# 4  
Old 12-07-2010
The declaration for tolower() is:

Code:
int tolower( int );

The function takes an int argument, and returns an int result. Since the cast from char to int is probably from 8 to 16 bits, no data is lost. But on the cast back to char from the int return, the change from 16 to 8 bits means data could be lost.

Of course, changing the contents of the string object passed to ToLower() isn't going to change the contents of the string in the calling code. Since the object is passed by value, the object that the ToLower() method operates on is a copy of the object in the calling code. For example, this code

Code:
#include <ctype.h>
#include <string>
#include <iostream>

using namespace std;

void toLowerVal( string str )
{
    for ( int ii = 0; ii < str.length(); ii++ )
    {
        str[ ii ] = ( char ) ::tolower( str[ ii ] );
    }
}

void toLowerRef( string &str )
{
    for ( int ii = 0; ii < str.length(); ii++ )
    {
        str[ ii ] = ( char ) ::tolower( str[ ii ] );
    }
}

int main( int argc, char **argv )
{
    for ( int ii = 1; ii < argc; ii++ )
    {
        string s1 = argv[ ii ];
        cout << s1 << endl;
        toLowerVal( s1 );
        cout << s1 << endl;
        toLowerRef( s1 );
        cout << s1 << endl;
    }

    return( 0 );
}

produces this output:

Code:
bash-3.2$ ./str ASDf as1FFFF
ASDf
ASDf
asdf
as1FFFF
as1FFFF
as1ffff

Note that calls to toLowerVal() do not change the data contained in the calling code.
# 5  
Old 12-08-2010
Hi,

You are right.
A correct, working version of a code (with casting) should be:
Code:
int ToLower(string my_input)
  {
         int string_length = my_input.length();
         for (int i=0; i<string_length; i++) {
    my_input[i]=char(tolower(my_input[i]));
      }
  return 0;
  }

Thanks.

Last edited by Franklin52; 12-08-2010 at 06:39 AM.. Reason: Please use code tags, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Bool vs char * conversion

I have a problem at make step to install a downloaded package consisted of different programs. In file included from kcdbext.cc:16:0: kcdbext.h: In member function �char* kyotocabinet::IndexDB::get(const char*, size_t, size_t*)’: kcdbext.h:1281:14: error: cannot convert �bool’ to... (3 Replies)
Discussion started by: yifangt
3 Replies

2. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies

3. Programming

Double to const char conversion

Dear all, I am using C and ROOT for programming. And I need to incorporate following in my code. char *fps=NULL; int dec=0,sign=0; float mean = h1->GetMean(1); //0.001298 fps= fcvt(mean,6 , &dec, &sign); I need to provide this mean as const char to some other function to get... (8 Replies)
Discussion started by: emily
8 Replies

4. Programming

Small query regarding function "char * strerror(int errnum)"

As this function returns the address of the string corressponding to the errno value provided to it. Can someone please let me know where, in the memory, it could be (on freeBSD). The MAN page tells under the BUG section that "For unknown error numbers, the strerror() function will return its... (5 Replies)
Discussion started by: Praveen_218
5 Replies

5. Programming

Help with understanding ( int, char, long, short, signed, unsigned etc.... )

My question is simple: When should I use a long, int, char, unsigned/signed variables?? When I declare a variable "unsigned;" what did I do it??? Why would I delcare an integer "long" or "short" ( unsigned or signed)?? Any examples of when things like "unsigned", "long", "short" etc...... (6 Replies)
Discussion started by: cpp_beginner
6 Replies

6. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

7. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

8. UNIX for Dummies Questions & Answers

ANSI C, char to hex conversion

Hi, I have a char buf,ch; and the buf is filled with the result from MySQL server which I get like this numbytes = recv(sock, buf, 1024, 0));I have the followingcode to display the results printf("received %ld bytes:\n",numbytes); for(c=0;c<numbytes;c++){ ch = (char)buf; ... (2 Replies)
Discussion started by: alikims
2 Replies

9. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

10. Programming

char to int64 conversion

Hi, I'm converting a C program that I made using the Visual Studio. I now use GCC (over Linux) and can't find some equivalences. I changed my __int64 definitions to unsigned long long, but can't find an equivalent to the microsoft i64toa() function, which let you convert a char* to a 64 bit... (1 Reply)
Discussion started by: Raspoutine
1 Replies
Login or Register to Ask a Question