2016-05-24 3 views
-2

小数点以下を小数点以下に変換するプログラムをC++で作成するのに問題があります。私は以下のコードを貼り付けて、提案に公開しています。小数点以下を計算する方法

int main() 
{ 
    char n = 0;//for decimal point 
    char x = 0;//for tenths place 
    char y = 0;//for hundredths place 
    std::cout << "Please enter the decimal to the hundredths place here: "; 
    std::cin >> n; 
    std::cin >> x; 
    std::cin >> y; 
     if (x == 0) 
      std::cout << 0 << y << "/100"; 
     if (y == 0) 
      std::cout << x << "/10"; 

     if (y == 1 || y == 2 || y == 3 || y == 4 || y == 5 || y == 6 || y == 7 || y == 9) 
      std::cout << x << y << "/100"; 

} 
+1

私はトラブルのプログラムを作るを持っています*小数点以下を小数点に変換する* - 集中的な質問をしてください。何が「うまくいかない」?そして、あなたが投稿した 'if'ステートメントの代わりに単に' if(y> = 1 && y <= 9) 'と書いてあったのはどうでしたか? – PaulMcKenzie

+1

「分数」を定義します。 –

答えて

1

char型の入力をint型の値と比較しています。使用するint値のchar相当量(1ではなく1)と比較してみてください。また、あなたの最後の場合は8の可能性を除いて、私の意見では奇妙です。それはこのような何か与えるだろう

:Bettorunの答えに加えて

if (x == '0') 
    std::cout << 0 << y << "/100"; 
if (y == '0') 
     std::cout << x << "/10"; 

    if (y == '1' || y == '2' || y == '3' || y == '4' || y == '5' || y == '6' || y == '7' || y == '8' || y == '9') 
     std::cout << x << y << "/100"; 
0

を、また、あなたの変数に同じことを行う必要があります:

#include <iostream> 

using namespace std; 

int main() 
{ 
     char n = '0';//for decimal point 
     char x = '0';//for tenths place 
     char y = '0';//for hundredths place 

     cout << "Please enter the decimal to the hundredths place here: "; 
     cin >> n >> x >> y; 

     if (x == '0') 
      cout << '0' << y << "/100" << endl; 

     if (y == '0') 
      cout << x << "/10" << endl; 

     else if (y == '1' || y == '2' || y == '3' || y == '4' || y == '5' || y == '6' || y == '7' || y == '9') 
     cout << x << y << "/100" << endl; 

     return 0; 
} 
関連する問題