Wednesday, March 16, 2011

Program for Currency Conversion

Program that converts the older Pound money system to the latest one.
#include
using namespace std;
void main()
{
int shilling=0,pence=0,pounds=0;

cout<<"Enter Pounds: "; cin>>pounds;
cout<<"Enter Shillings: "; cin>>shilling;
cout<<"Enter Pence: "; cin>>pence;

float ans=(float)pounds + (float)shilling/20 + ((float)pence/12)/20;
cout<<"Conversion yields: "<
}

Tuesday, March 15, 2011

Program that takes input in Celsius and converts to fahrenheit

This is a beginner program which prompts user to input temperature in Celsius and outputs the temperature in Fahrenheit.


#include
using namespace std;
void main()
{
float fahrenheit=0.0, celsius=0.0;
cout<<"Enter Teperature in Celsius: "; cin>>celsius;
fahrenheit = ((celsius + 32)*9)/5;
cout<<"\nIn Fahrenheit: "<< fahrenheit;

}

Monday, March 14, 2011

C++ program that converts Dollars in Yen,Pounds,Francs etc

This is a beginner question, write a program that takes input in Dollars and converts them in Pounds, Yen etc and displays it. Here is how I have written it

#include
using namespace std;
void main()
{
float number=0.0;
cout<<"Enter Amount in Dollars: "; cin>>number;
cout<<"Conversion in:\nPounds: "< cout<<"\nDeutshemark: "< }

Monday, March 7, 2011

Writing, Deploying and Consuming a Web service in .NET

Suppose you want to write a web service that can send email and consume or use this web service in you C# application.
First off with writing the web service. For a detailed and basic version of the tutorial on how to write a web service, you can visit the following link.

Tutorial: Writing a web service

So we'll start by writing a web method that can get email address of the receiver, Subject of the email and Message as parameters and returns a message whether the email has been sent or not.
Here is what we'll need for the implementation of such a method.

[WebMethod]
public string sendMail(string mailTo, string emailSubject, string emailBody) {
MailMessage nmail = new MailMessage(); //Create a new mail message
nmail.From = new MailAddress(/*Your email address*/); //Set Sender's email address
nmail.To.Add(mailTo); //Set Recipient's Email address
//Note: you can add multiple Recipients
nmail.Subject = emailSubject; //Set subject of the email
nmail.Body = emailBody; //Set the body of the email
SmtpClient smtpClient = new SmtpClient(/*"127.0.0.1" or localhost or the IP or your server */); //Initiate SMTP client for your Server
try{
smtpClient.Send(nmail); //Send Email
}
catch(Exception e)
{
return “Error Occured”;
}
return "Mail Sent";
}

Also you will need the following dlls at the start of your code to make the above code executable

using System.Net.Mail;
using System.Web.Services.Protocols;

After this compile your code and see if it works fine, next step is to deploy your web service. Following tutorial describes how to deploy your web service.

Tutorial: How to Deploy your web service

Now once you have deployed your web service by the use of above tutorial. Next step is to consume it using your C# Forms application. So now open a new Visual C# Windows Application project. Make the form design as an email form with the labels of to, subject, message and a button which when pressed tries to send the email. My form looks like this.

Now you have to add the web reference to your application as shown in the figure below.

Right-Click References in the Solution Explorer and click Add references.Click on the link web service on the Local machine. This will display all the web services that are currently deployed on your IIS.


Select the one you just made and give it a reference name. In my case, I have given it a name “serv”. Click add reference.

It will appear under your refernces as

Now to the handler of the send button, you will have to initialize your service with the name you gave while adding reference. Like this

serv.Service snd = new sendemail.serv.Service();
Form the object snd you can call all the methods that are tagged as [web method]
String response=snd.sendmail(textBox1.Text, "", textBox3.Text, textBox4.Text);

You can show the string message returned by the service in the message box as follows

MessageBox.Show(response);