Help

I have to Develop a program that determines the number of days, hours, minutes and seconds it took a beacon's signal to travel to the Space Expedition's communication facility. The local receiver that catches the beacon's signal reports the travel time in seconds. Because the station's Executive Office was an English major, he is unable to convert the time from seconds into the required format and needs a program to do it for him.

The program should function as follows:

Ask the user for the time in seconds the signal traveled.
If the time is negative, display an error message to the user and exit.
Compute number of days, hours, minutes and seconds it took a beacon's signal to travel
Display the total travel time in proper units.

This is what I've written, I want someone opinion on how this looks and if there are any changes that I should make. Also, I have to write and "if" statement as the description says. How would I go about doing that? Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include <iostream>
using namespace std;

int main()
{
	double seconds = 60;
	double minutes = 1.0;
	double hours;
	double days;

	cout << "Please enter the time in seconds the signal traveled: ";
	cin >> seconds;

	double totalSeconds = seconds * minutes;
	double totalMinutes = totalSeconds / 60;
	double totalHours = totalMinutes / 60;
	double totalDays = totalHours / 24;

	cout << "\nTotal time in seconds: " << totalSeconds << endl;
	cout << "Total time in minutes: " << totalMinutes << endl;
	cout << "Total time in hours: " << totalHours << endl;
	cout << "Total time in days: " << totalDays << endl;

	cout << "\nPlease press ENTER to end program.";
	cin.sync();
	getchar();
	return 0;
}
Right, you'll need to add an if statement on line 13 after you read the input to ensure it is nonnegative, as the assignment requests. What don't you understand about if statements? Is it the syntax, or something else?

The rest looks good, except for what I think was a mistake in your reading of the problem. Your program is reporting the total time in different units; e.g., if the user puts in 60 seconds, it will display 60 seconds, 1 minute, 0.016... hours, etc.

I think the intent of the program is to "make change" with the time; instead of reporting it as you have done, an input of 60 seconds was supposed to output "0 days, 0 hours, 1 minute, 0 seconds", or something to that effect.
I'm not entirely sure If I had a mistake or not yet so I will look into it. Also, I'm still new to this and I don't have the 'If' statements all figured out yet. I would do something like this on line 13, for exmaple:

1
2
3
4
if ()
{
cout << " Error please exit the program." << endl;
}


I'm just not sure what to insert in the parenthesis.
You need to put in a boolean expression; i.e., an expression that evaluates to true or false. The expression you want here is actually quite simple, as all you want to do is check if the user's input is nonnegative (i.e., less than zero).
I've heard of the booleans but how would I set that up? Sorry for all the questions, even though this is easy I've only been doing this for 2 days.
Take a quick look at this section and see if you can figure it out:
http://www.cplusplus.com/doc/tutorial/operators/#relational
That section talks about relational operators and explains how to write boolean expressions with them.
Thanks for your help, I appreciate it :)
Topic archived. No new replies allowed.