Link to my first question:
Converting Celsius and Fahrenheit in C++
I've improved my code, following @Jamal's recommendations. I hope I did everything right. The only thing I didn't get was putting that tagline cout
in a comment.
I know I could have used if
instead of switch
, but I was wondering if I can do it with switches.
The previous code had only 1 input for temperature, so it was easy. Here, since we have 3 units, we need either 2 inputs (which I used) or maybe it could have been 1 input as (cf - Celsius to Fahrenheit) for example, but since switch doesn't accept strings and I've decided to use switch, it was a bit difficult.
So, how does it look? Any suggestions?
#include <iostream>
#include <string>
using std::cout;
using std::cin;
char temperature; // first temperature unit
char temperature2; // second temperature unit
double value; // value that is being converted
int x,y; // used to check what function we will be using for output
void welcome () { // intro and first 2 inputs manual
std::cout << "Welcome" << "\n" << "This little program (Version 2.0) converts the following temperature units: Celsius, Fahrenheit and Kelvins\nEnter first temperature unit which you want to convert and then a second one\nUse: c(C) for Celsius, f(F) for Fahrenheit or k(K) for Kelvins\nExample:\nc\nf\n";
std::cin >> temperature >> temperature2; // storing what 2 temperature units are being converted, which accepts c, C, f, F, k, K.
}
double switch2() {
switch (temperature) { // checking input for one of 3 accepted characters, else using default
case 'c' :
case 'C' : {
x = 1;
}
break;
case 'f' :
case 'F' : {
x = 2;
}
break;
case 'k' :
case 'K' : {
x = 3;
}
break;
default:
std::cout << "Invalid character " << temperature << "! Use c(C), f(F), k(K) for first temperature\n"; // any input except the 3 accepted options will result in this default output
}
return x;
}
double switch3() {
switch (temperature2) { // checking input for one of 3 accepted characters, else using default
case 'c' :
case 'C' : {
y = 1;
}
break;
case 'f' :
case 'F' : {
y = 2;
}
break;
case 'k' :
case 'K' : {
y = 3;
}
break;
default:
std::cout << "Invalid character " << temperature2 << "! Use c(C), f(F), k(K) for second temperature\n"; // any input except the 3 accepted options will result in this default output
}
return y;
}
// taking value and converting it depending on temperature units selected
void CtF (double value){
std::cout << "Converting Celsius to Fahrenheit\n" << "Enter value:\n";
std::cin >> value;
double fahrenheit = value*(9.0/5.0)+32;
std::cout << value << " Celsius in Fahrenheit is " << fahrenheit;
}
void CtK (double value){
std::cout << "Converting Celsius to Kelvins\n" << "Enter value:\n";
std::cin >> value;
double kelvins = value + 273.15;
std::cout << value << " Celsius in Kelvins is " << kelvins;
}
void FtC (double value){
std::cout << "Converting Fahrenheit to Celsius" << "\n" << "Enter value:\n";
std::cin >> value;
double celsius = (value-32)/(9.0/5.0);
std::cout << value << " Fahrenheit in Celsius is " << celsius;
}
void FtK (double value){
std::cout << "Converting Fahrenheit to Kelvins" << "\n" << "Enter value:\n";
std::cin >> value;
double kelvins = (value + 459.67)*5.0/9.0;
std::cout << value << " Fahrenheit in Kelvins is " << kelvins;
}
void KtC (double value){
std::cout << "Converting Kelvins to Celsius" << "\n" << "Enter value:\n";
std::cin >> value;
double celsius = value - 273.15 ;
std::cout << value << " Kelvins in Celsius is " << celsius;
}
void KtF (double value){
std::cout << "Converting Kelvins to Fahrenheit" << "\n" << "Enter value:\n";
std::cin >> value;
double fahrenheit = value*(9.0/5.0) - 459.67 ;
std::cout << value << " Kelvins in Fahrenheit is " << fahrenheit;
}
void result () { // function that executes 2 other functions and checks what output function to use
switch2();
switch3();
if (x==1 && y==2){
CtF(value);
}
if (x==1 && y==3){
CtK(value);
}
if (x==2 && y==1){
FtC(value);
}
if (x==2 && y==3){
FtK(value);
}
if (x==3 && y==1){
KtC(value);
}
if (x==3 && y==2){
KtF(value);
}
if ((x==1 && y==1) || (x==2 && y==2) || (x==3 && y==3)) { // error handling
std::cout << "Very funny :) \nYou can't convert the same temperature!";
}
std::cout << "\nMade by blackborg\n02.04.2018";
}
int main()
{
welcome();
result();
}
cout
. You can either put it in a comment or move it to the "welcome" output. \$\endgroup\$