2016-04-20 4 views
1

Ich bin ein Anfänger, der einige Fragen zu meinen Hausaufgaben für die Schule hat.wie Population/Neverending Schleife zu erhöhen

Mit meinem aktuellen Code stecke ich in einer endlosen Schleife, die ich vermute, weil mein Zustand nie wirklich getroffen wird (Pop A ist größer als Pop B) und ich bin mir nicht sicher, wie ich weitermachen soll. Ich bin mir auch nicht sicher, wie ich die Jahre, in denen Town A die Stadt B übertreffen sollte, richtig heraufrechnen/berechnen kann, aber das ist es, was ich bisher habe.

#include <iostream> 
#include <iomanip> 
using namespace std; 

int main() 
{ 
    int townA; 
    int townB; 
    double growthA; 
    double growthB; 
    double finalA; 
    double finalB; 
    int years = 0; 

    cout << "Enter the population of town A: "; 
    cin >> townA; 
    cout << "Enter the growth rate of town A: "; 
    cin >> growthA; 
    cout << "Enter the population of town B: "; 
    cin >> townB; 
    cout << "Enter the growth rate of town B: "; 
    cin >> growthB; 

    while ((townA <= 0) && (growthA <=0) && (townB > townA) && (growthB < growthA) && (growthB > 0)) 
    { 
     cout << "Error: Values must be positive, please try again." << endl; 
     cout << "Enter the population of town A: "; 
     cin >> townA; 
     cout << "Enter the growth rate of town A: "; 
     cin >> growthA; 
     cout << "Enter the population of town B: "; 
     cin >> townB; 
     cout << "Enter the growth rate of town B: "; 
     cin >> growthB; 
     cout << endl; 
    } 

    years = 0; 
    while (townA <= townB) 
    { 
     finalA = ((growthA/100) * (townA)) + townA; 
     finalB = ((growthB/100) * (townB)) + townB; 
     cout << "It took Town A " << years << " years to exceed the population of Town B." << endl; 
     cout << "Town A " << finalA << endl; 
     cout << "Town B " << finalB << endl; 
    } 

     cout << "\n\n" << endl; // Teacher required us to output it to the screen incase anyone is wondering why I have this block 
     cout << setw(3) << "Town A" << setw(15) << "Town B" << endl; 
     cout << setw(3) << growthA << "%" << setw(10) << growthB << "%" << endl; 
     cout << setw(3) << townA << setw(7) << townB << endl; 
     cout << "Year" << endl; 
     cout << "--------------------------------------------" << endl; 

    return 0; 
} 

Antwort

1

Sie sind nicht die Werte von townA und townB in der Schleife zu aktualisieren. Daher kommen Sie nie aus der Schleife heraus.

Außerdem müssen Sie in der Schleife years inkrementieren. Verwenden Sie:

while (townA <= townB) 
{ 
    finalA = ((growthA/100) * (townA)) + townA; 
    finalB = ((growthB/100) * (townB)) + townB; 
    cout << "Town A " << finalA << endl; 
    cout << "Town B " << finalB << endl; 

    // Update the values of the key variables. 
    ++years; 
    townA = finalA; 
    townB = finalB; 
} 

// This needs to be moved from the loop. 
cout << "It took Town A " << years << " years to exceed the population of Town B." << endl; 
+0

@ TonyD, natürlich :) –

Verwandte Themen