CodeBlanks Logo Icon CodeBlanks

Sepeti listeleme

Sepetteki ürünleri formatlı yazdıran fonksiyonu yazarsın.

Sepeti listeleme

Sepetteki ürünleri ekrana yazdırmak için printCart fonksiyonunu yazacağız. Bu fonksiyon items vektörünü gezer ve her ürünün ad, fiyat ve miktarını bir satıra yazar.

Range-based for ile her ürünü gezersin; her yinelemede std::cout << p.name << " " << p.price << " " << p.quantity << std::endl; ile bir satır basarsın. Fonksiyon üyeleri değiştirmediği için sonuna const eklemek iyi bir alışkanlıktır; bu, derleyicinin yanlışlıkla değişiklik yapmanı önler.

Formatlı çıktı

printCart'ta her ürün tek satırda yazılırken alanlar arasında boşlık kullanılır. std::cout << a << " " << b yazımı a ve b'yi bir boşlukla ayırır. std::endl ise imleci bir sonraki satıra geçirir.

Biçimlendirme seçimi, çıktının okunabilirliğini belirler. Sütun hizalaması gerekirse <iomanip> başlığındaki std::setw kullanılır; bu projede yalnızca boşlukla ayıracağız. Üç alanı tek satırda yazmak, sepetin hızlıca gözden geçirilmesini sağlar.

Uygulama printCart fonksiyonunu yaz

Cart yapısına bir printCart fonksiyonu ekle. İmzası void printCart() const olmalı. Gövdede range-based for ile her ürünü gez ve std::cout << p.name << " " << p.price << " " << p.quantity << std::endl; ile yazdır. main iki ürün ekleyip printCart'ı çağırıyor. Çıktı iki satır olmalı: Defter 25 2 ve Kalem 10 3.

Başlangıç kodu

main.cpp
#include <iostream>
#include <string>
#include <vector>

struct Product {
    std::string name;
    double price;
    int quantity;
};

struct Cart {
    std::vector<Product> items;
    
    void addItem(Product p) {
        items.push_back(p);
    }
    
    double getSubtotal() {
        double sum = 0;
        for (const auto& p : items) {
            sum += p.price * p.quantity;
        }
        return sum;
    }
};

int main() {
    Cart cart;
    Product p1;
    p1.name = "Defter";
    p1.price = 25.0;
    p1.quantity = 2;
    cart.addItem(p1);
    Product p2;
    p2.name = "Kalem";
    p2.price = 10.0;
    p2.quantity = 3;
    cart.addItem(p2);
    cart.printCart();
    return 0;
}

Örnek çözüm

main.cpp
#include <iostream>
#include <string>
#include <vector>

struct Product {
    std::string name;
    double price;
    int quantity;
};

struct Cart {
    std::vector<Product> items;
    
    void addItem(Product p) {
        items.push_back(p);
    }
    
    double getSubtotal() {
        double sum = 0;
        for (const auto& p : items) {
            sum += p.price * p.quantity;
        }
        return sum;
    }
    
    void printCart() const {
        for (const auto& p : items) {
            std::cout << p.name << " " << p.price << " " << p.quantity << std::endl;
        }
    }
};

int main() {
    Cart cart;
    Product p1;
    p1.name = "Defter";
    p1.price = 25.0;
    p1.quantity = 2;
    cart.addItem(p1);
    Product p2;
    p2.name = "Kalem";
    p2.price = 10.0;
    p2.quantity = 3;
    cart.addItem(p2);
    cart.printCart();
    return 0;
}

Beklenen çıktı

Defter 25 2
Kalem 10 3

Soru ve cevaplar

Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.

1 printCart ne yapar?

main.cpp
cart.printCart();
  • Sepete ürün ekler
  • Toplamı hesaplar
  • Sepetten ürün çıkarır
  • Sepeti ekrana yazar Doğru cevap

Açıklama: printCart sepetteki ürünleri ekrana yazdırır.

2 Format parçalarını eşle

Kavram Anlamı
p.name Ürün adı
p.price Birim fiyat
p.quantity Miktar
std::endl Satır sonu

3 printCart gövdesini yaz

Soru
void printCart() const {
for (const auto& p : items) {
    std::cout << ____ << " " << p.price << " " << p.quantity << std::endl;
}
}
Cevap
void printCart() const {
for (const auto& p : items) {
    std::cout << p.name << " " << p.price << " " << p.quantity << std::endl;
}
}

Açıklama: p.name satırın başında yazılır.

Başlangıç bölümündeki tüm dersler