Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

tell me errors and decode with explanation

+4 votes
asked Jun 19, 2023 by Deepak Omkar (160 points)
#include<iostream>
using namespace std;
namespace BMW
{
    int Car_model;
    string Car_name;
    void display()
    {
        cout<<"enter the Model of a Car";
        cin>>Car_Model;
        cout<"enter the name of a Car";
        cin>>Car_name;
        cout<"name of Car ="<<Car_name;
        cout<<"Model of Car = "<<Car_Model;
    }
}
namespace Supra
{
    int Car_Model;
    string Car_name;
    void diplay()
    {
        cout<<"enter the Model of a Car";
        cin>>Car_Model;
        cout<<"enter the Car_name";
        cin>>Car_name;
        cout<<"name of a Car ="<<Car_name;
        cout<<"Model of a Car ="<<Car_Model;
    }
}

1 Answer

0 votes
answered Jun 28, 2023 by Peter Minarik (86,240 points)

You have a mixed casing of Car_Model and Car_model. Most programming languages, including C++ is case sensitive, so stick with one or the other, as they are two different identifiers.

The operator for a stream to take data is << (two less than) not a single one, so check your "cin<" code as they are wrong.

After this, your code should compile, but...

... you have no main function, so your code will not run.

... and I have never seen such a design in my life. Why would you have these global variables (Car_name and Car_model) in namespaces (BMW and Supra)? They are not particularly practical. Usually, classes should be employed to hold data that belongs together.

Try this instead:

#include <iostream>
#include <string>

class Car
{
private:
    int _model;
    std::string _make;

public:
    Car(std::string make, int model)
    {
        _model = model;
        _make = make;
    }
    
    void Display() const
    {
        std::cout << "Make: " << _make << "; Model: " << _model << std::endl;    
    }
    
    int GetModel() { return _model; }
    std::string GetMake() { return _make; } 
};

int main()
{
    Car bmw("BMW iX", 2019);
    Car toyota("Toyota GR Supra", 2020);
    bmw.Display();
    toyota.Display();
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...