2017-07-04 5 views
-7

私は関数を使用して異なる温度に変換し、文を切り替えるメニューオプションを使用してプログラムを必要とする...私は助けに感謝します、これは私がこれまで持っているもの...funtionsとswitch文を使用していますか?

#include <iostream> 
    #include <iomanip> 
    #include <cmath> 
    #include <cstdlib> 

    using namespace std; 

    int getMenuSelection(); //Displays menu and gets user selection 
    void convertFromCelsius(double tempCelsius);//From Celsius to the other  scales 
    void convertFromFahrenheit(double tempFahrenheit);//From Fahrenheit to the other scales 
    void convertFromKelvin(double temKelvin);//From Kelvin to the other scales 

int main() 
{ 
int menuSelection = 0; 
double tempCelsius, 
     tempFahrenheit, 
     tempKelvin; 

do 
{ 
    system("cls"); 

    menuSelection = getMenuSelection(); 

    switch (menuSelection) 
    { 
    case 1: convertFromCelsius(); 
     cout << "\nThis program will convert the temperatures in degree" 
       "Celsius to degree Fahrenheit and Kelvin\n"; 
     cout << "Enter 3 Temperature values in degree Celsius here: "; 
     cin >> tempCelsius; 
     tempFahrenheit = tempCelsius * (9.0/5.0) + 32; 
     tempKelvin = tempCelsius + 273.15; 
     cout << "\n***********Conversion Table************\n"; 
     cout << setprecision(2) << fixed 
       << "\nCelsius:\n" << tempCelsius << endl; 
     cout << setprecision(2) << fixed 
       << "\nFaherenheit:\n" << tempFahrenheit << endl; 
     cout << setprecision(2) << fixed 
       <<"\nKelvin:\n" << tempKelvin << endl; 
     break; 
    case 2: convertFromFahrenheit(); 
     cout << "\nThis program will convert the temperatures in degree" 
      "Fahrenheit to degree Celsius and Kelvin\n"; 
     cout << "Enter 3 Temperature values in degree Fahrenheit here: "; 
     cin >> tempFahrenheit; 
     tempCelsius = (tempFahrenheit - 32) * (5.0/9.0); 
     tempKelvin = (tempFahrenheit + 459.67) * (5.0/9.0); 
     cout << "\n***********Conversion Table************\n"; 
     cout << setprecision(2) << fixed 
      << "\nFaherenheit:\n" << tempFahrenheit << endl; 
     cout << setprecision(2) << fixed 
      << "\nCelsius:\n" << tempCelsius << endl; 
     cout << setprecision(2) << fixed 
      << "\nKelvin:\n" << tempKelvin << endl; 
     break; 
    case 3: convertFromKelvin(); 
     cout << "\nThis program will convert the temperatures in degree" 
       "Kelvin to degree Fahrenheit and Celsius\n"; 
     cout << "Enter 3 Temperature values in degree Kelvin here: "; 
     cin >> tempKelvin; 
     tempFahrenheit = tempKelvin * (9.0/5.0) - 459.67; 
     tempCelsius = tempKelvin - 273.15; 
     cout << "\n***********Conversion Table************\n"; 
     cout << setprecision(2) << fixed 
      << "\nKelvin:\n" << tempKelvin << endl; 
     cout << setprecision(2) << fixed 
      << "\nFaherenheit:\n" << tempFahrenheit << endl; 
     cout << setprecision(2) << fixed 
      << "\nCelsius:\n" << tempCelsius << endl; 
     break; 
    case 4: break; //Do nothing. Exit Condition 
    default: cout << "Please enter a number between 1 and 4" << endl; 
     system("pause"); 
    } 
} while (menuSelection != 4); 

system("pause"); 
return 0; 
} 
int getMenuSelection() 
{ 
int menuSelection = 0, attemp = 1; 
cout << "MarsX Space Vehicles Company Staff Tool\n"; 
cout << "\n------------------------------------------\n"; 
cout << "\n1.Convert from Celsius"; 
cout << "\n2.Convert from Fahrenheit"; 
cout << "\n3.Convert from Kelvin"; 
cout << "\n4.Quit Program"; 
cin >> menuSelection; 
} 
+1

'convertFromCelsius'などの関数を実装してください。それらの関数は変換された値を返すべきです。恐らく 'void 'の戻り値の型は役に立たないでしょう。 – MKR

+0

あなたは一度に多すぎるコードを書いてしまい、デバッグするのが難しくなりました。 1つの変換関数を記述する。その1つの関数のテストを書く。テストの結果に基づいてテストおよび修正機能を実行します。次に、次の関数を書いて繰り返します。すべての変換関数が機能したら、メニュー関数を記述します。次に、以前のようにメニュー機能をテストします。すべてのテストが終了したら、テストコードをメニュー機能を呼び出す 'main'関数に置き換えます。プログラムが完了しました。 – user4581301

+0

