Witam, dziś uczyłem się o funkcjach. Stworzyłem poniższy program, teraz pytanie czy mimo użycia funkcji nie dodałem sobie kilku linijek kodu na darmo.
#include <iostream>
#include <windows.h>
using namespace std;
int wybor; double metry, centymetry; // Zmienne
void wypisz_menu() // Wypisywanie menu glownego
{
cout << "@@@@@@@@@@@@@@@@@@@@\n"<<endl;
cout << "1.Metry na centymetry"<<endl;
cout << "2.Metry na milimetry"<<endl;
cout << "3.Centymetry na metry"<<endl;
cout << "4.Centymetry na milimetry\n"<<endl;
cout << "@@@@@@@@@@@@@@@@@@@@\n\n"<<endl;
}
void wypisz_opcje() // Opcje po wyliczeniu
{
cout << endl;
cout << "1.Wroc do menu."<<endl;
cout << "2.Wyjdz"<<endl;
cout << "Wybor: "; cin >> wybor;
switch (wybor)
{
case 1: system("cls"); break;
case 2: exit(0); break;
}
if ((wybor!=1&&wybor!=2))
{
system("cls");
cout << "Blad!";
wypisz_opcje();
}
}
double metry_na_centymetry(double metry) // FUNKCJE
{
cout << "Podaj liczbe metrow: "; cin >> metry;
system("cls");
return metry*100;
}
double metry_na_milimetry(double metry)
{
cout << "Podaj liczbe metrow: "; cin >> metry;
system("cls");
return metry*1000;
}
double centymetry_na_metry(double centymetry)
{
cout << "Podaj liczbe centymetrow: "; cin >> centymetry;
system("cls");
return centymetry/100;
}
double centymetry_na_milimetry(double centymetry)
{
cout << "Podaj liczbe centymetow: "; cin >>centymetry;
system("cls");
return centymetry*10;
} // KONIEC FUNKCJI
int main()
{
for (;;)
{
wypisz_menu();
Sleep(600);
cout << "Wybierz opcje:"; cin >> wybor; // wybor opcji
system("cls");
switch (wybor) // SWITCH
{
case 1 : cout<< "Jest to: "<<metry_na_centymetry(metry); " centymetrow."; break;
case 2 : cout<< "Jest to: "<<metry_na_milimetry(metry)<< " milimetrow."; break;
case 3 : cout<< "Jest to: "<<centymetry_na_metry(centymetry)<< " metrow."; break;
case 4 : cout<< "Jest to: "<<centymetry_na_milimetry(centymetry)<< " milimetrow."; break;
}
wypisz_opcje();
}
}