Nhờ giải đáp: printf và cout cho kết quả khác nhau trên test case CodeLearn

Chào mọi người. Trước tiên, mình mong mọi người không nhầm lẫn, đây không phải là post giải bài tập.

Nhờ mọi người xem giúp mình xem 2 đoạn code này có gì khác nhau không, vì đoạn dùng printf thì chỉ pass 6/7 test case của CodeLearn, đoạn dùng cout thì pass 7/7. Nếu được thì nhờ các bạn giải đáp thêm những thông tin nâng cao liên quan đến môi trường test của codelearn nếu biết ạ

Link đề: https://codelearn.io/learning/lap-trinh-huong-doi-tuong-trong-cpp?activityType=12&activityId=2537

Đoạn code giống nhau

#include<iostream>
#include<string>
using namespace std;

class Date {
private:
	int day;
	int month;
	int year;

public: Date(int day, int month, int year) {
	this->day = day;
	this->month = month;
	this->year = year;
}

	  void setDate(int day, int month, int year) {
		  this->day = day;
		  this->month = month;
		  this->year = year;
	  }

	  int getDay() {
		  return day;
	  }

	  void setDay(int day) {
		  this->day = day;
	  }

	  int getMonth() {
		  return month;
	  }

	  void setMonth(int month) {
		  this->month = month;
	  }

	  int getYear() {
		  return year;
	  }

	  void setYear(int year) {
		  this->year = year;
	  }

	  void display() {
                 // Phan can so sanh
	  }
};

2 đoạn code cần so sánh

2 đoạn này mình đã rút gọn

#include<iostream>
#include<string>
using namespace std;

class Date {
    int day, month, year;
    void display() {
        string day = to_string(this->day);
        string month = to_string(this->month);
        string year = to_string(this->year);
        if (day.length() == 1) {
            day = "0" + day;
        }
        if (month.length() == 1) {
            month = "0" + month;
        }
        printf("%s/%s/%s\n", day.c_str(), month.c_str(), year.c_str());
    }
}
#include<iostream>
#include<string>
using namespace std;

class Date {
    int day, month, year;
    void display() {
        string day = to_string(this->day);
        string month = to_string(this->month);
        string year = to_string(this->year);
        if (day.length() == 1) {
            day = "0" + day;
        }
        if (month.length() == 1) {
            month = "0" + month;
        }
       cout << day + "/" + month + "/" + year << "\n";
    }
}

Mình xin cảm ơn mọi người

Tại sao code C++ mà bạn không code C++ thuần mà lại đá sang C (printf với C string) vậy?

Bạn có thể đọc tham khảo: https://stackoverflow.com/a/10866136/

Cái này nhiều khả năng là do CodeLearn đọc mã của bạn trước để đảm bảo hàm display chỉ sử dụng cout theo ý muốn của họ.

Hai đoạn mã trên về cơ bản sẽ in ra hai kết quả giống nhau chỉ là cout được khuyên dùng hơn đối với C++.

1 Like

When learning C++, you should know the differences between printf and cout. It’s not a question of which is better, because you don’t need to worry about which output should be preferred. If you want formatted output, such as a double value with three decimal places (3 digits behind comma or a period in US format), printf is the first choice. Output with cout is raw and prints the values ​​as declared. For fast output, cout is the first choice.

What’s the main difference? printf depends on the specified format (e.g., %s for string, %d for integer, etc.).Hence: printf(ormatted). For example, if the specified format is %d and the output value is a double or float, an error is raised. This doesn’t happen with cout, because cout considers types at compile time and therefore knows what format an output value should be in.

UPDATE:

Nhờ mọi người xem giúp mình xem 2 đoạn code này có gì khác nhau không, vì đoạn dùng printf thì chỉ pass 6/7 test case của CodeLearn , đoạn dùng cout thì pass 7/7 .

I don’t know what the question was on CodeLearn, but rating printf 6/7 or cout 7/7 is nonsense when it comes to the question of which output is better.

