![]() |
| | #1 |
| Registered User Join Date: Aug 2009
Posts: 30
| Simple program problem from upper to lower case Source 1.cpp(39) : warning C4244: 'argument' : conversion from 'int' to 'char', possible loss of data and cout.put((int)myLower); // cout.put outputs char found by myUpper + TOLOWER Source 1.cpp(39): error 734: (Info -- Loss of precision (arg. no. 1) (31 bits to 7 bits)) Here is my code. Thank you for help in advance: Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int myUpper; // declares myUpper an integer allowing us to use const char TOLOWER
int myLower;
const int TOLOWER = ' '; // initializes TOLOWER to ' ' which has decimal value of 32
cout << "Please Enter an Upper Case Letter: "; // asks user for myUpper
myUpper = cin.get(); // cin.get takes first character entered by user
myLower = myUpper + TOLOWER; // add value of 32 to upper case decimal value to find a lower case
cout << "\nYour Lower Case Letter is: ";
cout.put((int)myLower); // cout.put outputs char found by myUpper + TOLOWER
cout << "\n\n"; // newlines entered for clarity
return(EXIT_SUCCESS);
}
|
| steals10304 is offline | |
| | #2 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| Are you aware of the char variable type? |
| Daved is offline | |
| | #3 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| It doesn't matter since cin.get() returns an int and cout.put() takes a char, he will have a warning whichever way he declares the variable. If you just want to get rid of the warning, you can do a cast: Code: cout.put(static_cast<char>(myLower));
__________________ bit∙hub [bit-huhb] n. A source and destination for information. |
| bithub is offline | |
| | #4 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| >> It doesn't matter since cin.get() returns an int and cout.put() takes a char, I missed that at first, but according to dinkumware's reference put takes a char and get has a version that works with char as well. >> you can get input using ">>" instead of the get() function The assignment requires get and put. |
| Daved is offline | |
| | #5 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,476
| How on earth is anyone supposed to know that adding a space to an uppercase char makes if lowercase? There are functions for that sort of thing: isupper and tolower, iirc. Even if you insist on rolling your own, you should do: Code: lower = upper - 'A' + 'a'; Code: lower = 'a' + (upper - 'A');
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger |
| iMalc is offline | |
![]() |
| Tags |
| loss of data, loss of percision, noob |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A Full Program to analyze. | sergioms | C Programming | 2 | 12-30-2008 09:42 AM |
| get keyboard and mouse events | ratte | Linux Programming | 10 | 11-17-2007 05:42 PM |
| Problem with fgets ! | Astra | C Programming | 6 | 11-18-2006 09:23 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C Programming | 3 | 03-04-2005 02:46 PM |
| simple frontend program problem | gandalf_bar | Linux Programming | 16 | 04-22-2004 06:33 AM |