I am trying to create a program to calculate the price of the car. The loop user input twice. But I keep getting error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>

using namespace std;
class AClass
{
    public:
    int MyNum;
    string MyCarBrand;
}; // for the class assign

int main()
{
    int brand, model, x, discount, netprice, price;
    cout<<"Please Enter Your Choice !!!"<<endl;
    AClass MyCar;
    MyCar.MyNum="Brand 1: ";
    cout<<MyCar.MyNum<<endl;
    MyCar.MyCarBrand="Model : "
    cout<<MyCar.MyCarBrand<<endl;
    cout<<"Year :";
    cin>>x;
    
 // Using else if to created option for user to choose
    if(x>2020)
    {
        cout<<"Original Price:"<<endl;
        cin>>price;
        cout<<"WELCOME TO PANDU CEPAT AUTO"<<endl;
        cout<<"For model"<<endl;
        cout<<"There is no discount for this model. Sorry !";
        
    }
    else if(x>=2018 && x<=2020)
    {
        cout<<"Original Price : RM ";
        cin>>price;
        cout<<"WELCOME TO PANDU CEPAT AUTO !"<<endl;
        cout<<"For model"<<x<<endl;
        discount=price*10/100;
        cout<<"Discount 10% :"<<discount<<endl;
        netprice=price-(price*10/100);
        cout<<"The net price is RM :"<<netprice<<endl;
    }
    else if(x>=2016 && x<=2017)
    {
        cout<<"Original Price RM ";
        cin>>price;
        cout<<"WELCOME TO PANDU CEPAT AUTO !"<<endl;
        cout<<"For model"<<x<<endl;
        discount=price*20/100;
        cout<<"Discount 20% :"<<discount<<endl;
        netprice=price-(price*20/100);
        cout<<"The net price is RM"<<netprice<<endl;
    }
    else if(x>=2014 && x<=2015)
    {
        cout<<"Original Price RM ";
        cin>>price;
        cout<<"WELCOME TO PANDU CEPAT AUTO !"<<endl;
        cout<<"For model"<<x<<endl;
        discount=price*30/100;
        cout<<"Discount 30% :"<<discount<<endl;
        netprice=price-(price*30/100);
        cout<<"The netprice is RM "<<netprice<<endl;
    }
    else if(x>=2012 && x<=2013)
    {
        cout<<"Original Price RM ";
        cin>>price;
        cout<<"WELCOME TO PANDU CEPAT AUTO !"<<endl;
        cout<<"For model"<<x<<endl;
        discount=price*40/100;
        cout<<"Discount 40% :"<<discount<<endl;
        netprice=price-(price*40/100);
        cout<<"The netprice is RM "<<netprice<<endl;
    }
    else
        cout<<"Original Price RM ";
        cin>>price;
        cout<<"WELCOME TO PANDU CEPAT AUTO !"<<endl;
        cout<<"For model"<<x<<endl;
        discount=price*50/100;
        cout<<"Discount 50% :"<<discount<<endl;
        netprice=price-(price*50/100);
        cout<<"The netprice is RM "<<netprice<<endl;

    return 0;
}


there are problems with this code, the output that I am trying to achieve is like this below. The user can choose their own Brand, Model and Year while the program will give the price. And this program needs to be loop twice before ending.

Please Enter Your Choice !!!
Brand 1: Proton
Model: Saga
Year: 2020
Original Price: RM 34000

WELCOME to Pandu Cepat Auto
For model 2020
Discount 10%: RM 3400
The netprice is: RM 30600

Please Enter Your Choice !!!
Brand 2: Proton
Model: Saga
Year: 2021
Original Price: RM 40000

WELCOME to Pandu Cepat Auto
For model 2021 Sorry!! No discount given!!
Last edited on
Please use code tags for code so that it's readable. And format it nicely before pasting. We are not machines!


[code]
//here be formatted code
[/code]

Last edited on
Thank you and sorry it is my first time asking here.
Iffahh wrote:
I keep getting error

So what is the EXACT error you are getting? Run-time error? Compile-time error? Logical error at run-time?

You've used code and output tags, that is a big help. You've indicated what your expected output should be. Maybe consider giving us an example of what your output actually is would be the next step.
Line 13: brand and model are unused.
Line 16: You're trying to assign a string to an integer.
Line 18: You need a semicolon at the end.

Lines 79-85 are indented as though they should execute inside the else at line 77, but they aren't enclosed in braces, so they always execute.

The if/else ladder has a lot of duplicate code. I would have the ladder just compute the discount. Then after you've computed the discount, you can print the appropriate output.

Since you didn't post in the beginners forum, I'll assume that you know about functions and methods. I'd create methods of the AClass class to read the info, compute the discount and print the results. Also, AClass should contain the brand, model, year, and original price of the car.

Here is a partial implementation that puts all this together. You'll need to write the code where the comments say "TBD"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

using namespace std;
class AClass
{
public:
    string brand, model;
    int year;
    int price;
    void read();
    double computeDiscount();
    void print();
};				// for the class assign


// Prompt the user for brand, model, year and original price
void
AClass::read()
{
    // TBD
}

// Return the discount for the model year. This returns the fractional
// discount. i.e., 10% is returned as 0.10.
double
AClass::computeDiscount()
{
   // TBD
}

// Compute the discount and Print the info for this car
void
AClass::print()
{
    double discount = computeDiscount();

    cout << "\nWELCOME TO PANDU CEPAT AUTO !" << endl;
    cout << "For model " << year << '\n';
    //TBD
}


int
main()
{
    AClass MyCar;

    MyCar.read();
    MyCar.print();
}


A possible OOP based solution code could be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Car
{
private:
    string m_model;
    string m_brand;
    int m_price;
    int m_year;

public:

    Car( const string & model, const string & brand, int price, int year ) 
    : m_model{model}, m_brand{brand}, m_price{price}, m_year{year}
    {}
    
    Car() { Read(); }
    
    int Discount() const {
        if( m_year < 2010 ) return 50;
        if( m_year < 2014 ) return 40;
        if( m_year < 2016 ) return 30;
        if( m_year < 2018 ) return 20;
        if( m_year < 2021 ) return 10;
        return 0;
    }
    string Model() const { return m_model; }
    string Brand() const { return m_brand; }
    int Price() const { return m_price; }
    int Year() const { return m_year; }
    
    void Print() const {
        cout<<endl<<"Original Price RM "<<Price()<<endl;
        cout<<"For model "<< Model()<<endl;
        cout<<"Discount " <<Discount()<<"% :"<< " RM "<< Discount()*Price()/100<<endl;
        cout<<"The netprice is RM "<< Price()-(Price()*Discount()/100) <<endl;
    }
    
    void Read() {
        cout << "Please enter the data for another car !!!"<<endl;
        cout << "Enter the car's model name: "; cin >> m_model;
        cout << "Enter the car's brand name: "; cin >> m_brand;
        cout << "Enter the car's price: "; cin >> m_price;
        cout << "Enter the  year: "; cin >> m_year;
        cout << endl;
    }
};

int main()
{
    int count;
    cout << "WELCOME TO PANDU CEPAT AUTO !"<<endl;
    cout << "How many cars want you read in? "; cin >> count;
    vector<Car> vCars;
    cout << endl;
    for( int i = 0; i < count; ++i) vCars.push_back( Car() );
    
    for( const auto & car : vCars ) car.Print();
}

Consider, that in this code the Read() method cannot deal with input errors. E.g if the user inputs a string where an integer is expected.
Topic archived. No new replies allowed.