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.
int ToLower(string my_input)
{
int string_length = my_input.length();
for (int i=0; i<string_length; i++) {
my_input=tolower(my_input);
}
return 0;
}
Thanks.
nt i=0; char str[]="Test String.\n"; char c; while (str) { c=str; putchar (tolower(c)); i++;
---------- 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:
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>]�
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;
my_input=tolower(my_input);
}
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 crashed:))
I would be grateful for your opinion.
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;
my_input=tolower(my_input);
}
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.
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
#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 );
}