如何在C++中读取值并输出值

How to read the value and output the value in C++

本文关键字:输出 读取 C++      更新时间:2023-10-16

首先,我对C++非常陌生。我的代码有问题。程序应该能够获取/读取用户输入的值并将其输出回来。我尝试了获取/设置C++方法,但在获取和输出main中的值时遇到了一些问题。以下是我的代码,

    #include <iostream>
    using namespace std;
    class Store {
    public:
        //get and Set Price
        void setPrice(int x){
            price = x;
        }
        int getPrice(){
            return price;
        }
        //Get and set % Marked Up
        void setPercentageMarkedUp(int y){
            markedUpPrice = y;
        }
        int getPercentageMarkedUp(){
            return markedUpPrice;
        }
        //Get and set percentage Sales tax
        void setPercentageSalesTax(int y){
            percSalesTax = y;
        }
        int getPercentageSalesTax(){
            return percSalesTax;
        }
    private:
        int price;
        int markedUpPrice;
        int percSalesTax;
    };
    int main(){
        int price;
        Store obj;
        cout << "enter the Original Price of the item: "<<endl;
        obj.getPrice();
        cout<<"the value is:"<<price<<endl;
        return 0;
}

由于我对C++和StackOverflow都很陌生,请不要因为我问这个简单的问题而降级。我知道它很基本。一定会感谢那些提供帮助的人。提前谢谢。

只需要如下更新主

int main(){
    Store obj;
    int n_price;
    cout << " Enter Original Price: " << endl;
    cin >> n_price;
    obj.setPrice(n_price);
    cout << "Original Price: " << n_price<< endl;
    return 0;
}

谢谢大家。:)