2017-12-10 4 views
-1

私は帝国システムから国際システムへの異なる値と通貨を変換する変換プログラムを書いています。 1つの変換だけではないようにプログラムをループします。ここに私のコードです:関数をループする

#include "std_lib_facilities.h" 
#include "stdio.h" 

int a = 0; 
int b = 0; 
int c = 0; 

void money() 
{ 
cout << "This will convert CAD to either USD or EUR\n"; 
cout << "Please input USD or EUR followed by an enter keystroke for conversion\n"; 
string a; 
while(cin >> a){ 
    if(a == "USD"){ 
     cout << "If you would like to convert USD into CAD, enter 1.\n"; 
     cout << "If you would like to convert CAD into USD, enter 2.\n"; 
     int x; 
     cin >> x; 
     if(x == 1){ 
      cout << "Please enter the amount you wish to convert.\n"; 
      double j; 
      cin >> j; 
      double k = j*1.29; 
      cout << j << "USD is " << k << "CAD.\n"; 

     } 
     if (x == 2){ 
      cout << "Please enter the amount you wish to convert.\n"; 
      double o; 
      cin >> o; 
      double p = o*0.77; 
      cout << o << "CAD is " << p << "USD.\n"; 
     } 
    } 
    if(a == "EUR"){ 
     cout << "If you would like to convert EUR into CAD, enter 1.\n"; 
     cout << "If you would like to convert CAD into EUR, enter 2.\n"; 
     int y; 
     cin >> y; 
     if(y == 1){ 
      cout << "Please enter the amount you wish to convert.\n"; 
      double g; 
      cin >> g; 
      double h = g*1.46; 
      cout << g << "EUR is " << h << "CAD.\n"; 

     } 
     if(y == 2){ 
      cout << "Please enter the amount you wish to convert.\n"; 
      double z; 
      cin >> z; 
      double x = z*0.69; 
      cout << z << "CAD is " << x << "EUR.\n"; 
     } 
    } 
} 

} 

void weight() 
{ 
double amount; 
cout << "This will convert pounds to kilograms.\n"; 
cout << "Please input the amount you wish to convert.\n"; 
cin >> amount; 
cout << "Would you like to convert " << amount << "kg to lb or the reverse?\n"; 
cout << "To convert kg to lb, please press 1. To convert lb to kg please press 2.\n"; 
int q; 
while(cin >> q){ 
    if(q == 1){ 
     double kg = amount*2.2; 
     cout << amount << "kg is " << kg << "lb.\n"; 
    } 
    if(q == 2){ 
     double lb = amount*0.5; 
     cout << amount << "lb is " << lb << "kg.\n"; 
    } 

} 

} 

void temperature() // t to f and f to t 
{ 

} 


void setup() 
{ 
cout << "Please enter either c,w or t for the corresponding conversions.\n"; 
cout << "(Where c is for currency, w is for weight, and t is for temperature.\n"; 
string a; 
while (cin >> a){ 
    if(a == "c"){ 
     money(); 
     } 
    if(a == "w"){ 
     weight(); 
    } 
    if(a == "t"){ 
     temperature(); 
    } 

} 
} 


int main() // loop it to make more than one conversion 
{ 
cout << "Welcome to the ultimate converter app.\n"; 
setup(); 
cout << "Would you like to perform another conversion? (Y/N)\n"; 
string y; 
while(cin >> y){ 
    if(y == "Y"){ 
     setup(); 
    } 
    if(y == "N"){ 
     exit (EXIT_FAILURE); 
    } 
} 
return 0; 
} 

プログラムは期待通りに機能しますが、最後にはループしません。私は最後のコマンドを入力するだけで凍結します。戻り値0はフリーズします。

ご協力いただきありがとうございます。

+2

コードをステップ実行するときにデバッガは何をしますか? – Grantly

答えて

1

私はちょうどあなたのコードを試してみました。問題は、それはループに詰まっているということです。私が見る限り、ここには2つの主要な問題があります。

