Thread: Math Problems?

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    20

    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.
    Last edited by Salem; 12-31-2007 at 01:35 AM. Reason: Added code tags, for all the good it'll do.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    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. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    all your variables are ints and when division is performed on ints, they're rounded down

    use doubles or floats instead

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Hey, I think it will not terminate even if I enter 0? Hmmm ...

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    @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

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  7. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    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. #8
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    right, didn't see it's type. The condition will always be true.

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    20

    ...

    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.
    Last edited by Suudsu2200; 12-31-2007 at 09:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. American math students ...
    By whiteflags in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 05-26-2008, 12:47 PM
  3. Random math problems
    By got1sleeve in forum C++ Programming
    Replies: 3
    Last Post: 01-23-2008, 11:20 PM
  4. making a math quiz program
    By mackieinva in forum C Programming
    Replies: 10
    Last Post: 09-17-2007, 03:38 PM
  5. Library for matrix math/ linear algebra?
    By The V. in forum C++ Programming
    Replies: 0
    Last Post: 09-25-2001, 10:36 PM