first program
Mar 18, 2021 at 5:16pm UTC
hello i'm new to programming in cpp
i've written a small code that is a "pandemic calculator"
my question is how can i make the infected number be equal to the population number and that the program will not end when only half the population is infected but when the whole population is infected
or basically how can i write the same code without using #include "windows.h" i'm on windows yet i use code-blocks and mingw64 bit compiler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <iostream>
#include <cstdlib>
#include <windows.h>
void calculate(int );
int main()
{
// using std::cout;
// using std::cin;
int population {0};
std::cout << "enter population number: \n\n" ;
std::cin >> population;
calculate(population);
return 0;
}
void calculate(int pop)
{
int infected {1}, deaths {0}, days {0};
do {
system("cls" );
std::cout << "population: " << pop << " infected: " << infected << " deaths: " << deaths << " days numbered: " << days;
infected *= 2;
if (infected > 1000)
{
deaths = infected * 34 / 1000;
}
days++;
pop -= deaths;
Sleep(1000);
}while (infected < pop);
}
Last edited on Mar 18, 2021 at 5:20pm UTC
Mar 18, 2021 at 5:44pm UTC
Without using windows.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <iostream>
#include <thread>
#include <chrono>
void calculate(size_t);
int main()
{
size_t population {0};
std::cout << "enter population number: " ;
std::cin >> population;
calculate(population);
}
void calculate(size_t pop)
{
size_t infected {1}, deaths {0}, days {0};
do {
std::cout << "population: " << pop << " infected: " << infected << " deaths: " << deaths << " days numbered: " << days << '\n' ;
infected *= 2;
if (infected > 1000)
deaths = infected * 34 / 1000;
++days;
pop -= deaths;
//Sleep(500);
std::this_thread::sleep_for(std::chrono::seconds(1));
} while (infected <= pop);
std::cout << "population: " << pop << " infected: " << infected << " deaths: " << deaths << " days numbered: " << days << '\n' ;
}
Topic archived. No new replies allowed.