### Trying to cin an int into a function.

I created a small program to convert Farenheit to Celcius. The function itself has no issues converting as I change the value in the parenthesis near the output function but I'd like to input the value I'm trying to convert from cin instead of inputting it through the code itself.

I currently have a weak grasp of the principles of cin so any info offered would definitely help.

int convert(int x) {

int z = 32;
auto dbl = 0.5556;
auto answer = (x - z) * dbl;

}

int main() {

std::cout << convert(100);
}

 ``12345678910111213141516`` ``````#include double F_to_C( double f ) { // check for temperature not less than absolute zero? return ( f - 32 ) * 5.0 / 9 ; } int main() { double f ; std::cout << "enter temperature in degrees fahrenheit: " ; std::cin >> f ; std::cout << f << " degrees fahrenheit == " << F_to_C(f) << " degrees celsius\n" ; }``````
Very impressive and thanks!

I'm curious though, from reading my meager attempt at programming, what would you suggest I study? What stupidity is oozing from me at this point?
Learn how to use the documentation of the C++ Standard Library.

https://www.cplusplus.com/reference/istream/istream/

https://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

Last edited on
Mr WallStreet wrote:
What stupidity is oozing from me at this point?

Before you worry about learning C++, you need to learn to think logically.

1) What's wrong with the following two lines of your code?
 ``12`` ``````auto answer = (x - z) * dbl; std::cin >> answer;``````

Hint: You compute answer then promptly overlay it with input from cin.

2) You may have noticed in JLBorges' code, that the cin statement is not in the conversion routine. It doesn't belong there. The conversion routine should do one thing: Convert.
Last edited on
Have a look at https://www.learncpp.com/
 Mr WallStreet wrote: What stupidity is oozing from me at this point?

first, lets be clear that you are not stupid. Programming is a new way to think and do things for most people, and it takes some time to absorb all the new information, process it, and start using it correctly. You tried, which is more than 75% of the people coming here bother to do, and it bodes well for your future as a coder. That said, this is going to seem like a lot, but every one needed a mention for future code habits and maybe seeing 'how to think about it' a bit:

do not over-use auto. it just makes it harder to debug/read code later on. Say double for doubles. Auto has its place, but not on simple variables. You can make everything 'auto'. Then you have to figure out which integer was really a double type bugs.

int z{32}; //prefer this way to initialize things. Also, prefer good variable names. z means nothing unless its an axis in 3d space or some other obvious contextual and common use of the letter z. Likewise, convert() means nothing in a large program, say from what to what in the name.

your function returns an integer after cooking up a double. That may be OK, but it is more likely a bug.

z and dbl are not necessary. you can make them constants with a meaningful name or you can just use in the equation and document what it stands for in a comment. Try to avoid unnecessary code like blah = something; return blah; should just be 'return something' without the extra middleman. Also avoid unnecessary variables.
- unnecessary variables often mean you will make pointless copies of data, which is slow.
- unnecessary lines of code is that much more to debug, maintain, and comprehend when you come back to it later.