letter to upper

This is a discussion on letter to upper within the C++ Programming forums, part of the General Programming Boards category; I don't think it should ... I'm using MSVC++ ... someone else, probably wiser, will know....

  1. #16
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I don't think it should ... I'm using MSVC++ ... someone else, probably wiser, will know.

  2. #17
    Registered User
    Join Date
    May 2006
    Posts
    28
    thank for the help ne way

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Just to confirm: you did press the enter key, right?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Quote Originally Posted by laserlight
    Just to confirm: you did press the enter key, right?


    But, it should work!! There's nothing wrong with it. I've tried it again and again. Did you compile, build and do everythink to it. Maybe it's not happy with the linking. I don't know

  5. #20
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>the first code printed the letter in lower case

    no - it shows whatever you entered on whatever case you
    entered and prints it in upper case on a newline (as a result of
    pressing enter)

    >>second code closed it when i press a letter
    FAQ

    >>Put in your std::cin.ignore(); then

    I think he means a cin.get(); - which may need a cin.ignore();
    before it as cin leaves newlines. He meant this in regard to the
    console box disappearing

    Restate your problem - do you want to type something and see
    it converted to upper case, or do you want to type something
    and see it on screen in upper case AS you type it?

    [EDIT]
    Read laserlights sig on how to ask a question!
    [/EDIT]
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #21
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> [EDIT]
    Read laserlights sig on how to ask a question!
    [/EDIT]

    forgot the good before 'a' and 'question' richie!

  7. #22
    Registered User
    Join Date
    May 2006
    Posts
    41
    Try this:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    void main()
    {
    
    	string inputstring;
    	
    	cin >> inputstring ;
    	string myString(inputstring);
    	strupr((char *) myString.c_str());
    	
    	cout<< myString.c_str();
    
    }

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Shal, hope you realise:

    1. void main() should be int main()
    2. strupr() is non-standard
    3. Using c_str() the way you did is asking for trouble. c_str() returns a const char*, and you really shouldnt be casting away the const-ness like that, and then try to change what should not be changed in that way.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    This is a complete input fetching routine for you, which should do what you want. You will only see capital letters regardless of what the user types and everything is put into a string.

    I also use Dev C++ so it really shouldn't be a portability problem for you.
    Code:
    #include <iostream>
    #include <conio.h>
    #include <cctype>
    #include <string>
    
    int main()
    {
       std::string mystring; 
       char letter;
       std::cout<<"Enter string: ";
       while((letter = getch()) != EOF && letter != '\n' && letter != '\r')
          if(letter == '\b' && mystring.size() > 0) {
               std::cout<<"\b \b";
               mystring.erase(mystring.size()-1);
          }
          else if(std::isalnum(letter) || std::isspace(letter) || std::ispunct(letter)) {
              letter = std::toupper(letter);
              std::cout << letter;
              mystring.append(1, letter);
          }
      
       std::cout<< "\nYou entered [" << mystring << "]\n";
       std::cin.get();
    
       return 0;
    }
    Last edited by whiteflags; 05-24-2006 at 09:23 AM.

  10. #25
    Registered User
    Join Date
    May 2006
    Posts
    41
    Ya, I know its a bit cryptic to do it this way, but as k1ll3r could not compile the previous code so I thot of trying some different way.

    but I surely agree to ur point 2 n 3.

    but I am not convinced when it comes to main!!
    main should return int as per ANSI/ISO standard. But making it void also won't make any difference. The return value would be helpful if I want my start up code to check if main executed successfully or not. Otherwise any ways its gonna be return "0".

  11. #26
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I just do int main() out of habbit, but there is no reason why the code I posted shouldn't have worked I think!

  12. #27
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    void main results in any old junk being returned to the OS - which
    I assume could (possibly) lead it to believe that your program
    crashed - not a big problem for small programs but I imagine that
    larger programs that are more resource dependant it could cause
    issues.

    We still haven't heard from the OP since my lasy post... perhaps
    some feedback from him/her will result in a resolution to this.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  13. #28
    Registered User
    Join Date
    May 2006
    Posts
    28
    wow i didnt expect to see that many replys thanks all--now back to the subject, citizens code worked but its kinda long and hard to understand could it be umm simplified?

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    citizens code worked but its kinda long and hard to understand could it be umm simplified?
    Not by much, though there are a few places where say, empty() should be used instead of checking size().

    Here is a commented version of the program, with a few minor modifications:
    Code:
    #include <iostream>
    #include <cctype>
    #include <string>
    #include <conio.h> // for the non-standard getch() function
    
    int main()
    {
    	std::string mystring; // string entered by user, with backspaces accounted for
    	char letter;          // current character entered by user
    
    	std::cout << "Enter string: ";
    
    	// grab the string character by character
    	// getch() reads in the character without having it printed to screen
    	// terminate when EOF or a newline/carriage return is encountered
    	while ((letter = getch()) != EOF && letter != '\n' && letter != '\r') {
    		// handle backspaces by removing the most recent character
    		// check that the string is not empty, to avoid removing from an empty string
    		if (letter == '\b' && !mystring.empty()) {
    			// remove printed character from screen
    			std::cout << "\b \b";
    			// remove from string
    			mystring.erase(mystring.size() - 1);
    		}
    		// handle printable characters
    		else if (std::isprint(letter)) {
    			// change character to uppercase, if applicable
    			letter = std::toupper(letter);
    			// print character to screen
    			std::cout << letter;
    			// append character to string
    			mystring.push_back(letter);
    		}
    	}
    
    	// print string
    	std::cout<< "\nYou entered [" << mystring << "]\n";
    	std::cin.get();
    	return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Registered User
    Join Date
    May 2006
    Posts
    28
    thanks laserlight!

Page 2 of 3 FirstFirst 123 LastLast
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, 12: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, 01:04 PM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM

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