I don't think it should ... I'm using MSVC++ ... someone else, probably wiser, will know.
I don't think it should ... I'm using MSVC++ ... someone else, probably wiser, will know.
thank for the help ne way
Just to confirm: you did press the enter key, right?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Originally Posted by laserlight
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
>>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
>> [EDIT]
Read laserlights sig on how to ask a question!
[/EDIT]
forgot the good before 'a' and 'question' richie!
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(); }
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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".
I just do int main() out of habbit, but there is no reason why the code I posted shouldn't have worked I think!
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
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?
Not by much, though there are a few places where say, empty() should be used instead of checking size().citizens code worked but its kinda long and hard to understand could it be umm simplified?
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; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
thanks laserlight!