あなたはバグを探す場所が複数ありませんでした。一つの関数の記述とテストを学んだことを、次の関数に適用して、書き込みとデバッグの時間を節約できました。 – user4581301

答えて

1

これはバージョンです

#include <iostream> 
    #include <iomanip> 
    #include <cmath> 
    #include <cstdlib> 

    using namespace std; 

    int getMenuSelection(); //Displays menu and gets user selection 
    void convertFromCelsius(double tempCelsius);//From Celsius to the other  scales 
    void convertFromFahrenheit(double tempFahrenheit);//From Fahrenheit to the other scales 
    void convertFromKelvin(double temKelvin);//From Kelvin to the other scales 

int main() 
{ 
int menuSelection = 0; 
double tempCelsius, 
     tempFahrenheit, 
     tempKelvin; 

do 
{ 

    menuSelection = getMenuSelection(); 

    switch (menuSelection) 
    { 
    case 1: 
     cout << "\nThis program will convert the temperatures in degree" 
       "Celsius to degree Fahrenheit and Kelvin\n"; 
     cout << "Enter 3 Temperature values in degree Celsius here: "; 
     cin >> tempCelsius; 
     tempFahrenheit = tempCelsius * (9.0/5.0) + 32; 
     tempKelvin = tempCelsius + 273.15; 
     cout << "\n***********Conversion Table************\n"; 
     cout << setprecision(2) << fixed 
       << "\nCelsius:\n" << tempCelsius << endl; 
     cout << setprecision(2) << fixed 
       << "\nFaherenheit:\n" << tempFahrenheit << endl; 
     cout << setprecision(2) << fixed 
       <<"\nKelvin:\n" << tempKelvin << endl; 
     break; 
    case 2: 
     cout << "\nThis program will convert the temperatures in degree" 
      "Fahrenheit to degree Celsius and Kelvin\n"; 
     cout << "Enter 3 Temperature values in degree Fahrenheit here: "; 
     cin >> tempFahrenheit; 
     tempCelsius = (tempFahrenheit - 32) * (5.0/9.0); 
     tempKelvin = (tempFahrenheit + 459.67) * (5.0/9.0); 
     cout << "\n***********Conversion Table************\n"; 
     cout << setprecision(2) << fixed 
      << "\nFaherenheit:\n" << tempFahrenheit << endl; 
     cout << setprecision(2) << fixed 
      << "\nCelsius:\n" << tempCelsius << endl; 
     cout << setprecision(2) << fixed 
      << "\nKelvin:\n" << tempKelvin << endl; 
     break; 
    case 3: 
     cout << "\nThis program will convert the temperatures in degree" 
       "Kelvin to degree Fahrenheit and Celsius\n"; 
     cout << "Enter 3 Temperature values in degree Kelvin here: "; 
     cin >> tempKelvin; 
     tempFahrenheit = tempKelvin * (9.0/5.0) - 459.67; 
     tempCelsius = tempKelvin - 273.15; 
     cout << "\n***********Conversion Table************\n"; 
     cout << setprecision(2) << fixed 
      << "\nKelvin:\n" << tempKelvin << endl; 
     cout << setprecision(2) << fixed 
      << "\nFaherenheit:\n" << tempFahrenheit << endl; 
     cout << setprecision(2) << fixed 
      << "\nCelsius:\n" << tempCelsius << endl; 
     break; 
    case 4: break; //Do nothing. Exit Condition 
    default: cout << "Please enter a number between 1 and 4" << endl; 
     system("pause"); 
    } 
} while (menuSelection != 4); 

system("pause"); 
return 0; 
} 
int getMenuSelection() 
{ 
int menuSelection = 0, attemp = 1; 
cout << "MarsX Space Vehicles Company Staff Tool\n"; 
cout << "\n------------------------------------------\n"; 
cout << "\n1.Convert from Celsius"; 
cout << "\n2.Convert from Fahrenheit"; 
cout << "\n3.Convert from Kelvin"; 
cout << "\n4.Quit Program\n"; 
cin >> menuSelection; 
return menuSelection; 
} 

を私はしかし、いくつかの重要な発言を持っている:

あなたが実装されていませんでした変換メソッドを呼んでいた、その機能は、しかしスイッチトイレに適切に行われることに注意してください動作しますあなたのコードのp、私はそれらに触れていない。
また、menuSelectionメソッドは、intを返すように選択してから何かを返す必要がありました。特にメニューを機能させるためには、特にそうでした。私はそれを付け加えた。
また、あなたの出力を見ることができないように、それはクリーニングして再起動していたので、do {}whileループからsystem("cls");を取り出しました。
最後に、あなたのコードは1つの温度のみを変換することに注意してください。今は3つの温度を必要に応じて変換することがあなたの仕事です。

https://www.edx.org/course/introduction-c-microsoft-dev210x-4

https://www.sololearn.com/Course/CPlusPlus/

グッドラック:

は最後に、ここではあなたがC++を効率的に学ぶために従うことができますコースのいくつかのリンクです。

関連する問題