Skip to main content
added 873 characters in body
Source Link
Loki Astari
  • 96.6k
  • 5
  • 125
  • 338

###Algorithm:

This is good:

a=ih*60+im;
b=fh*60+fm;

Not sure what you are doing here:

e=abs(((780+a)/(781+b))*720-abs(a-b));

What are the 780/781/720 constants doing? If you remove all the extra stuff it should just work

e=abs(a-b); // Difference between the two times in minutes.

Then you print statement is fine:

cout<<"the elasped time is "<<e/60<<":"<<e%60;

###Variable Names I am sure you can come up with better names for your variables. The whole point is to try and make the code readable for the next person. So think up some meaningful names.

###Layout

Declare one variable per line. Just before you use it. Its even better if you initialize it as you declare it. It looks neater and is easy to read.

std::cout << "Enter initial hour:";
int firstHour;
std::cin >> firstHour;
....

....
int timeDifference  = abs(firstTimeInMinutes - secondTimeInMinutes);

###White Space to make it easier to read:

Add white space between operators so they stick out (and we don't have to squint to read your code)

cout<<"Enter initial hour:";
cin>>ih;

// Easier to read as:

std::cout << "Enter initial hour:";
std::cin >> ih;

###Validate input.

Your code looks good but you have no error checking. If anything but a number is typed in then the whole program crashes with undefined behavior.

if (std::cin >> ih) {
    // Correctly read a value from the stream
}
else {
    std::cout << "Error: Could not read a number\n";
    exit(1);
}

###Reading formatted text:

I would like to include hours and time in hr:min format.

C actually has a much better interface for reading formatted text:

int hour;
int min;
if (std::fscanf(stdin, "%d:%d", &hour, &min) == 2)
{
    // Read worked.
}

But it can be done with C++. You just need to manual check that the charter separator is a ':' character.

int hour;
int min;
char v;
if (std::cin >> hour >> v >> min && v == ':')
{
    // Read worked
}

###Algorithm:

This is good:

a=ih*60+im;
b=fh*60+fm;

Not sure what you are doing here:

e=abs(((780+a)/(781+b))*720-abs(a-b));

What are the 780/781/720 constants doing? If you remove all the extra stuff it should just work

e=abs(a-b); // Difference between the two times in minutes.

Then you print statement is fine:

cout<<"the elasped time is "<<e/60<<":"<<e%60;

###Variable Names I am sure you can come up with better names for your variables. The whole point is to try and make the code readable for the next person. So think up some meaningful names.

###Layout

Declare one variable per line. Just before you use it. Its even better if you initialize it as you declare it. It looks neater and is easy to read.

std::cout << "Enter initial hour:";
int firstHour;
std::cin >> firstHour;
....

....
int timeDifference  = abs(firstTimeInMinutes - secondTimeInMinutes);

###White Space to make it easier to read:

Add white space between operators so they stick out (and we don't have to squint to read your code)

cout<<"Enter initial hour:";
cin>>ih;

// Easier to read as:

std::cout << "Enter initial hour:";
std::cin >> ih;

###Algorithm:

This is good:

a=ih*60+im;
b=fh*60+fm;

Not sure what you are doing here:

e=abs(((780+a)/(781+b))*720-abs(a-b));

What are the 780/781/720 constants doing? If you remove all the extra stuff it should just work

e=abs(a-b); // Difference between the two times in minutes.

Then you print statement is fine:

cout<<"the elasped time is "<<e/60<<":"<<e%60;

###Variable Names I am sure you can come up with better names for your variables. The whole point is to try and make the code readable for the next person. So think up some meaningful names.

###Layout

Declare one variable per line. Just before you use it. Its even better if you initialize it as you declare it. It looks neater and is easy to read.

std::cout << "Enter initial hour:";
int firstHour;
std::cin >> firstHour;
....

....
int timeDifference  = abs(firstTimeInMinutes - secondTimeInMinutes);

###White Space to make it easier to read:

Add white space between operators so they stick out (and we don't have to squint to read your code)

cout<<"Enter initial hour:";
cin>>ih;

// Easier to read as:

std::cout << "Enter initial hour:";
std::cin >> ih;

###Validate input.

Your code looks good but you have no error checking. If anything but a number is typed in then the whole program crashes with undefined behavior.

if (std::cin >> ih) {
    // Correctly read a value from the stream
}
else {
    std::cout << "Error: Could not read a number\n";
    exit(1);
}

###Reading formatted text:

I would like to include hours and time in hr:min format.

C actually has a much better interface for reading formatted text:

int hour;
int min;
if (std::fscanf(stdin, "%d:%d", &hour, &min) == 2)
{
    // Read worked.
}

But it can be done with C++. You just need to manual check that the charter separator is a ':' character.

int hour;
int min;
char v;
if (std::cin >> hour >> v >> min && v == ':')
{
    // Read worked
}
Source Link
Loki Astari
  • 96.6k
  • 5
  • 125
  • 338

###Algorithm:

This is good:

a=ih*60+im;
b=fh*60+fm;

Not sure what you are doing here:

e=abs(((780+a)/(781+b))*720-abs(a-b));

What are the 780/781/720 constants doing? If you remove all the extra stuff it should just work

e=abs(a-b); // Difference between the two times in minutes.

Then you print statement is fine:

cout<<"the elasped time is "<<e/60<<":"<<e%60;

###Variable Names I am sure you can come up with better names for your variables. The whole point is to try and make the code readable for the next person. So think up some meaningful names.

###Layout

Declare one variable per line. Just before you use it. Its even better if you initialize it as you declare it. It looks neater and is easy to read.

std::cout << "Enter initial hour:";
int firstHour;
std::cin >> firstHour;
....

....
int timeDifference  = abs(firstTimeInMinutes - secondTimeInMinutes);

###White Space to make it easier to read:

Add white space between operators so they stick out (and we don't have to squint to read your code)

cout<<"Enter initial hour:";
cin>>ih;

// Easier to read as:

std::cout << "Enter initial hour:";
std::cin >> ih;