cplusplus-training/Teil_1/4. Kontrollstrukturen/Uebung 2 - Fahrenheit in Celsius.cpp
2021-10-15 08:36:02 +02:00

66 lines
1.7 KiB
C++

#include <iostream>
using namespace std;
const int kEauationVal = 32;
template <class T>
void getVal(const char text[], T *val);
int main()
{
float fahrenheit = 0, schritt_weite = 0, maximal_wert = 0;
cout << "Eingabe\n\n";
getVal("Fahrenheit", &fahrenheit);
getVal("Schrittweite", &schritt_weite);
getVal("Maximalwert", &maximal_wert);
cout << "\nAusgabe\n\n";
for (int i = fahrenheit; i <= maximal_wert; i += schritt_weite)
{
float celsius_wert = ((i - kEauationVal) * 5) / 9;
cout << "Celsius: " << celsius_wert;
if (celsius_wert <= 10)
cout << " es ist kalt";
else if (celsius_wert <= 20)
cout << " es ist angenehm";
else if (celsius_wert > 20)
cout << " es ist heiss";
cout << endl;
}
do
{
cout << "Fahrenheit: " << fahrenheit << " | Celsius: " << ((fahrenheit - kEauationVal) * 5) / 9 << endl;
fahrenheit += schritt_weite;
} while (fahrenheit <= maximal_wert);
for (int i = fahrenheit; i <= maximal_wert; i += schritt_weite)
{
float celsius_wert = ((fahrenheit - kEauationVal) * 5) / 9;
cout << "Celsius: " << celsius_wert;
if (celsius_wert <= 10)
cout << " es ist kalt";
else if (celsius_wert <= 20)
cout << " es ist angenehm";
else if (celsius_wert > 20)
cout << " es ist heiss";
cout << endl;
}
return 0;
}
template <class T>
void getVal(const char text[], T *val)
{
do
{
cout << "\t" << text << " = ";
if (cin.fail())
{
cin.clear();
cin.ignore();
}
cin >> *val;
} while (cin.fail() || *val <= 0);
}