İlk önce örnek bir program yazalım.
#include <iostream>
using namespace std;
int main()
{
int x,y,top,carp,cik,bol;
cout << "x sayisini gir..:";
cin >> x;
cout << "y sayisini gir..:";
cin >> y;
top=x+y;
carp=x*y;
cik=x-y;
bol=x/y;
cout << endl <<"X degeri" << "Y degeri" << "Sonuc" << endl;
cout << x << " + " << y << " = " << top << endl;
cout << x << " - " << y << " = " << cik << endl;
cout << x << " * " << y << " = " << carp << endl;
cout << x << " / " << y << " = " << bol << endl;
system("pause");
return 0;
}
using namespace std;
int main()
{
int x,y,top,carp,cik,bol;
cout << "x sayisini gir..:";
cin >> x;
cout << "y sayisini gir..:";
cin >> y;
top=x+y;
carp=x*y;
cik=x-y;
bol=x/y;
cout << endl <<"X degeri" << "Y degeri" << "Sonuc" << endl;
cout << x << " + " << y << " = " << top << endl;
cout << x << " - " << y << " = " << cik << endl;
cout << x << " * " << y << " = " << carp << endl;
cout << x << " / " << y << " = " << bol << endl;
system("pause");
return 0;
}
Yukarıdaki yazdığımız program, kullanıcının istediği iki sayıyı girerek toplam, fark, çarpım ve bölüm sonuçlarını ekranda gösterir. Programı çalıştırdığımızda ise karşımıza şöyle bir görüntü çıkar.
Karşımıza çıkan bu görüntü karışık ve hoş bir görüntü vermemekte. Ancak setw() fonksiyonunu kullanarak daha düzenli ve iyi bir görüntü oluşturabiliriz. Setw() fonksiyonunu kullanabilmemiz için ilk önce gerekli kütüphaneyi programa dahil etmeliyiz. Aşağıdaki bulunan kodu programa yazarak kütüphanemizi dahil edebiliriz.
#include <iomanip>
Aşağıdaki kodlar yazdığımız programın setw() eklenmiş halidir.. Setw kullanımı ise aşağıdeki kodu inceleyerek anlayabiliriz.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int x,y,top,carp,cik,bol;
cout << "x sayisini gir..:";
cin >> x;
cout << "y sayisini gir..:";
cin >> y;
top=x+y;
carp=x*y;
cik=x-y;
bol=x/y;
cout << endl <<"X degeri"<<setw(20) << "Y degeri"<<setw(10) << "Sonuc" << endl;
cout << x <<setw(10)<< " + " <<setw(10)<< y << " = " <<setw(10)<< top << endl;
cout << x <<setw(10)<< " - " <<setw(10)<< y << " = " <<setw(10)<< cik << endl;
cout << x <<setw(10)<< " * " <<setw(10)<< y << " = " <<setw(10)<< carp << endl;
cout << x <<setw(10)<< " / " <<setw(10)<< y << " = " <<setw(10)<< bol << endl;
system("pause");
return 0;
}
#include <iomanip>
using namespace std;
int main()
{
int x,y,top,carp,cik,bol;
cout << "x sayisini gir..:";
cin >> x;
cout << "y sayisini gir..:";
cin >> y;
top=x+y;
carp=x*y;
cik=x-y;
bol=x/y;
cout << endl <<"X degeri"<<setw(20) << "Y degeri"<<setw(10) << "Sonuc" << endl;
cout << x <<setw(10)<< " + " <<setw(10)<< y << " = " <<setw(10)<< top << endl;
cout << x <<setw(10)<< " - " <<setw(10)<< y << " = " <<setw(10)<< cik << endl;
cout << x <<setw(10)<< " * " <<setw(10)<< y << " = " <<setw(10)<< carp << endl;
cout << x <<setw(10)<< " / " <<setw(10)<< y << " = " <<setw(10)<< bol << endl;
system("pause");
return 0;
}
Programın çalıştırdığımızda ise aşağıdaki görüntüyü elde ederiz.
Herhangi bir sorunuz veya sorununuz olursa aşağıdan yorum atarak veya iletişim bölümünden mesaj atarsanız yardımcı olmaya çalışırım.