Here we will put miscellaneous tool resources, eg in form of practical programming tips for solving ordinary “everyday problems” or more problematic ones. Small manuals of different kind you will find in the right column.
- Concrete programming issues should be posted in the course’s discussion group (Slack). Questions asked by mail, will be forwarded to the forum, if they might be of general interest. Is there an appropriate practical or general solution as an answer, we will put it here, so this page will eventually become a useful FAQ.
This is how you do to…
|Clear screen in a console program|
|Windows have no special API function for this. The easiest way is to call a system function provided by iostream, to execute dos commands. You simply do the following function call, with the current dos-command as a string argument: system(“cls”);… then the console window will be emptied of its contents. A (poorer) alternative is to write a simple function that scrolls the screen:
A similar command is system(“pause”); … which stops the program execution until you press a key.
| Get automatic formatting of your code
|If you have a lot of code to format, eg. after readjustment of code sections, you can benefit from a convenient tool in Visual Studio: Select desired code to adjust and choose in the menu bar: Edit -> Advanced -> Format Selection (alt. Format Document), and code will automatically adapt to the recommended standard (or alternative default setting you selected).|
| Make settings in Visual Studio
|To change default settings in Visual Studio, select menu Tools -> Options and then desired option in left view. See sample image for settings of the editor. Here you can eg. choose whether line numbers should be displayed or how to auto-format code:
Project-specific settings, such as file paths for VC++ Directories or choice of character encoding (eg. Unicode Character Set) to be applied, is made on the project’s Properties page: Right click on the project name in Solution Explorer view and select Properties at the bottom of the list that appears.
|Convert a digit in a string to a number|
|Digits in a string are stored with its ASCII code. For example, 0, 1, 2 the are stored as 48, 49, 50. If you want to make calculations with the numbers, they must first be converted from ASCII to numbers. For string: str = “43″; …for instance the second digit can be calculated this way: digit2 = str – ’0′; // dvs 51-48 = 3|
|Erase one character in a c-string|
|Delete 3rd character in the string ‘str’:
Here copied string str+3=”le” one step to the left. Print is now “Kale”. 3rd character is erased!
|Initialize an array|
|A globally defined array is automatically initialized to 0 in all elements.
An array can also be initiated by the definition:
|To use “and”, “or” and “not” in Visual Studio|
|According to recent standards, we can use “and”, “or” and “not” instead of &&, | | and!. To allow this in Visual C++, you include the library: