0
My program doesn't work as I expected
The output is correct until some line were it stops working and is displaying this: timeout: the monitored command dumped core What can I do?
3 Respostas
+ 4
That is the result of division by zero in the line
int d = a / ( c / 100 );
If you divide an integer by another integer, the result will be an integer as well (the decimal fraction is truncated), e.g.
10 / 4 = 2
4 / 10 = 0
So if you enter an integer smaller than 100 for 'c', c / 100 would be 0 and the next division would fail.
If you expect a floating-point value from an arithmetic operation, at least one operand should be a floating-point type, e.g.
10.0 / 4 = 2.5
4 / 10.0 = 0.4
Keep in mind the conversion to int at the end will cut the fraction off again, and an input of 0 for 'c' will still lead to division by zero.
+ 1
Ok, thx for help
0
Here is the code:
#include <iostream>
using namespace std;
int main()
{
int a; /*Numarul de cazuri descoperite din ziua respectiva*/
cout <<"Introduceti numarul de cazuri descoperite din aceasta zi: ";
cin >>a;
cout <<a <<endl;
int x; /*Statistica 1*/
cout <<"Introduceti cate cazuri nedescoperite se ascund in spatele unui caz descoperit: ";
cin >>x;
cout <<x <<endl;
int b = a * x; /*Numarul de cazuri in total*/
cout <<"Numarul total de cazuri din aceasta zi: " <<b <<endl;
int c; /*Statistica 2*/
cout <<"Introduceti numarul de decese raportate la 100 de cazuri: ";
cin >>c;
cout <<c <<endl;
int d = a / (c / 100); /*Numarul de decese intr-o zi*/
cout <<"Numarul de decese din aceasta zi: " <<d <<endl;
int e; /*Numarul de zile de la infectare pana la vindecare*/
cout <<"Introduceti numarul de zile de la infectarea unei persoane pana la vindecarea acesteia: ";
cin >>e;
cout <<e <<endl;
int f; /*Numarul de infectari de acum e zile*/
cout <<"Introduceti numarul de infectari de acum " <<e <<" zile: ";
cin >>f;
cout <<f <<endl;
int g; /*Numarul de decese de acum e zile*/
cout <<"Introduceti numarul de decese de acum " <<e <<" zile: ";
cin >>g;
cout <<g <<endl;
int h = f - g; /*Numarul de vindecari din aceasta zi*/
cout <<"Numarul de vindecari din aceasta zi: " <<h <<endl;
return 0;
}