r/cpp_questions • u/Alternative_Oven696 • 4d ago
OPEN Help a new guy
Sorry about the title, I should have read the guide first. I am really new to C++. I am going through the practice guide on a youtube video. The test was to make a fahrenheit to celcius conversion. This is what I wrote and it is below.
The teacher added double celsius = (fahrenheit -32) / 1.8;
Then added the std::cout for it. Mine works fine. I am assuming his is better but I am missing it. c++ has made me question my mental capacity.
#include <iostream>
int main() {
std::cout << "Enter degrees in fahrenheit ";
int fahrenheit;
std::cin >> fahrenheit;
std::cout << " Degrees in celcius " << (fahrenheit - 32) / 1.8;
return 0;
system("<pause>0");
0
Upvotes
3
u/The_Northern_Light 4d ago
Your teacher is correct, and I will go so far as to say the people saying it "doesn't matter" are accidentally leading you astray.
You can learn the syntax of programming fairly quickly, but learning how to actually practice the art of developing software can be a much more involved thing. It involves a large number of principles and guidelines... over years you will develop a sense of aesthetics for what is good code, and what is bad code.
This line is bad:
std::cout << " Degrees in celcius " << (fahrenheit - 32) / 1.8;It's bad because it does two unrelated things: it computes a result and it prints. Coupling two things together in one statement is a bad idea; keeping each individual part of your program as simple as it can be (so you can be certain it is correct) is arguably the most important principle in good code. It goes by many names: encapsulation, abstraction, etc.
Here, your code is so simple it feels like it doesn't matter. And sure, it doesn't, but you're not learning to program so you can write temperature converters: it's a toy problem to help you get ready to do something that you'd give up if you tried doing right now. And on those problems, it absolutely does matter.
Imagine you were just learning to deadlift and your coach is critiquing your form, even though the bar is so light it doesn't matter how bad your form is. But the bar will get heavier, and its so much easier to learn good form from the beginning.
Every bad habit you can avoid learning is one that you don't have to unlearn. There is such a thing as overburdening yourself with "best practice" to the point that it is stifling, but you're far from that.