C++ programming question. I did the codes but my year result is wrong. Please help me fix this.
Question: The population of a town A is less than the population of a town B. However, the population of town A is growing faster than the population of town B. Write a program that prompts the user to enter the population and growth rate of each town. The program outputs after how many years the population of town A will be greater than or equal to the population of town B and the populations of both towns at that time.
My codes:
#include<iostream>
using namespace std;
int townPopulationA, townPopulationB, year;
double townGrowthRateA, townGrowthRateB, finalA, finalB;
int main()
{
cout<<"This program is to compare population
growth. Town A population is less than Town B.But Town A have
higher growth rate\n";
cout << "Please enter higher population for town
B but higher increase for town A\n";
cout << "Enter the population of Town A:
";
cin >> townPopulationA;
cout << "Enter the population of Town B:
";
cin >> townPopulationB;
if (townPopulationA < townPopulationB)
{
cout << "Enter the growth
rate of town A: ";
cin >> townGrowthRateA;
cout << "Enter the growth
rate of town B: ";
cin >> townGrowthRateB;
if (townGrowthRateA <=
townGrowthRateB)
{
cout <<
"Your input is invalid";
return 1;
}
else if (townGrowthRateA >
townGrowthRateB)
{
finalA = townPopulationA * (1 + townGrowthRateA
/ 100.00);
finalB = townPopulationB * (1 + townGrowthRateB
/ 100.00);
year++;
cout <<
"Town A Population: " << finalA << endl;
cout <<
"Town B Population: " << finalB << endl;
}
cout << "After " <<
year << " year, the population of town A is greater or equal
to town B\n";
}
else if (townPopulationA > townPopulationB)
{
cout << "Your input is
invalid";
return 1;
}
system("pause");
}
#include<iostream>
using namespace std;
int townPopulationA, townPopulationB, year;
double townGrowthRateA, townGrowthRateB, finalA=0, finalB=0;
int main()
{
cout<<"This program is to compare population growth. Town A
population is less than Town B.But Town A have higher growth
rate\n";
cout << "Please enter higher population for town B but higher
increase for town A\n";
cout << "Enter the population of Town A: ";
cin >> townPopulationA;
cout << "Enter the population of Town B: ";
cin >> townPopulationB;
if (townPopulationA < townPopulationB)
{
cout << "Enter the growth rate of town A: ";
cin >> townGrowthRateA;
cout << "Enter the growth rate of town B: ";
cin >> townGrowthRateB;
if (townGrowthRateA <= townGrowthRateB)
{
cout << "Your input is invalid";
return 1;
}
else if (townGrowthRateA > townGrowthRateB)
{
finalA = townPopulationA;
finalB = townPopulationB;
do//iterating upto finalA is
greater than or equal to finalB
{
finalA += finalA * (1 + townGrowthRateA / 100.00);
finalB += finalB * (1 + townGrowthRateB / 100.00);
year++;
}while(finalA<finalB);
cout << "Town A Population: " << (int)finalA <<
endl;
cout << "Town B Population: " <<(int) finalB <<
endl;
}
cout << "After " << year << " year, the
population of town A is greater or equal to town B\n";
}
else if (townPopulationA > townPopulationB)
{
cout << "Your input is invalid";
return 1;
}
//system("pause");
}
output:
This program is to compare population growth. Town A population
is less than Town B.But Town A have higher growth rate
Please enter higher population for town B but higher increase for
town A
Enter the population of Town A: 200
Enter the population of Town B: 250
Enter the growth rate of town A: 100
Enter the growth rate of town B: 80
Town A Population: 16200
Town B Population: 15366
After 4 year, the population of town A is greater or equal to town
B
Process exited normally.
Press any key to continue . . .
C++ programming question. I did the codes but my year result is wrong. Please help me...
c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...
C++ programming I need at least three test cases for the program and at least one test has to pass #include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; void temperatureCoverter(float cel){ float f = ((cel*9.0)/5.0)+32; cout <<cel<<"C is equivalent to "<<round(f)<<"F"<<endl; } void distanceConverter(float km){ float miles = km * 0.6; cout<<km<<" km is equivalent to "<<fixed<<setprecision(2)<<miles<<" miles"<<endl; } void weightConverter(float kg){ float pounds=kg*2.2; cout<<kg<<" kg is equivalent to "<<fixed<<setprecision(1)<<pounds<<" pounds"<<endl; } int main() { string country;...
please do the program in simple programming it is for my first
c++ computer class i posted the example on pic 2,3 which is how
this should be done
Write a program that calculates and prints the bill for a cellular telephone company. The company offers two types of services: regular and premium. Its rates vary depending on the type of service. The rates are computed as follows: Regular service: $10.00 plus the first 50 minutes are free. Charges for...
My C++ code won't loop like it should. What am I doing wrong? #include <iostream> using namespace std; int main() { int n; int flag= 1; cout << "Prime/Not Prime" << endl; cout << "Enter the Number to check Prime:" << endl; cin >> n; int m = n / 2; int i; for (i = 2; i <= m; i++) if (n % i == 0) ...
My if/else statement wont run the program that I am calling. The menu prints but once I select a number the menu just reprints, the function called wont run. What can I do to fix this probelm? #include <iostream> #include "miltime.h" #include "time.h" #include "productionworker.h" #include "employee.h" #include "numdays.h" #include "circle.h" using namespace std; int main() { int select=1; while (select>0) { cout << "Option 1:Circle Class\n"<< endl; cout << "Option 2:NumDay Class\n" << endl; cout <<"Option 3:Employee and Production...
Am I using the right cin statement to take in values for my array? If I print them out it prints "0x61fea0" with the input 1 2 3 4 5 ///////////////////////////////// #include <iostream> using namespace std; const int ARRAY_LENGTH = 5; int main(){ int numbers[ARRAY_LENGTH]; int threshold; //@todo prompt user to enter array values cout << "Please input 5 numbers: " << endl; for(int i = 0; i < ARRAY_LENGTH; i++){ //@todo add cin statement to read in values for...
may i ask for help with this c++ problem?
this is the code i have for assignment 4 question 2:
#include<iostream>
#include<string>
#include<sstream>
#include<stack>
using namespace std;
int main()
{
string inputStr;
stack <int> numberStack;
cout<<"Enter your expression::";
getline(cin,inputStr);
int len=inputStr.length();
stringstream inputStream(inputStr);
string word;
int val,num1,num2;
while (inputStream >> word)
{
//cout << word << endl;
if(word[0] != '+'&& word[0] != '-' && word[0] !=
'*')
{
val=stoi(word);
numberStack.push(val);
// cout<<"Val:"<<val<<endl;
}
else if(word[0]=='+')
{
num1=numberStack.top();
numberStack.pop();
num2=numberStack.top();
numberStack.pop();...
I need help in my C++ code regarding outputting the enums in string chars. I created a switch statement for the enums in order to output words instead of their respective enumerated values, but an error came up regarding a "cout" operator on my "Type of Item" line in my ranged-based for loop near the bottom of the code. I tried casting "i.type" to both "i.GroceryItem::Section::type" and "i.Section::type", but neither worked. Simply put, if a user inputs 1 as their...
HELP ( i get these comment for this assignment please help me to fixed Please do not ask user to enter cents. Amount due and received should be in dollars such as 2.35 and 5.) Business P2.8Giving change. Implement a program that directs a cashier how to give change. The program has two inputs: the amount due and the amount received from the customer. Display the dollars, quarters, dimes, nickels, and pennies that the customer should receive in return. #include <iostream>...
C++ Programming - Design Process I need to create a flow chart based on the program I have created. I am very inexperienced with creating this design and your help will be much appreciated. My program takes a string of random letters and puts them them order. Our teacher gave us specific functions to use. Here is the code: //list of all the header files #include <iomanip> #include <iostream> #include <algorithm> #include <string> using namespace std; //Here are the Function...