Some Win32 (unmanaged) C++ noob tips

This weekend I decided to do a little unmanaged C++

My biggest take-away was that I learned that unmanaged (aka Win32) C++ is really, really hard and obtuse.  As a C# guy (who used to be a VB6 programmer), I don't think that I appreciated how easy C# and VB6 are to work with. 

Here are two things that I tried to do that were hard to figure out: Read a single character from the console and read a line from the console.  Reading a single character is helpful for writing a "press any key to continue..." feature.  Reading a line from the console is helpful for -- well -- reading a line of input from the user. 

To read a single character from the console:

  1. Open up your .cpp file
  2. Add the following line somewhere near the top of the file:

    #include "conio.h"
  3. Read the single character with this line:

    int iKeyPress = _getch();

To read a line from the console:

  1. Open up your .cpp file
  2. Add the following lines somewhere near the top of the file:

    #include <iostream>
    #include <string>
  3. Read the line from the console:

    string message;
    getline(cin, message, '\n');

If you want to get really creative, you can even prompt the user for what you want to do by using "printf" to put a message onto the console.

printf("Type in your name then press ENTER: ");
string message;
getline(cin, message, '\n');

 

-Ben

posted @ Sunday, June 22, 2008 11:08 AM

Print

Comments on this entry:

# re: Some Win32 (unmanaged) C++ noob tips

Left by Remi Gillig at 6/23/2008 6:49 AM
Gravatar
Using proper C++ (aka STL) :

char ch;
std::cin >> ch;
std::cout << "You entered the character '" << ch << "'\n";

std::string str;
std::cin >> str;
std::cout << "You entered the string '" << str << "'\n";

# re: Some Win32 (unmanaged) C++ noob tips

Left by Peter at 7/8/2008 7:33 PM
Gravatar
The only problem with the "proper" C++ is that std::cin will give errors like crazy if you try to put multiple words. So the way he did it was right, except for the printf should be std::cout but the rest is proper

Your comment:



 (will not be displayed)


 
 
 
Please add 6 and 5 and type the answer here:
 

Live Comment Preview:

 
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910