2011-12-16 70 views
2

我是一個C++新手,並遇到第一個程序出現問題。我試圖乘以兩個浮點數,結果總是顯示爲1.1111e + 1,其中1是隨機數。以下是我寫的小程序。乘以C++中的兩個浮點數給我非數字結果

#include <iostream> 
#include <string> 
#include <conio.h> 
using namespace std; 
int main() 
{ 
    float bank; 
    float dollor; 
    cout<<"Enter amount of $ deposited at Bank: ";//the data to input is 5000 
    cin>>bank; 
    cout<<"Enter current $ price: ";//1usd = 800mmk: the data to input is 800 
    cin>>dollor; 
    bank*=dollor;//all deposited $ to local currency 
    cout<<"Result is "<<bank; 
    getch(); 
} 

,並將該軟件的結果是圖4e + 006。

ps:我聲明爲float,以便有時輸入花車。 請幫我在這個程序,我錯了。謝謝大家..

+0

-1:看起來像是對我的正確答案。 – 2011-12-16 13:38:35

回答

8

4e+006scientific notation4000000,這是5000*800的正確答案。

要精心,4e+006代表4 * 10**6,其中10**6爲十的6次方。

要使用固定點符號,你可以改變你的程序就像這樣:

#include <iomanip> 
... 
cout << "Result is " << fixed << bank; 
1

那麼,5000由800確實是4E6,即4 * 10^6,400萬。

1

嘗試:

#include <iomanip> 

//... 
cout << "Result is "<< setprecision(2) << bank; 

或...

cout.precision(2); 
cout << "Result is " << fixed << bank; 
1

它在科學記數法。

看看this。除此之外,它還顯示瞭如何以定點符號打印數字。