# Thread: Math Problems?

1. ## Math Problems?

I have this code:
Code:
```#include <iostream>

using namespace std;

int bday;
int bmonth;
int byear;
int cday;
int cmonth;
int cyear;
char cont;
int spd;

int main()
{

do{
bday=0;
cday=0;
byear=0;
cyear=0;
cmonth=0;
bmonth=0;
spd=0;

system("cls");
cout<< "SLEEPY TIME!" << endl;
cout<< "This is a program that calculates how much you have time you have slept in your entire life. Hit ENTER to continue." << endl;
cin.get();
system("cls");
cout<<"How many hours on average do you sleep in a night?" << endl;
cin>> spd;
system("cls");
cout<<"What year were you born in? (example: 1994)" << endl;
cin>> byear;
cin.ignore();
system("cls");
cout<<"What month of the year were you born in? Please represent numerically (january: 1 , february: 2 , etc.)" << endl;
cin>> bmonth;
cin.ignore();
system("cls");
cout<<"What day of the month were you born? (example: 10)" << endl;
cin>> bday;
cin.ignore();
system("cls");
cout<<"What year is it currently? (example: 1994)" << endl;
cin>> cyear;
cin.ignore();
system("cls");
cout<<"What month is it currently? Please represent numerically (january: 1 , february: 2 , etc.)" << endl;
cin>> cmonth;
cin.ignore();
system("cls");
cout<<"What day of the month is it currently? (example: 10) \n";
cin>> cday;
cin.ignore();
system("cls");

cout<<"You have been sleeping for: "<< (cyear - byear)*(spd/24) <<" years, " << (cmonth - bmonth)*(spd/24) <<" months, and "<< (cday - bday)*(spd/24) <<" days.\n(Based on 8 hours of sleep a day, days rounded)" << endl;

cout<< "0. Quit" <<endl;
cout<< "1. Restart" <<endl;
cin>> cont;
cin.ignore();

} while (cont);
}```
----------------------------------------

The problem is that for some reason, when I put in say, 10 for cyear, cday, cmonth, byear, and bmonth, but put in 15 bday, and 1 spd, it comes out with 0 days, 0 years, and 0 months. do you know why?

thanks.

2. What happened to the code Tags and the code indentation, please use code tags

Code:
```#include <iostream>

using namespace std;

int bday;
int bmonth;
int byear;
int cday;
int cmonth;
int cyear;
char cont;
int spd;

int main()
{
do
{
bday=0;
cday=0;
byear=0;
cyear=0;
cmonth=0;
bmonth=0;
spd=0;

system("cls");
cout<< "SLEEPY TIME!" << endl;
cout<< "This is a program that calculates how much you have time you have slept in your entire life. Hit ENTER to continue." << endl;
cin.get();
system("cls");
cout<<"How many hours on average do you sleep in a night?" << endl;
cin>> spd;
system("cls");
cout<<"What year were you born in? (example: 1994)" << endl;
cin>> byear;
cin.ignore();
system("cls");
cout<<"What month of the year were you born in? Please represent numerically (january: 1 , february: 2 , etc.)" << endl;
cin>> bmonth;
cin.ignore();
system("cls");
cout<<"What day of the month were you born? (example: 10)" << endl;
cin>> bday;
cin.ignore();
system("cls");
cout<<"What year is it currently? (example: 1994)" << endl;
cin>> cyear;
cin.ignore();
system("cls");
cout<<"What month is it currently? Please represent numerically (january: 1 , february: 2 , etc.)" << endl;
cin>> cmonth;
cin.ignore();
system("cls");
cout<<"What day of the month is it currently? (example: 10) \n";
cin>> cday;
cin.ignore();
system("cls");

cout<<"You have been sleeping for: "<< (cyear - byear)*(spd/24) <<" years, " << (cmonth - bmonth)*(spd/24) <<" months, and "<< (cday - bday)*(spd/24) <<" days.\n(Based on 8 hours of sleep a day, days rounded)" << endl;

cout<< "0. Quit" <<endl;
cout<< "1. Restart" <<endl;
cin>> cont;
cin.ignore();

} while (cont);
}```
ssharish

3. all your variables are ints and when division is performed on ints, they're rounded down

use doubles or floats instead

4. spd is an int, and will (presumably) have a value < 24. spd/24 will be computed using integer arithmetic, which (for positive values of spd) rounds down towards zero. This means that, if spd is between 0 and 23, then spd/24 will always have a value of zero.

5. Hey, I think it will not terminate even if I enter 0? Hmmm ...

6. @manav
look at the last 5 lines of code
it prompts the user for input and stores it in cont
If cont is false (false = 0, true = 1), then it goes out of the loop

7. Yeah! And cont is declared as char!
So it will either be '0' or '1' (if right values are entered) and none of them is false!

Or ... am I wrong?

8. right, didn't see it's type. The condition will always be true.

9. ## ...

Why did I make cont a char? Duh.. Better fix that. So what should I do with spd?

EDIT: Oh, nvm i know what to do. Thanks for all the help guys.

Popular pages Recent additions