2017-12-04 9 views
0

So, ich versuche, die Genauigkeit für Eingabewerte in meinem Code festzulegen. Ich möchte, dass die Werte mit zwei Dezimalstellen nach der Genauigkeit gedruckt werden, obwohl ich nicht genau weiß, wie.Einrichten der Präzision C++

Hier ist mein Code.

#include <iostream> 
#include <iomanip> 

float uphill, wellD, waterLvl, buckVol; 
float buckAscRate, downHill, volume; 
float last; 
float timeReq; 

int scene = 1; 





void timeRequired() 
{ 
    std::setw(2); 
    std::setprecision(2); 

    std::cout << "Scenario " << scene << ":" << std::endl; 
    std::cout << "up hill" << "   " << uphill << " sec" << std::endl; 
    std::cout << "well diamter" << "   " << wellD << " in" << std::endl; 
    std::cout << "water level" << "   " << waterLvl << " in" << std::endl; 
    std::cout << "bucket volume" << "   " << buckVol << " cu ft" << std::endl; 
    std::cout << "bucket ascent rate" << "   " << buckAscRate << " in/sec" << std::endl; 
    std::cout << "down hill" << "   " << downHill << " sec" << std::endl; 
    std::cout << "required volume" << "   " << volume << " cu ft" << std::endl; 

    timeReq = (uphill + downHill); 

    std::cout << "TIME REQUIRED" << "   " << timeReq << " sec" << std::endl; 

    std::cout << " " << std::endl; 



} 

void scenarioCONT() 
{ 
    do 
    { 
     std::cin >> wellD; 
     std::cin >> waterLvl; 
     std::cin >> buckVol; 
     std::cin >> buckAscRate; 
     std::cin >> downHill; 
     std::cin >> volume; 
     std::cin >> last; 

     if (uphill <= 1) uphill = 2; 
     if (wellD <= 0) wellD = 1; 
     if (waterLvl <= 0) waterLvl = 1; 
     if (buckVol <= 0) buckVol = 1; 
     if (buckAscRate <= 0) buckAscRate = 1; 
     if (downHill <= 0) buckAscRate = 1; 
     if (volume <= 0) volume = 1; 

     if (last > 1) 
     { 
      uphill = last; 
      scenarioCONT(); 

     } 



    } while (last != 0); 



} 
void scenario() 
{ 
    do 
    { 
     std::cin >> uphill; 
     std::cin >> wellD; 
     std::cin >> waterLvl; 
     std::cin >> buckVol; 
     std::cin >> buckAscRate; 
     std::cin >> downHill; 
     std::cin >> volume; 
     std::cin >> last; 

     if (uphill <= 1) uphill = 2; 
     if (wellD <= 0) wellD = 1; 
     if (waterLvl <= 0) waterLvl = 1; 
     if (buckVol <= 0) buckVol = 1; 
     if (buckAscRate <= 0) buckAscRate = 1; 
     if (downHill <= 0) buckAscRate = 1; 
     if (volume <= 0) volume = 1; 

     if (last > 1) 
     { 
      timeRequired(); 
      uphill = last; 
      scenarioCONT(); 

     } 
      scene++; 
      timeRequired(); 


    } while (last != 0); 


} 





int main() 
{ 

    scenario(); 

    system("pause"); 
} 

Ich habe gesagt ionmanip zu verwenden, um die Präzision zu setzen, obwohl ich nicht zu 100% auf bin, wie es geht. Irgendwelche Vorschläge?

+0

einen Blick auf http: // www .cplusplus.com/reference/iomanip/setprecision/Anweisungen zur Verwendung von 'std :: setprecision()'. Außerdem würde es die Dinge ein wenig aufräumen, um die '<<" "<<' Teile loszuwerden ... Sie können 'std :: setw()' dafür verwenden. Oder fügen Sie diese Leerzeichen in eine Variable ein und definieren Sie die Leerzeichen-Variable. Alles andere als all diese Leerzeichen wurden von Hand eingegeben oder kopiert/eingefügt. –

Antwort

0

Wenn Sie Ausgang 2 Dezimalstellen wollen, sollten Sie std::fixed verwenden, zusammen mit std::setprecision().

Werfen Sie einen Blick here.

Zum leichteren Verständnis ist hier ein Beispiel: cout << setprecision(2)<< 3.1415; Ausgänge 3.1 (2 Ziffern insgesamt) und

cout << setprecision(2)<<fixed<< 3.1415; Ausgänge 3.14 (2 Ziffern nach floating point)