Thread: letter to upper

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    28

    letter to upper

    hi im trying to make it when the player types a letter it will be uppercase...it compiles and runs but does not convert to uppercase... heres the code:

    Code:
    #include <iostream.h>
    #include <ctype.h>
    int main()
    {
    char letter;
    cout<<"Enter a letter : ";
    cin>>letter;
    letter=toupper(letter);
    std::cin.ignore();
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... why dont you print the letter and have a look?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Ok, I think I see what you mean. But run this:

    Code:
    #include <iostream.h>
    #include <ctype.h>
    
    
    int main()
    {
    	char letter;
    	cout<<"Enter a letter : ";
    	cin>>letter;
    
    	letter=toupper(letter);
    
    	cout<< letter;
    
    	return 0;
    }
    I think what you want is this:
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <ctype.h>
    
    
    int main()
    {
    	char letter;
    	cout<<"Enter a letter : ";
    	letter = getch();		// The user doesn't have to press enter
    
    	letter=toupper(letter);	
    
    	cout<< letter;
    
    	return 0;
    }
    Conio.h is not standard, so this may/maynot work, it does for me tho

    Also, you can do this on a loop easily enough. Just make a string, and in a loop say StringName[i] += letter. You'll have to do something about backspaces I think, so the best way to do this is to read in a whole string at once, and then get a loop to 'toupper' it, if you get me.
    Last edited by twomers; 05-24-2006 at 02:24 AM.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    28
    doesnt work

    the first code printed the letter in lower case and the second code closed it when i press a letter

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by k1ll3r
    doesnt work
    What doesn't?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    doesnt work
    How does it not work? Also what exactly do you want to do? twomers caught on that you might have something else in mind, one that will need something non-standard like conio.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    28
    the first code printed the letter in lower case and the second code closed it when i press a letter

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Put in your

    std::cin.ignore();

    then

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    28
    How does it not work? Also what exactly do you want to do?
    i just want the letter/s i type to appear in capital letter

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    the first code printed the letter in lower case
    That should not happen.

    the second code closed it when i press a letter
    Run the program from the command line, or add something like std::cin.ignore... have you tried a hello world program yet?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    do this so:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
    	string MyString;
    
    	cout<<"Enter a string of letters( no spaces!!): ";
    	cin >> MyString;
    
    	for ( int i=0; i<MyString.length(); i++ )	
    	{
    		MyString[i] = toupper( MyString[i] );
    	}
    
    	cout<< MyString;
    
    	return 0;
    }
    and put that ignore thing there if you want

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    28
    Run the program from the command line, or add something like std::cin.ignore... have you tried a hello world program yet?
    i did try std::cin.ignore(); and i have tried a hello program
    Adding std::cin.ignore(); just makes the first letter i type invisible and then the next letters in lowercase.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    28
    do this so:


    Code:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
    	string MyString;
    
    	cout<<"Enter a letter : ";
    	cin >> MyString;
    
    	for ( int i=0; i<MyString.length(); i++ )	
    	{
    		MyString[i] = toupper( MyString[i] );
    	}
    
    	cout<< MyString;
    
    	return 0;
    }
    that once again is in lowercase

  14. #14
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Not when I run it
    Last edited by twomers; 08-03-2006 at 03:24 PM.

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    28
    maybe it has something to do with the compilers?i have no clue
    btw:im using dev tool c++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting letter occurences in a string
    By pjr5043 in forum C++ Programming
    Replies: 35
    Last Post: 05-05-2008, 09:18 PM
  2. Advice requested, Code makes sense to me, not compiler
    By andrew.bolster in forum C Programming
    Replies: 53
    Last Post: 01-06-2008, 01:44 PM
  3. to lower or to upper that is the question!
    By verbity in forum C++ Programming
    Replies: 20
    Last Post: 04-25-2007, 06:42 PM
  4. Big Letter became small letter
    By cogeek in forum C Programming
    Replies: 27
    Last Post: 12-13-2004, 02:04 PM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM