C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-11-2009, 03:39 PM   #1
Registered User
 
Join Date: Aug 2009
Posts: 34
Simple program problem from upper to lower case

My homework is to create a program that changes an Uppercase letter to a lower case letter. My teacher has not introduced the tolower function, so I am not using it. We were also required to use the cout.put and cin.get functions. Anyway, my program works but when I send it through my teachers automated assignment checker I get 2 warnings (this will cost me points). The warnings are as follows:

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   Reply With Quote
Old 08-11-2009, 03:53 PM   #2
Registered User
 
Join Date: Jan 2005
Posts: 7,252
Are you aware of the char variable type?
Daved is offline   Reply With Quote
Old 08-11-2009, 03:57 PM   #3
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,020
Quote:
Originally Posted by Daved View Post
Are you aware of the char variable type?
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));
If you want to avoid casts altogether, you can get input using ">>" instead of the get() function, and then declare all your variables are chars.
__________________
bit∙hub [bit-huhb] n. A source and destination for information.
bithub is offline   Reply With Quote
Old 08-11-2009, 04:11 PM   #4
Registered User
 
Join Date: Jan 2005
Posts: 7,252
>> 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   Reply With Quote
Old 08-12-2009, 02:31 AM   #5
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,732
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';
Which it may help to think of as:
Code:
lower = 'a' + (upper - 'A');
That is of course after you've determined that it is even an alphabet character and not um say, a digit or punctuation mark etc.
__________________
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
iMalc is offline   Reply With Quote
Reply

Tags
loss of data, loss of percision, noob

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:44 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22