String to Int function (C++)
This little piece of code can convert a string of decimal characters into an integer value. If you seek to convert a string into a double, see this post.
/*
* A function to convert a string into an integer
*/
#include <string>
int StringToInt (std::string str)
{
int total = 0;
int length = str.length(); // the length of the string
int x = 1; // this is our multiplier, used to convert each digit into tens, units, etc
for (int i = 1; i < str.length(); i++)
x *= 10; // initialise x correctly
for (int i = 0; i < length; i++)
{ // loop through the string
// 48 is the base value (ASCII)
// multiply it by x to get it into tens, units, etc
total += (static_cast <int> (str[i]) - 48) * x;
x /= 10; // divide x by 10, to reduce the units by one
}
return total; // return the value as an integer
}
/** EXAMPLE USAGE **/
#include <iostream>
int main ()
{
int t = StringToInt("1234") + 300;
std::cout < < t; // should be 1534 (1234 + 300 = 1534)
std::cin.get ();
return EXIT_SUCCESS;
}
A Pretend Radio Class (C++)
This C++ class is a good demonstration of Object Orientation in code.
Download the entire code: RadioClass.zip
// definition file for class Radio
#ifndef RADIO_H
#define RADIO_H
#include <iostream>
using namespace std;
const bool FM = true;
const bool AM = false;
const bool ON = true;
const bool OFF = false;
const int MIN_VOLUME = 0;
const int MAX_VOLUME = 11; // it goes to eleven!
class Radio
{
public:
// constructors:
Radio();
Radio(bool b,bool p,double fm,double am,int v);
// accessors:
bool getBand();
bool getPower();
double getFMStation();
double getAMStation();
int getVolume();
// mutators:
bool setBand(bool b);
bool setPower(bool p);
bool setFMStation(double s);
bool setAMStation(double s);
bool setVolume(int v);
// utility functions:
void displayRadio();
private:
bool band; // FM = true, AM = false
bool power; // on = true, off = false
double FMstation , AMstation ; // as per norms
int volume; // 0..11 is the valid range
};
#endif
// Implementation file for class Radio
#include <iostream>
#include "Radio.h"
using namespace std;
// constructors:
Radio::Radio()
{
band = FM;
volume = 4;
power = OFF;
FMstation = 88.1;
AMstation = 530;
}
bool Radio::setFMStation(double s)
{
if((s < 88.1) || (s > 107.9)) // Station's input out of range
return false;
if((int(s * 10) % 2) == 0) // station's .x is even and therefore invalid
return false;
FMstation = s;
return true;
}
bool Radio::setAMStation(double s)
{
if((s < 530) || (s > 1600))
return false;
if((int(s) % 10) != 0) // AMstation not divisible by 10 and therefore invalid
return false;
AMstation = s;
return true;
}
// utility functions:
void Radio::displayRadio()
{
cout << "Band = " <<; ((band == FM) ? "FM":"AM") << endl;
cout << "Power = " << ((power == ON) ? "ON":"OFF") << endl;
cout << "FM Station = " << FMstation << endl;
cout << "AM Station = " << AMstation << endl;
cout << "Volume = " << volume << endl << endl;
}
// mutators:
bool Radio::setBand(bool b)
{
band = b;
return true;
}
bool Radio::setPower(bool p)
{
power = p;
return true;
}
bool Radio::setVolume(int v)
{
if((v < MIN_VOLUME) || (v > MAX_VOLUME))
return false;
volume = v;
return true;
}
//custom constructor , placed later on purpose.
Radio::Radio(bool b,bool p,double fm,double am,int v) // build a radio with custom defaults
{
band = FM;
setBand(b);
power = OFF;
setPower(p);
FMstation = 88.1;
setFMStation(fm);
AMstation = 530;
setAMStation(am);
volume = 4;
setVolume(v);
}
// accessors:
bool Radio::getBand()
{
return band;
}
bool Radio::getPower()
{
return power;
}
double Radio::getFMStation()
{
return FMstation;
}
double Radio::getAMStation()
{
return AMstation;
}
int Radio::getVolume()
{
return volume;
}
// Driver file for class Radio
#include <iostream>
#include "Radio.h"
using namespace std;
//Radio object driver, this file sends the commands that "push the radio buttons" via function calls.
void main()
{
Radio myJamBox; // Build a radio to be referred to as 'myJamBox'.
myJamBox.displayRadio(); // display myJamBox's current attributes.
myJamBox.setAMStation(550); // adjust's myJamBox's AM station to 530
myJamBox.setPower(ON); // flips myJamBox's power swith ON
myJamBox.displayRadio(); // display myJamBox's current attributes.
system("pause");
}
A Calculator Program Made in Visual Basic
Posted by kturley in Visual Basic on February 8, 2010
This simple calculator is a great example for beginners to examine to understand the basics of Visual Basic. I must warn you that this isn’t perfect, but perfecting it would be an excellent exercise for a novice Visual Basic programmer.

- Download: VBcalc.zip
- This download is a Microsoft Visual Studio Solution file, Visual studio is required to open it. A free edition of the Visual studio is available from Microsoft at http://www.microsoft.com/express/Downloads/