Population Calculator

Hello programmers.

I have a few questions about a program I wrote using Visual Studio Express: This is a homework question, and it does everything that is required, however I have a few questions that are baffling me. I have zero experience.

1. In a do/while loop. Can you tell it to do multiple things before you get to the while statement? I tried the below example and it did not work at all how I thought it would.

Example:

int main()

{
long long int sm_City = 0;
long long int lg_City = 0;
int counter = 0;
float sm_Growth = 0;
float lg_Growth = 0;

do
cout << "Enter starting population of the smaller city. " << endl;
cin >> sm_City; // Population of the smaller city
cout << endl;
cout << "Enter the starting population of the larger city. " << endl;
cin >> lg_City; // Population of the larger city
cout << endl;

while (sm_City > lg_City) // checks user input to ensure validity
{
cout << "Please check and re-enter your starting populations." << endl;
}

Question #2

This kind of follows question #1

I was trying to get the above code to allow me to let the user re-enter the starting populations if they were incorrect. I was unable to figure out which statement I needed to use to accomplish this. I ended up going a different route to complete the homework question which I have posted below. Can anyone tell me what I did wrong with my code below, or how I could add a statement to allow the user to re-enter the starting populations if they enter them backwards.

#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

int main()
{
long long int sm_City = 0;
long long int lg_City = 0;
int counter = 0;
float sm_Growth = 0;
float lg_Growth = 0;

cout << "Enter starting population of the smaller city. " << endl;
cin >> sm_City; // Population of the smaller city
cout << endl;
cout << "Enter the starting population of the larger city. " << endl;
cin >> lg_City; // Population of the larger city
cout << endl;

if (sm_City > lg_City) // checks user input to ensure validity
{
cout << "Please check and re-enter your starting populations." << endl;
}

else if (sm_City < lg_City)
cout << "Enter growth rate for the smaller city " << endl;
cin >> sm_Growth; // user inputed growth rate of the smaller city
sm_Growth /= 100; // conversion to a decimal
cout << endl;
cout << "Enter the growth rate for the larger city " << endl;
cin >> lg_Growth; // user inputed growth rate of the larger city
lg_Growth /= 100; // conversion to a decimal
cout << endl;


cout << "Year" << " " << "Small City" << " " << "Larger City" << endl;
cout << "---------------------------------------------" << endl;

while (sm_City < lg_City)
{
sm_City += (sm_City * sm_Growth); // calculates growth
lg_City += (lg_City * lg_Growth); // calculates growth
counter += 1; // increments the year

cout << counter << '\t' << sm_City << '\t' << " " << lg_City << endl;
cout << endl;
}
return 0;
}
powell80 wrote:
In a do/while loop. Can you tell it to do multiple things before you get to the while statement? I tried the below example and it did not work at all how I thought it would.
You need curly braces: do { /**/ } while(condition);
powell80 wrote:
I was trying to get the above code to allow me to let the user re-enter the starting populations if they were incorrect. I was unable to figure out which statement I needed to use to accomplish this. I ended up going a different route to complete the homework question which I have posted below. Can anyone tell me what I did wrong with my code below, or how I could add a statement to allow the user to re-enter the starting populations if they enter them backwards.
You want to loop on input and exit on success.
1
2
3
4
5
6
7
8
while((std::cout << "Enter a non-negative integer: ") && (std::cin >> num))
{
    if(num >= 0)
    {
        break;
    }
    std::cout << num << " is not a non-negative integer, try again." << std::endl;
}
Last edited on
Topic archived. No new replies allowed.