c++ Should I put something in the blank? (Studying!

Asked 2 years ago, Updated 2 years ago, 24 views

There is a blockage in the empty space where the vehicle number is entered.

Can you give us a hint?

I wish there was a simple example, but I can't even think of course

I'm going to wrap my hair up and put it up Please help me

c++

2022-09-21 19:58

1 Answers

Yeah, it looks much better than the previous question. But rather than asking questions like this, I tried something, but it didn't work Or I think it would be good to ask questions like, "I'm not sure about this." It would be nice if you think of this as a place to learn from each other by asking and answering questions about things you don't know, not a place to do homework for you.

First of all, the creator of Car is wrong. std::string to NULL is a little wrong, but you can think of it as an empty string. The std::string is created by default as an empty string because the constructor exists. Therefore, you don't need to do anything else, but there are blanks, so you can fill it out as below.

Car() {
    carNo = ""; // or carNo = std::string();
    productYear = 2016;
}

Enter vehicle number (with spaces) If you look at , you will need to enter the vehicle number via the keyboard. For C++, you can use std::cin. However, the condition of the problem is so that you can enter it with spaces. If so, the string until the moment you enter the vehicle number and enter will be the vehicle number. In other words, you can think of it as taking one line. std::getline() is a function that accepts one line. If you make the first factor std::cin and the second factor carNo as shown below, you put a string in carNo.

std::getline(std::cin, carNo);

If you look at the vehicle number designation of the object with the entered vehicle number , you can think of it as putting the vehicle number in the car object. The Car class has a void setCarNo(string cn)member function, so you can call it and insert the vehicle number. The manufacturing year is then stored in py, so you can also call the void setProductYear(intpy) member function. So it's going to be like this.

carArray[i].setCarNo(carNo);
carArray[i].setProductYear(py);

information output of myCar object (see execution result) does not show the execution result, but if you look at Car's member function, you can call this if you see void displayCarInfo().

myCar.displayCarInfo();

Output car information of friends you entered (see execution result) There is no execution result for this either, but it will be like calling void displayCarInfo() right? However, since it is the vehicle information you entered, you can call the member function by the elements in the carArray array that you received through the for statement. Try this part yourself. You can easily write it by referring to the for statement that traverses the elements of the previous array and the code that outputs myCar just above.


2022-09-21 19:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.