Basically, the user console is a text only interface.
Typically, Windows applications don't use this interface, preferring the graphical GUI instead. However, basic high school C++ classes use only the text-only user console. Thus the problem.
All C console applications start by calling the main() function which is passed the number of command line arguments and an array of argument strings.
int main(int argc, char **argv) { // argc is the number of arguments - >=1 // argv[0] is the name of the executable // argv[1] thru argv[argc] are the arguments return 0; // Required, zero means no error // value can be tested by bat file or calling routine }
conio.h
getch() Reads each character as you type it putch() Writes one character gotoxy() Sets where the next character will be written color() Allows red, blue ... used to highlight menu selectionsTogether, these commands allow you to create menus where you can use the arrow keys to move a cursor and the enter key to select an option.
The stream method does NOT allow this flexibility. Instead you need to use ANSI terminal commands which are not supported everywhere. (Under DOS, Ansi.sys has to be loaded in config.sys.)
streams.h
Unfortunately, this is the only form of I/O taught in modern AP Computer Science classes.
#include <iostream> cout << someString << "a string constant" << anInt << endl;The endl constant is a nice touch providing a system independent way of moving to the next line.
cin >> a;One problem with cin is that the first time that it is used, it moves the cursor to the next line.
C++ Builder
Here is a basic program which uses stream I/O - 143 Kb.
#pragma hdrstop #include <condefs.h> // For all console applications #include <iostream.h> // For cout #include <conio.h> // For getch //--------------------------------------------------------------------------- #pragma argsused int main(int argc, char **argv) { cout << "Any string"; getch(); // This pauses the program so you return 0; // can read the output }
#pragma hdrstop #include <condefs.h> // For all console applications #include <iostream.h> // For cout #include <conio.h> // For getch //--------------------------------------------------------------------------- #pragma argsused int main(int argc, char **argv) { int c; do { c = getch(); if (c==0) // Zero means extended character { c = getch(); // Read the second byte printf("DOS extended character 00 %02X\n", c); } else { printf("Simple ASCII character %02X\n", c); } } while (c^=0x1b); getch(); return 0; }
Visual C++
When used with cin, the getline function that is supplied in the string library (VC++ 6.0) contains a design problem - it requires the user to hit enter twice. It also places a null string in the input buffer (ie, you have to test for and remove extra null strings). Microsoft Knowledge Base Article - 240015
getline(cin,user_response); // This has a compiler design problemThis can be fixed by editing line 165 of the file: