Thread: Simple program problem from upper to lower case

  1. #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);
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are you aware of the char variable type?

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Problem with fgets !
    By Astra in forum C Programming
    Replies: 6
    Last Post: 11-18-2006, 09:23 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM

Tags for this Thread