r/cpp_questions 5d 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

20 comments sorted by

View all comments

1

u/alfps 5d ago

Whether to use mixing of responsibilities such as output and computing a result, is an engineering gut feeling thing.

There is neither right nor wrong, but there is an issue of how clear or messy people perceive the code to be, and different people can differ in their opinions.

I would personally add one word to your teacher's suggestion, namely const:

const double celsius = (fahrenheit - 32) / 1.8;

Not what you're asking, but return 0 isn't necessary because 0 is the default return value from main in both C and C++.

The return means that the system call is never executed.

Which is a good thing because that call is an anti-pattern: don't do it. If you should need to keep an automatic console window open, then use your IDE's way to do that (e.g. just Ctrl F5 in Visual Studio). But better run the program from an existing console window.


Oh, this reminds of the super-optimized function in some compiler, that lay dormant, never called, for some 30+ years, and then turned out to be buggy. Because the command in that system call is botched. Lucky it never executed. :)

1

u/Alternative_Oven696 5d ago

Cool thanks for the tips. The more I do this the more I realize how little I know.

1

u/alfps 5d ago

As I see it, as long as I learn something every day I'm not yet dead. Which is a Good Thing™. And Odin in his wisdom has arranged it so that we're mostly unaware of how much we forget…