C++ notes
// Description: template for trivial programs as well as a body of knowledge, tool functions, and nomenclature
// created 10 9 1
// updated 10 9 5 ( originals good and commented )
10 9 7 ( originals corrected for errors )
10 9 10 ( more minor stuff )
10 9 21 ( made section headers )
10 9 27 ( i / o & string stuff )
#include<iostream>
#include<string>
using namespace std;
most basic {
long num1(0L), num2(0L), num3(0L), num4(0L);
// scope
int count1 = 10;
<< "Value of outer count1 = " << count1;
{ // New scope starts here...
int count1 = 20; // This hides the outer count1; affects globals too
int count2 = 30;
cout << "Value of inner count1 = " << count1;
} // ...and ends here
cout << "Value of outer count1 = " << count1;
// cout << count2 << endl; // uncomment to get an error
// namespace
}
function templates {
void main( void )
{
bool doAgain;
do
{
// the program
doAgain = getBool( "\nAgain? y/n -- ");
}
while ( doAgain );
return 0;
}
#ifndef unsorted_list_h
#define unsorted_list_h
{class stuff}
#endif
void holdScreen( string promptForInput );
/* void holdScreen( string promptForInput ) ---- ---- ---- ----
pre: from [ whatever ] , receives a prompt to make the user press enter
// ---- ---- ---- ---- ---- ---- ---- ----*/
void holdScreen( string promptForInput )
{
cout << promptForInput;
cin.ignore( 99,'\n' );
}
}
getDataType {
#include<iostream>
#include<string>
bool getBool(string promptForInput ); // single letter switch, perhaps with cin.get( )
bool getBool( string promptForInput ); // toLowers a string, permitting 'yes'
bool getBool( string promptForInput, string option1, string option2 ); // offers any two options I define
char getChar( string promptForInput );
char getBoundedChar( string promptForInput, char minValue, char maxValue ); // obviously only on ascii with consecutive letters
int getInt( string promptForInput );
int getBoundedInt( string Prompt, int minValue, int maxValue );
/* bool getBool( stringpromptForInput ) ---- ---- ---- ----
pre: from [ whatever ] , standard tool, utilizing getChar (which handles the prompt) for a y/n
post: to [ whatever ] , gives a valid bool
// ---- ---- ---- ---- ---- ---- ---- ----*/
bool getBool( string promptForInput ) // char driven switch, perhaps with cin.get()
{
do
{
switch ( getChar( promptForInput ) )
{
case 'y':
case 'Y':
return true;
case 'n':
case 'N':
return false;
}
cout << "Please enter y or n." << endl;
} while ( true );
}
/* bool getBool( stringpromptForInput ) ---- ---- ---- ----
pre: from [ whatever ] , standard tool, passing a prompt down
post: to [ whatever ] , returns a bool from the lowered string from toLower & getline
getBool( "" );
// ---- ---- ---- ---- ---- ---- ---- ----*/
bool getBool( string promptForInput ) // toLowers a string, permitting 'yes'
{
do
{
string userResponse = toLower( getLine( promptForInput ) );
if ( userResponse == "y" || userResponse == "yes" )
return true;
if ( userResponse == "n" || userResponse == "no" )
return false;
cout << "Please enter yes or no." << endl;
} while ( true );
}
/* bool getBool( stringpromptForInput, string option1, string option2 ) ---- ---- ---- ----
pre: from [ whatever ] , passes a prompt down and takes lowercased exclusive responses or their first letter
post: to [ whatever ] , returns a bool selecting either option but CautioN about the order
// ---- ---- ---- ---- ---- ---- ---- ----*/
bool getBool( string promptForInput, string option1, string option2 )
{
do
{
string userResponse = toLower( getLine( promptForInput ) );
if ( userResponse == option1.substr(0,1) || userResponse == option1 )
return true;
if ( userResponse == option2.substr(0,1) || userResponse == option2 )
return false;
cout << "Please enter " << option1 << " or " << option2 << "-- ";
} while ( true );
}
/* char getChar( string promptForInput ) ---- ---- ---- ----
pre: from [ whatever ] , standard tool deposits prompt onto the screen
post: to [ whatever ] , gives any valid char
// ---- ---- ---- ---- ---- ---- ---- ----*/
char getChar( string promptForInput )
{
do
{
cout << promptForInput;
char userInput;
cin >> userInput;
if ( cin )
{
cin.ignore( 99, '\n' );
return userInput;
}
cout << "\nNot a char\nTry again --" << endl;
cin.clear( );
cin.ignore( 99, '\n' );
} while ( true );
}
// NEED commentary stuff
char getBoundedChar( string promptForInput, char minValue, char maxValue )
{
// in case of foolish arguments
if ( minValue > maxValue )
{
int temp = minValue;
minValue = maxValue;
maxValue = temp;
}
do
{
char userInput = getChar( promptForInput );
if ( userInput >= minValue && userInput <= maxValue )
return userInput;
cout << "Choose a character between " << minValue << " and " << maxValue << "." << endl;
} while ( true );
}
/* int getBoundedInt( string Prompt, int minValue, int maxValue ) ---- ---- ---- ----
pre: from [ whatever ] , receiving the range values to limit the int returned
post: good Ints go back to [ whatever ] , returns an int within the range
// ---- ---- ---- ---- ---- ---- ---- ----*/
int getBoundedInt( string promptForInput, int minValue, int maxValue )
{
// in case of foolish arguments
if ( minValue > maxValue )
{
int temp = minValue;
minValue = maxValue;
maxValue = temp;
}
do
{
int userInput = getInt( promptForInput );
if ( userInput >= minValue && userInput <= maxValue )
return userInput;
cout << "Value must be from " << minValue << " to " << maxValue << "." << endl;
} while ( true );
}
/* int getInt( string promptForInput ) ---- ---- ---- ----
pre: standard tool; receives a string prompt from boundedInt
post: provides a legitimate int back to boundedInt
// ---- ---- ---- ---- ---- ---- ---- ----*/
int getInt( string promptForInput ) // short, int, long // float, double, long Int
{
do
{
int userResponse;
cout << promptForInput;
cin >> userResponse;
if ( cin )
{
cin.ignore( 99, '\n' );
return userResponse;
}
cin.clear( );
cin.ignore( 99, '\n' );
cout << "\nNot an integer\nTry again --";
} while ( true );
}
}
string stuff {
#include<string>
string toLower(string s);
string getLine(string promptForInput );
int toInt( string numberString );
/* string getLine(string promptForInput ) ---- ---- ---- ----
pre: from [ whatever ] , receives a string prompt from [ whatever ] for the screen
post: provides a string to [ whatever ]
// ---- ---- ---- ---- ---- ---- ---- ----*/
string getLine(string promptForInput )
{
cout << promptForInput;
string userResponse;
getline( cin, userResponse );
return userResponse;
}
/* string toLower( string s) ---- ---- ---- ----
pre: from [ whatever ] , receives a string prompt to lower case
post: provides a lower case string to [ whatever ]
// ---- ---- ---- ---- ---- ---- ---- ----*/
string toLower( string s)
{
const string lower = "qwertyuioplkjhgfdsazxcvbnm";
const string upper = "QWERTYUIOPLKJHGFDSAZXCVBNM";
for ( string::size_type index = 0 ; index < s.length( ) ; ++index )
{
string::size_type location = upper.find( s[ index ] );
if ( location != string::npos )
s[ index ] = lower[ location ];
}
return s;
}
string toLower( string UU )
{
int alphaDist = 'z' - 'Z' ;
for ( string::size_type I = 0; I < UU.length( ) ; I++ )
{
char letter = UU[ I ];
if ( letter >= 'A' && letter <= 'Z' )
UU[ I ] = ( alphaDist + letter );
}
return UU;
}
string toLoooower( string nn )
{
const short TABLE_SIZE = 255;
char asciiLower[ TABLE_SIZE ];
int step = 0;
char anyChar = ' '; // serves as upper limits for step to test against
// first simulate the wonky ascii, but without the danger
while ( step < anyChar )
{
asciiLower[ step ] = '?';
step++;
}
// then everything before the uppercase letters
while ( step < 'A' )
{
asciiLower[ step ] = anyChar;
anyChar++;
step++;
}
anyChar = 'a';
// make all of them lower case
while ( step <= 'Z' )
{
asciiLower[ step ] = anyChar;
anyChar++;
step++;
}
anyChar = 'Z' + 1;
// and now everything else
while ( step < TABLE_SIZE )
{
asciiLower[ step ] = anyChar;
anyChar++;
step++;
}
// this would obviously be elsewhere
string::size_type nnLength = nn.length( );
char currentChar;
for ( string::size_type I = 0; I < nnLength; I++ )
{
currentChar = nn[ I ];
nn[ I ] = asciiLower[ currentChar ];
}
return nn;
}
/* int string2Int( string numberString ) ---- ---- ---- ----
pre: receives a string with numbers from [ whatever ]
post: provides an integer to [ whatever ]
// ---- ---- ---- ---- ---- ---- ---- ----*/
int string2Int( string numberString ) // atoi
{
bool negative = numberString[ 0 ] == '-'; // ie if first char is -, then it's neg
int result = 0;
for ( string::size_type index = (negative?1:0); index < numberString.length( ); ++index )
{
char digitChar = numberString[index];
int digitValue = digitChar-'0';
result = 10*result + digitValue;
}
if ( negative )
result = -result;
return result;
}
}
file i/o {
#include <fstream>
fstream ; ifstream ; ofstream fileName;
fileName.open( "filePath" );
//fileName >> var;
//or: getline( fileName, stringVar );
fileName.close( );
//in the library
{
// file exist?
fileName.fail( ); // returns true when it fails
!fileName
fileName.is_open( );
}
void charsFromFile( char nn[ ] );
void outputToFile( char nn[ ] );
/* void charsFromFile( char nn [ ] ) ---- ---- ---- ----
pre: from [ whatever ] , references array nn [ ] and receives ARRAY_SIZE as a global variable
post: referenced array nn [ ] now has the contents of DojunkChars.txt assuming no overflow
// ---- ---- ---- ---- ---- ---- ---- ----*/
void charsFromFile( char nn [ ] ) // not the form that Hester suggested
{
// int ARRAY_SIZE = ?
ifstream fileVar;
fileVar.open( "DojunkChars.txt" );
/*
if ( !fileVar )
{
ofstream fileCreation("DojunkChars.txt");
fileCreation.close( );
cout << " I had to create the file but it's blank"
<< "\n Let's fill it now";
enterChars( firstArrayEvar );
}
else
*/
{
for ( int i = 0 ; i < ARRAY_SIZE ; i++ )
{
fileVar >> nn [ i ];
}
fileVar.close( );
}
}
/* void outputToFile( char nn [ ] ) ---- ---- ---- ----
pre: from [ whatever ] , references array nn [ ] and receives ARRAY_SIZE as a global variable
post: DojunkChars.txt now has the contents of referenced array nn [ ] separated by spaces only
// ---- ---- ---- ---- ---- ---- ---- ----*/
void outputToFile( char nn [ ] )
{
// int ARRAY_SIZE = ?
ofstream outputFile;
outputFile.open( "DojunkChars.txt" );
for ( int i = 0 ; i < ARRAY_SIZE ; i++ )
{
outputFile << nn [ i ] << ' '; // the space makes it readable by the ifstream later on
}
outputFile.close( );
}
}
randomGens {
#include<time.h> // rand functions
void initRandom( void );
int random( int low, int high );
/* void initRandom( void ) ---- ---- ---- ----
pre: from [ whatever ] , seeds srand
post: srand now seeded
// ---- ---- ---- ---- ---- ---- ---- ----*/
void initRandom( void )
{
srand( unsigned( time( 0 ) ) );
}
/* int random( int low, int high ) ---- ---- ---- ----
pre: from [ whatever ] , receives bounds as well as uses global int RAND_MAX
post: provides a pseudo-random integer
// ---- ---- ---- ---- ---- ---- ---- ----*/
int random( int low, int high )
{
// int RAND_MAX = ?
return low + int( ( high - low + 1 ) * ( rand( ) / ( int( RAND_MAX ) + 1 ) ) );
}
/* int random( int low, int high ) ---- ---- ---- ----
pre: from [ whatever ] , receives initial values from the prototype for a struct
post: now initialized
// ---- ---- ---- ---- ---- ---- ---- ----*/
}
visual {
// console
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
// "std::" because he avoided namespace std;
std::cout << "Hello world!\n";
return 0;
}
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
Console::ReadLine();
return 0;
}
}