Trying to cin an int into a function.

Dec 8, 2021 at 2:56am
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;
std::cin >> answer;
return answer;


}







int main() {

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

Dec 8, 2021 at 3:17am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

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" ;
}
Dec 8, 2021 at 3:34am
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?
Dec 8, 2021 at 3:38am
Learn how to use the documentation of the C++ Standard Library.

Read about the std::istream class (cin is a std::istream), and specifically about the >> operator:

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

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


Last edited on Dec 8, 2021 at 4:38am
Dec 8, 2021 at 5:20am
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?
1
2
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 Dec 10, 2021 at 10:20pm
Dec 8, 2021 at 10:07am
Have a look at https://www.learncpp.com/
Dec 8, 2021 at 1:07pm
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.
-unnecessary code may confuse other coders using or reading your work.

Dec 8, 2021 at 4:08pm
Topic archived. No new replies allowed.