Why cout is khuyên dùng? Please explain.

As @noname00 mentioned above

The link mentioned is not an objective explanation, but rather a penchant or a preference of the author who wrote these lines. First, a little about C++: It’s obvious that C++ is, so to speak, an extension of C. From this perspective, you can see that C++ basically runs on and is based on every C library.

printf completely – it’s a relic of C that’s rarely needed or useful in C++.

Total bullshit, because: cin and cout are C++ IOs, and scanf and printf are standard C IOs. And C always runs faster than C++. That’s a fact.

Some compilers can warn about this under some circumstances, but some compilers can’t/won’t at all, and none can under all circumstances.

Again, bullshit. It’s obviously due to problems with the C++ compiler implementation. It has nothing to do with printf (or scanf). And the rest of the argument reveals the author’s dislike of C because the author sees C as a NON-Object Oriented Language. OOL (here, C++) doesn’t mean that everything has to be programmed in a C++ like manner. This means that printf/scanf can be more efficient at formatting and outputting/inputting data, especially for simple or repetitive output. Furthermore, the performance of cin/cout can be slow because they need to synchronize with the underlying C library. This is essential if both C IO and C++ IO are to be used.

Some problems on online judges also judge student’s source code before testing the result.

This problem’s test cases look like this:

  • Lớp Date có thuộc tính year kiểu int với phạm vi truy cập private
  • Lớp Date có thuộc tính day kiểu int với phạm vi truy cập private
  • Lớp Date có thuộc tính month kiểu int với phạm vi truy cập private
  • Lớp Date có phương thức khởi tạo Date(day: int, month: int, year: int) và các getter được cài đặt đúng theo yêu cầu.
  • Lớp Date có phương thức setDate() và các getter được cài đặt đúng theo yêu cầu.
  • Lớp Date có các setter và getter được cài đặt đúng theo yêu cầu.
  • Lớp Date có phương thức display() được cài đặt đúng theo yêu cầu.

And the last test case requires display() method should be implemented correctly. These lines are requirement, so don’t say these are bullshit.

@noname00
You don’t seem to understand what I wrote. Are these three sentences difficult to understand?

  1. printf completely – it’s a relic of C that’s rarely needed or useful in C++.
  1. Some compilers can warn about this under some circumstances, but some compilers can’t/won’t at all, and none can under all circumstances.
  1. I don’t know what the question was on CodeLearn, but rating printf 6/7 or cout 7/7 is nonsense when it comes to the question of which output is better.

Google-Translation:

printf hoàn toàn – đó là di tích của C hiếm khi cần thiết hoặc hữu ích trong C++.

=> clearly bullshit.

Một số trình biên dịch có thể cảnh báo về điều này trong một số trường hợp, nhưng một số trình biên dịch không thể/sẽ không cảnh báo, và không có trình biên dịch nào có thể cảnh báo trong mọi trường hợp.

=> again, guessing is a bullshit in science!

Tôi không biết câu hỏi trên CodeLearn là gì, nhưng việc đánh giá printf 6/7 hay cout 7/7 là vô nghĩa khi nói đến câu hỏi đầu ra nào tốt hơn.

=> I said NONSENSE if this phrase " but rating printf 6/7 or cout 7/7 is nonsense when it comes to the question of which output is better." is true.

So, where did I mention display() or the code posted by @Thanh_Tu_Vo? As a moderator, you should read carefully before reprimanding a member.

UPDATE:

  1. My reply is refered to the question Nhờ giải đáp: printf và cout cho kết quả khác nhau trên test case CodeLearn.
    ==> so, all is about printf and cout.
  2. I wrote: “The link provided is not an objective explanation.” I was referring to the link YOU POSTED: “Bạn có thể đọc tham khảo:
    https://stackoverflow.com/a/10866136/
    ” and clearly criticized the content of the link, which was written by someone named Jerry Coffin. Are you this Jerry Coffin? If you are this Jerry Coffin, please tell me where I’m wrong.
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?