実際にメインで必要なときに、すべての関数でwhileループを使用しています。これらのシナリオでは、「while」ループの数を抑えてください。 2つ目は、 "cin >> variable"を引数として使用したことです。これから離れて、代わりに単純な変数を使用してください。
ここで私はあなたのコードを汚い方法で修正しましたが、それは機能します。 D

int a = 0; 
int b = 0; 
int c = 0; 

void money() 
{ 
    cout << "This will convert CAD to either USD or EUR\n"; 
    cout << "Please input USD or EUR followed by an enter keystroke for conversion\n"; 
    string a; 
    while (cin >> a) { 
     if (a == "USD") { 
      cout << "If you would like to convert USD into CAD, enter 1.\n"; 
      cout << "If you would like to convert CAD into USD, enter 2.\n"; 
      int x; 
      cin >> x; 
      if (x == 1) { 
       cout << "Please enter the amount you wish to convert.\n"; 
       double j; 
       cin >> j; 
       double k = j * 1.29; 
       cout << j << "USD is " << k << "CAD.\n"; 
       break; 

      } 
      if (x == 2) { 
       cout << "Please enter the amount you wish to convert.\n"; 
       double o; 
       cin >> o; 
       double p = o * 0.77; 
       cout << o << "CAD is " << p << "USD.\n"; 
       break; 
      } 
     } 
     if (a == "EUR") { 
      cout << "If you would like to convert EUR into CAD, enter 1.\n"; 
      cout << "If you would like to convert CAD into EUR, enter 2.\n"; 
      int y; 
      cin >> y; 
      if (y == 1) { 
       cout << "Please enter the amount you wish to convert.\n"; 
       double g; 
       cin >> g; 
       double h = g * 1.46; 
       cout << g << "EUR is " << h << "CAD.\n"; 
       break; 

      } 
      if (y == 2) { 
       cout << "Please enter the amount you wish to convert.\n"; 
       double z; 
       cin >> z; 
       double x = z * 0.69; 
       cout << z << "CAD is " << x << "EUR.\n"; 
       break; 
      } 
     } 
    } 

} 

void weight() 
{ 
    double amount; 
    cout << "This will convert pounds to kilograms.\n"; 
    cout << "Please input the amount you wish to convert.\n"; 
    cin >> amount; 
    cout << "Would you like to convert " << amount << "kg to lb or the reverse?\n"; 
    cout << "To convert kg to lb, please press 1. To convert lb to kg please press 2.\n"; 
    int q; 
    while (cin >> q) { 
     if (q == 1) { 
      double kg = amount * 2.2; 
      cout << amount << "kg is " << kg << "lb.\n"; 
      break; 
     } 
     if (q == 2) { 
      double lb = amount * 0.5; 
      cout << amount << "lb is " << lb << "kg.\n"; 
      break; 
     } 

    } 

} 

void temperature() // t to f and f to t 
{ 

} 


void setup() 
{ 
    cout << "Please enter either c,w or t for the corresponding conversions.\n"; 
    cout << "(Where c is for currency, w is for weight, and t is for temperature.\n"; 
    string a; 
    while (cin >> a) { 
     if (a == "c") { 
      money(); 
     } 
     if (a == "w") { 
      weight(); 
     } 
     if (a == "t") { 
      temperature(); 
     } 
     break; 
    } 
} 


int main() // loop it to make more than one conversion 
{ 
    cout << "Welcome to the ultimate converter app.\n"; 
    setup(); 
    string y; 
    while (true) { 
     cout << "Would you like to perform another conversion? (Y/N)\n"; 
     cin >> y; 
     if (y == "Y") { 
      setup(); 
     } 
     else if (y == "N") { 
      exit(EXIT_FAILURE); 
     } 
    } 
    return 0; 
} 
+0

ありがとうございます!私は間違いをもっと理解しようとします。 – KDX

+0

しかし、なぜcin >>変数が良い引数ではないのか分かりません。 – KDX

+0

cin <<変数は、while(true)ループと同じ効果があるという単純な理由から、実際には良い引数ではありません。 breakコマンドが呼び出されない限り、決して終了しません。 bool変数をループ引数として追加すると、読みやすくなり、意味があります。 – Hampfh