Hello everyone, i got a question, but cant figure it out
here's the question:

Declare a structure to hold the time. It should have 2 members: one for hours and one for
minutes (each as integers).
Declare another structure for date. It should have 3 members: one for day, one for month
and a third one for year.

Write a program which makes use of these two structures and prompts the user to enter
two times (in hours and minutes) i.e. a departure time + a journey time as well as the
departure date. The program should also be able to read a series of departure time ,
Journey time and departure date from a data file (input file). The program will then
add together these two times ensuring that the answer is valid (minutes are less than 60
and hours are less than24). Based on this the program will display the departure date, the
departure time, the journey time, the arrival time and the arrival date. The final resultant
time will then be displayed on the screen and to an output file.
Both the input times and the resulting time should be stored using the previously
declared structure.

i try it, but as my new with this i dont know if am goin in the rigth way.

here's the code:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    struct time
    { 
           int hours;
           int minutes;
    };
    struct date
    {
           int day;
           int month;
           int year;
    };
    
    int n,ID;
    static struct date departuredate[10];
    cout<<"Enter departure date\n";
    cin>>n;
    
    int i=0;
    while (i<n)
    {
          cout<<"Enter departure date\n";
          cin >> departuredate[i].day;
          cin >>departuredate[i].month;
          cin >> departuredate[i].year;
          i++;
          }
          for (i=0; i<n; i++)
          { 
              cout<<"departuredate:"<< departuredate[i].day<<"\t"<<departuredate[i].month"\t"<< departuredate[i].year << "\n" ;
          }
              
    system("PAUSE");
    return EXIT_SUCCESS;
}

Any help please
i reall y need it as soon as possible
please help
URGENT PLEASE!!

Edited 6 Years Ago by liveurlifeteddy: URGENT!!

Your structs look fine. I suggest hard coding some inputs so you can show us an example input, the output it produces, and tell us the output you would expect it to produce.

David

As David said, the strucs are fine. You may want to create a structure to hold the results of your calculations, too. From the problem description, it looks like you have quite a ways to go. I would suggest breaking the problem into a number of pieces:
1. Get input from the user. It is always good practice to prompt the user for the format of the input. In other words, say something like "Enter Day (1-31) Month (1-12) Year (yyyy)". Also, you have to check that each number they entered is valid, AND do something if it isn't.
2. Get input from a file. Do you have to prompt the user for a file name? You should still check the format of the input data.
3. Convert the dates. Often when people work with times, they convert the time into a single integer. Write a function to do this. For this problem, I would use a number of minutes. To do this, pick a start date. Count the number of FULL years since the starting date. Multiply by the number of minutes in a year. Add in the number of minutes in the FULL months since the beginning of the current year (each month is different!). Then add in the number of minutes in the FULL days since the start of the month. Add in number of hours (time 60, of course), and finally the number of minutes in the time. Now you have your date and time converted to an integer that is based on your start date. This will make things a lot easier, it the dates go across month or year boundaries.
4. Do the calculations. Since the times and dates are converted to integers, it is easy to add or subtract them.
5. Convert back to “normal” time and date. Just reverse what you did. Be careful because the number of minutes is different for each month.
6. Write the output.

In general, it is a lot easier to debug programs if you put all your intermediate values in variables when you do your calculations. This way, you can look at them with a debugger or print them out in diagnostic messages.

By breaking the program into pieces, you can get each piece tested and working without having to worry about the others pieces.

"0" is kind of arbitrary. It makes more sense to return EXIT_SUCCESS if everything went well, and EXIT_FAILURE otherwise. The 'system pause' I think is because some IDEs close the terminal right away so you can't inspect the results. I always thought this was pretty silly...

