[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

HELP???

/*******************************************************************
Project         33
Author          Cuong Nguyen
Purpose         Given as input three integers representing a date
                as day, month, year, print out the number day, month
                and year for the following day's date.
*******************************************************************/
#include <iostream.h>
#include <stdlib.h>


int main()
{
      int day, month, year;

      //Get input from the users
      cout <<"Enter the date: ";
      cin >> day;
      cout <<"Enter the month: ";
      cin >> month;
      cout <<"Enter the year: ";
      cin >> year;

      while (true)
      {
         int day = getNumericalInput("Day", 1, 31, -1);
         if (day < 0) ...
         int month = getNumericalInput("Month", 1, 12, -1);
         if (month < 0) ...
         int year = getNumericalInput("Year", 1900, 2050, -1);
         if (year < 0) ...

         if (!checkDate(day, month, year))
             ...
         
         int day1 = day;
         int month1 = month;
         int year1 = year;
         getNextDay(day1, month1, year1);
         printResult(day, month, year, day1, month1, year1);
      }

      system("PAUSE");
      return 0;
}

I dont know what should i do next, and i also have to do following functions (at least)

HandleError(int ErrorCode)
DaysInMonth(int Month)
DayIsValid(int Day, int Month)
MonthIsValid(int Month)
0
cuong5985
Asked:
cuong5985
  • 2
1 Solution
 
Jaime OlivaresCommented:
this question is the same as:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21377276.html

But I don't agree with itsmeandnobodyelse's suggestion to use lots of functions to solve the problem.
You can do it all in the main function:

int main()
{
      int day, month, year;

      //Get input from the users
      cout <<"Enter the date: ";
      cin >> day;
      cout <<"Enter the month: ";
      cin >> month;
      cout <<"Enter the year: ";
      cin >> year;

      // increment date here
      // first increment day
      // check if you has surpassed the number of days of specific month
      // if so, increment month, and set day to 1
      // check if you has surpassed month 12
      // if so, increment year, and set month to 1

      // print calculated day, month and year

      system("PAUSE");
      return 0;
}
0
 
itsmeandnobodyelseCommented:
>>>> But I don't agree with itsmeandnobodyelse's suggestion to use lots of
>>>> functions to solve the problem.

That wasn't my idea but a requirement of the asker.

Cuong,

the getNumericalInput function was supposed to do the cout/cin job you added at the beginning of main function. Furthermore, it should check upper and lower bounds of input and return an error value if the user cancels the input.

By moving the user I/O in the loop you could enter dates for testing as long as you didn't want to quit and you don't have to restart your prog when starting a new test.

int getNumericalInput(const string& prompt, int low, int upp, int err)
{
     int input;
     while (true)
     {
           cout << prompt;
           cin >> input;
           if (input < low || input > upp)
           {
                 cout << "The input value should be between " << low << " and " << upp << endl;
                 cout << "Do you want to quit (y/n) ? ";
                 char c;
                 cin >> c;
                 if (c == 'n' || c=='N')
                     return err;
                 cout << endl;      
           }
           else
                break;
     }
     return input;
}

>>>> HandleError(int ErrorCode)
>>>> DaysInMonth(int Month)
>>>> DayIsValid(int Day, int Month)
>>>> MonthIsValid(int Month)

Where do you know, that you have to implement these functions?

1. HandleError

You could call HandleError where I made ... in my code. Pass the error code that was returned by getNumericalError (passed as last argument) or use a list of new error codes (e. g. 1001 for "Invalid day entered", 1002 for "Invalid month entered"). HandleError then would output the error code and an appropriate text and finally call exit(ErrorCode) to stop the application.

2. DaysInMonth

That function simply returns the number of days of each month, e. g. 31 for month 1 == january. You could use a switch statement like

     switch(Month)
     {
     case 1: return 31;
     ...
     case 12: return 31;
     default: return -1;  // error
             
     }

3. DayIsValid(int Day, int Month)

That is equivalent to my checkDate function, though the Year is missing in the argument list. Without a year you couldn't check 29-Feb days in leap years. You could use the DaysInMonth function to check if the given Day fits to the given Month.

4. MonthIsValid

That only checks if the given month is between 1 and 12. That check you can omit as the getNumericalInput function already does the job.

Note, I would have provided full code if you would show some efforts to apply the hints you already got from me and others.

Regards, Alex

0
 
cuong5985Author Commented:
This is what i can do

/*******************************************************************
Project         33
Author          Cuong Nguyen
Purpose         Given as input three integers representing a date
                as day, month, year, print out the number day, month
                and year for the following day's date.
*******************************************************************/
#include <iostream.h>
#include <stdlib.h>

int getNumericalInput(const string& prompt, int low, int upp, int err);
double DaysInMonth(int Month);
double DayIsValid (int Day, int Month);
double MonthIsValid (int Month)

int main()
{
      int Day, Month, Year;

      //Get input from the users
      cout <<"Enter the date: ";
      cin >> Day;
      cout <<"Enter the month: ";
      cin >> Month;
      cout <<"Enter the year: ";
      cin >> Year;

      Day = getDay;
      Month = getMonth;
      Year = getYear;

      if Day > DaysInMonth
         Day = 1
         Month++

      if Month > 12
         Month = 1
         Year++



      system("PAUSE");
      return 0;
}

int getNumericalInput (const string& prompt, int low, int upp, int err)
/*********************************************************************
Function Name:
Purpose:
Arguments:
Returns:
*********************************************************************/
{
      int input;
      while(true)
      {
         cout <<prompt;
         cin >>input;
         if (input < low || input >> upp)
         {
            cout <<"The input value should be between "<<low<<"and"<<upp<<endl;
            cout <<"Do you want to quit (y/n)?";
            char c;
            cin >>c;
            if (c == 'n' || c == 'N')
               return err;
            cout <<endl;
         }
         else
            break;
         }
         return input;
}



double DaysInMonth (int Month)
/*********************************************************************
Function Name:  DaysInMOnth
Purpose:  The function simply returns the number of days of each month
Arguments: month
Returns:  days in month
*********************************************************************/
{
   switch(Month)
   {
   case 1: return 31;
   case 2: return 28;
   case 3: return 31;
   case 4: return 30;
   case 5: return 31;
   case 6: return 30;
   case 7: return 31;
   case 8: return 31;
   case 9: return 30;
   case 10: return 31;
   case 11: return 30;
   case 12: return 31;
   default: return -1; //error
   }
}

double DayIsValid (int Day, int Month)
/*********************************************************************
Function Name:
Purpose:
Arguments:
Returns:
*********************************************************************/
{
    if (!CheckDate (day, month, year))

    int day1= day;
    int month1= month;
    int year1 = year;
    getNextDay (day1, month1, year1);
    printResult (day, month, year, day1, month1, year1);
}

double MonthIsValid (int Month)
/*********************************************************************
Function Name:
Purpose:
Arguments:
Returns:
*********************************************************************/
{
   int month = getNumericalInput("Month", 1, 12, -1);
   if (month < 0)
      return false;
}
0
 
itsmeandnobodyelseCommented:
Did you compile your main function?
Did you compile any of the code you posted above?

I still have the suspicion you would like me to do your homework for you.

If that above is your very first C++ program and you've never compiled a C++ program, you have to tell that and we could help you doing the first steps. But the projects you posted here and in the previous thread are no beginner projects. You must at least know of all primitive data types, of basic C++ syntax, e. g. any statement must be terminated by a semicolon, of functions and function prototypes, of  include statements and standard libraries and their header files, e. g. time.h, of loops, switch statements and return statements, to have a chance even to unterstand a solution or hint we give you here.

Yesterday, I gave you a main function that would have compiled if you replaced some ... by a simple "return 1;" and if you declared a few function prototypes of the functions I used.

Today, you posted my original main function including all "..." parts and added some cout/cin statements that have been wrong where you put them as - obviously - user I/O was supposed to be done by a function 'getNumericalInput'. Today, I also gave you an implementation of getNumericalInput, but you were not able to apply it int the main function, though you already had the code.

I actually don't know what I should do with you as you are not honest to us. Listen, I could give you the code of all your projects but if your teacher would ask one single question about it, you wouldn't be able to explain. So, it would be bad for you if I would do it that way even if it wouldn't be against EE rules.

So, the only thing left for you, is to ask questions. Take the code I gave you and compile it. If you get an error message that you don't understand, ask for it. I will answer it.

Regards, Alex
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now