C++

Technical Notes

These notes are my personal online notebook of useful commands and "how-to's". You are welcome to make use of them if you find them helpful. They obviously don't come with any warranty! Click on one of the category tags above for the notes in any category.

Convert Roman numerals to decimal integers in C++

// Converts Roman numerals to decimal integers.
// This code is very resilient to non-standard or variant forms, but does not handle
// exotic forms such as fractions (S etc.), the overstrike x1000 operator or unicode numerals.
// Andrew Fountain, released under GPL 3.0 or higher
#define ID "$Id: roman_num.cpp 46 2012-08-03 18:24:25Z andrewfn $"
#include <iostream>
#include <string.h>
using namespace std;

/* Helper function for romChars()
 * return the value of a Roman character, or zero if not a valid character
 */
int romCharVal(char ch) {// C    D  E F G H  I  J K   L     M

C++ string handling

Read a line from a file: ( http://www.cplusplus.com/forum/beginner/8388/ )

#include < iostream>
#include < fstream>
#include < string>
using namespace std;

void main ()
{
        string STRING;
	ifstream infile;
	infile.open ("names.txt");
        while(!infile.eof) // To get you all the lines.
        {
	        getline(infile,STRING); // Saves the line in STRING.
	        cout << STRING; // Prints our STRING.
        }
	infile.close();
	system ("pause");
}

Alternately we can open the file with: ( http://www.cplusplus.com/doc/tutorial/files/ )

C++ Sample Code

#include <iostream>
using namespace std;

void f(const char *g);
void f(void);

void f(const char *g) { cout << "Hello, " << g << "!" << endl; }
void f(void) { cout << "Welcome to Paradise!" << endl; }

int main(int argc, char *argv[])
{
	f("Student");
	f();
	f(argv[1]);
	return 0;
}

C++ Notes

Syndicate content