I've made my first little program written in C++ and was wondering if anyone could evaluate it and suggest some improvements. No copy paste. I was wondering if I can do something with my little knowledge and came up with the simple idea of converting Celsius and Fahrenheit.
I don't know why, but when using 9/5, it gives a different output than when using 1.8 or 9.0/5.0.
#include <iostream>
using namespace std;
// My very first code written by myself
char temperature; // what are we converting ( celsius or farenheit into another )
float value; // value that is being converted ( -10, 0, 10, 20.5 etc. )
int main()
{
cout << "Welcome" << "\n" << "This little program converts Celsius to Fahrenheit and vice versa" << endl;
cout << "Enter c for Celsius, f for Fahrenheit or p for proof" << endl;
cin >> temperature; // storing input which accepts only c, f, p
switch (temperature) { // checking input for one of 3 accepted options, else using default
case 'c' : {
cout << "Converting Celsius to Fahrenheit" << "\n" << "Enter value" << endl;
cin >> value; // storing the value of temperature
float farenheit = value*1.8+32; // converting celsius to fahrenheit using the formula
cout << value << " Celsius in Fahrenheit is: " << farenheit << endl;
}
break;
case 'f' : {
cout << "Converting Fahrenheit to Celsius" << "\n" << "Enter value" << endl;
cin >> value; // storing the value of temperature
float celsius = (value-32)/1.8; // converting fahrenheit to celsius using the formula
cout << value << " Fahrenheit in Celsius is: " << celsius << endl;
}
break;
case 'p' : {
cout << "Proof that program is running correctly, here is a list of -20 to 20 Celsius converted to Fahrenheit" << endl;
for (int i=-20; i<21; i++) { // using a for loop to print Celsius to Fahrenheit values from -20 to 20
cout << i << " = " << i*1.8+32 << endl; // output Celsius = Fahrenheit
}
}
break;
default:
cout << "Invalid input! Use c, f or p" << endl; // any input except the 3 accepted options will result in this default output
}
cout << "Made by blackborg";
return 0;
}