Wednesday, April 29, 2009

Convert Std String to Integer in C++

In C++ there are no methods/functions to convert std string to integer. But, by using C standard library (cstdlib or stdlib.h) we can convert char* (C string) to integer using atoi function. We can also convert string to char* (C string) using c_str function in string class. Thus, we can convert string to integer via char*. This is the function (I call as stoi) and the example:

#include <iostream>
#include <cstdlib>

using namespace std;

int stoi(string _str) {
 return atoi(_str.c_str());
}

int main() {
 string current_year = "2009";
 string birth_year = "1987";
 int age = stoi(current_year) - stoi(birth_year);
 cout << "Age = " << age << " years old" << endl;
 
 return 0;
}

Output:

Age = 22 years old

Monday, April 20, 2009

Show Yahoo! Messenger Status On Website

This code contains 2 main parts. First, an image to show the status (<img> tag). And the second, a hyperlink to chat with the account (<a> tag). There are 2 method of chat. Using Yahoo! Messenger client program (desktop version) or using Yahoo! Webmessenger (web version). This is the basic code:

  • Desktop Version
    Preview:

    Code (to copy it, click the view source icon):
    <a href="ymsgr:sendim?YAHOO-ID"><img src="http://opi.yahoo.com/online?u=YAHOO-ID&m=g&t=2" border="0" /></a>
    NOTE: don't forget to replace the YAHOO-ID with your Yahoo! ID.
  • Web Version
    Preview:

    Code (to copy it, click the view source icon):
    <a href="http://messenger.yahoo.com/edit/send/?.target=YAHOO-ID"><img src="http://opi.yahoo.com/online?u=YAHOO-ID&m=g&t=2" border="0" /></a>
    NOTE: don't forget to replace the YAHOO-ID with your Yahoo! ID.

The image we can use not only the one appear at above. Absolutely, there are 25 images we can use. To change the image, just change the value of parameter t in the src of img code/tag. These are the values of t and the images associated with:

tOffline ImageOnline Image
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24