As David said, the strucs are fine. You may want to create a structure to hold the results of your calculations, too. From the problem description, it looks like you have quite a ways to go. I would suggest breaking the problem into a number of pieces:
1. Get input from the user. It is always good practice to prompt the user for the format of the input. In other words, say something like "Enter Day (1-31) Month (1-12) Year (yyyy)". Also, you have to check that each number they entered is valid, AND do something if it isn't.
2. Get input from a file. Do you have to prompt the user for a file name? You should still check the format of the input data.
3. Convert the dates. Often when people work with times, they convert the time into a single integer. Write a function to do this. For this problem, I would use a number of minutes. To do this, pick a start date. Count the number of FULL years since the starting date. Multiply by the number of minutes in a year. Add in the number of minutes in the FULL months since the beginning of the current year (each month is different!). Then add in the number of minutes in the FULL days since the start of the month. Add in number of hours (time 60, of course), and finally the number of minutes in the time. Now you have your date and time converted to an integer that is based on your start date. This will make things a lot easier, it the dates go across month or year boundaries.
4. Do the calculations. Since the times and dates are converted to integers, it is easy to add or subtract them.
5. Convert back to “normal” time and date. Just reverse what you did. Be careful because the number of minutes is different for each month.
6. Write the output.

In general, it is a lot easier to debug programs if you put all your intermediate values in variables when you do your calculations. This way, you can look at them with a debugger or print them out in diagnostic messages.

By breaking the program into pieces, you can get each piece tested and working without having to worry about the others pieces.

Hi, my PC broke down cant reply

here's the new coding and the date cant get it works:

#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
    
struct Time{
 int hours;
 int minutes;
 float times;
}deptime,jourtime,arrtime;

struct Date{
 int day;
 int month;
 int year;
}depdate,arrdate;

 ofstream myfile;
myfile.open ("Train.txt");
int n, i=0;

  cout<<"Please enter the amount of entry you want to make\n";
cin >> n;
   while (i<n)
   {
cout << "Enter the departure Date: \n";
    cin >> depdate.day >> depdate.month >> depdate.year;
    

cout << "Enter the departure time of train: \n";
    cin >> deptime.hours >> deptime.minutes;


    
if ( deptime.hours > 24 && deptime.minutes >=60){
      
     cout << "\n\n\n Hours should be less than 24 and minutes should be less than 60\n";
     }
else
cout <<"Departure Time: "<< deptime.hours <<"."<< deptime.minutes << endl;
cout <<"Departure Date: "<< depdate.day << "/"<< depdate.month<<"/"<<depdate.year<<endl;


cout << "Enter The Journey time of train: \n";
    cin >> jourtime.hours >> jourtime.minutes;
    
    
if ( jourtime.minutes >=60){
      
     cout << "\n\n\nminutes should be less than 60\n";
     }
else
cout <<"Journey Time: "<<  jourtime.hours <<"."<< jourtime.minutes << endl;



 arrtime.hours = (deptime.hours + jourtime.hours);
 arrtime.minutes = (deptime.minutes + jourtime.minutes);
 
 	if((arrtime.minutes>=60) && (arrtime.hours>=12))
 	{
        ++arrtime.hours,arrtime.hours=(arrtime.hours)%12;
		++arrtime.minutes,arrtime.minutes=(((arrtime.minutes)%60)-1);
		}



 
cout <<"\n\n\nDeparture Date: "<< depdate.day << "/"<< depdate.month<<"/"<<depdate.year<<endl;
cout <<"Departure Time: "<< deptime.hours <<"."<< deptime.minutes << endl;
 cout <<"Journey Time: "<<  jourtime.hours <<"."<< jourtime.minutes << endl;
cout <<"Arrival Time :" << arrtime.hours << "." << arrtime.minutes<<endl;
cout <<"Arrival Date :"<< arrdate.day<<"/"<<arrdate.month<<"/"<<arrdate.year<<endl;
    i++;
       } 
myfile <<"Departure Date"<<"\t"<<"Departure Time"<<"\t"<<"Journey Time"<<"\t"<<"Arrival Time"<<"\t"<<"Arrival Date"<<endl;
       for(i=0; i<n; i++)
{
       myfile <<depdate.day <<"/"<< depdate.month<<"/"<<depdate.year<<"\t";         
        myfile <<deptime.hours <<"."<< deptime.minutes << "\t\t";
        myfile <<jourtime.hours <<"."<< jourtime.minutes << "\t\t";
         myfile <<arrtime.hours << "." << arrtime.minutes<< "\t\t";
          myfile <<arrdate.day<<"/"<<arrdate.month<<"/"<<arrdate.year<<endl;
          }  
         myfile.close();

    system("PAUSE");
    return EXIT_SUCCESS;
}

Edited 6 Years Ago by liveurlifeteddy: n/a

This article has been dead for over six months. Start a new discussion instead.