-5

I’m creating an application that generates Year, month, weeks and days from a user input I’ve tried this but only the years and months work So for example when I put in 30 days, it says 1 month, 2 weeks and 2 days instead of just 1 month Thanks

// Description: This program prompts the user for an integer,    which will represents the total
////number of days; The program then will break it apart into years, months, weeks, and days ////(each one of these will have their own local variables), and once this is done, the outcome will be     //displayed on the screen.
//Enter no.of days : 1234Years : 3Months : 4Weeks : 2Days : 5
#include <iostream>
#include <cmath>
using namespace std;
const int daysInWeek = 7;
const int days_in_month = 30;
const int days_in_year = 365;
const int days_in_days = 1;
int main()
{
 //Local variables
 int totalDays;
 int years;
 int months;
 int weeks;
 int days;

 //program info/intro
 cout << "My name is Diana\n";
 cout << "Program 1: Convert Number of Days to Years, Months,     Weeks, and Days" << endl;
 cout << "----------------------------------------------------------------    ----- \n";
 //get numbers and develop math progress
 cout << "Enter the total number of days : ";
 cin >> totalDays;

 years = totalDays / days_in_year;
 months = (totalDays % days_in_year) / days_in_month;
 weeks = (days_in_month % daysInWeek);
 /*weeks = (totalDays%days_in_year) / daysInWeek;*/
 days = (totalDays% days_in_year) % daysInWeek;

 // Display it in the screen

 cout << "          " <<  
 cout << "Years = " << years << 
 cout << "Months = " << months << endl;
 cout << "Weeks = " << weeks << endl;
 cout << "Days = " << days << endl;
 system("pause");
 return 0;
}
Saleem
  • 1
  • 2
  • 2
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Jan 17 '19 at 22:15
  • 2
    Hmmm `weeks = (days_in_month % daysInWeek);` looks like `weeks` will be the same every program run regardless of what `totalDays` is... – scohe001 Jan 17 '19 at 22:20
  • You probably want to clean up your code so it's easier for you to logically follow. One solution that'd make your code a lot cleaner is to subtract the number of days in the years out of the total days, so you don't need to wrestle with solving the equations with modulo. – Daniel Jan 17 '19 at 22:27
  • Possible duplicate of [How does the modulus operator work?](https://stackoverflow.com/questions/12556946/how-does-the-modulus-operator-work) – ordonezalex Jan 18 '19 at 01:48
  • 1
    Restored original question. Please do not vandalize your own question. – SergeyA Jan 18 '19 at 15:18

1 Answers1

1

As already suggested a better approach would be to keep track of the remaining days:

years = totalDays / days_in_year;
totalDays %= days_in_year;
months = totalDays / days_in_month;
totalDays %= days_in_month;
weeks = totalDays / days_in_week;
totalDays %= days_in_week;
days = totalDays;
Osiris
  • 2,783
  • 9
  • 17