Hi friends :P

I'm needing some help on an assignment. I'm really not sure about all the argument-passing and structure references I have been assigned for my C++ class. Any help is greatly appreciated. I'll continue to work on it in the meantime, but I cannot get it to compile yet without any errors. Thank you!

// ----------------------------------------------------------------- 
// This program asks the user for two different time values and 
// returns the sum as the 'result'.  The user is able to enter an  
// unlimited number of cases and each one's results.
// ----------------------------------------------------------------- 

#include <iostream>
using namespace std;
		
struct Time					//declare Time structure
{	
	int Hours;					//Hour of Time
	int Minutes;				//Minute of Time
	int Seconds;				//Second of Time
};						//Necessary semi-colon

//declarations
void Input_Time(Time&, int, int, int); 						//Inputs the time value & returns it using a reference parameter
Time Input_Time(int, int, int);					 		//Inputs the second time value & returns it using the "return" statement
long Time_To_Seconds(Time, Time);	 					//Accepts a time value and returns a code indicating valid or invalid data
Time Seconds_To_Time(long);		 					//Accepts a time value for printing and returns nothing
int Validate_Time(Time); 							//Accepts time value and returns the total seconds
void DisplayTime(Time); 							//Accepts total seconds and returns a time value in hours, minutes, seconds
void Print_Zero(int);

int main()
{
	Time t1, t2, t3, final;
	long totalSeconds;

	cout << endl
	     << "This program will input two times in HR:MN:SC " << endl
	     << "and output the sum of both in HR:MN:SC." << endl
	     << endl;

	for(case=1; case < 9999; case++;)
	{
	t1 = Input_Time(Time&, int)
	t2 = Input_Time()
	totalSeconds = Time_To_Seconds(Time t1, Time t2);	//sum of t1 & t2
	
	cout << endl;
	
	cout << "\nTime 1: "; DisplayTime(t1);
    	cout << "\nTime 2: "; DisplayTime(t2);
   	cout << "\nResult: "; DisplayTime(final);
   	cout << endl;
    	return 0;
	}
}

void Input_Time(Time&)
{
	cout << "Time 1: ";
	cin >> t1.hours >> t1.Minutes >> t1.Seconds;
}

Time Input_Time()
{
	cout << "Time 2: ";
	cin >> t2.Hours >> t2.Minutes >> t2.Seconds;
	return t2;
}

//Time_To_Seconds()
//Accepts two Time structures and returns the sum of total seconds as a long datatype
long Time_To_Seconds(Time t1, Time t2)			
  {
  Time t3, 
  long totalSeconds;			//define a new structure for sum
  
  t3.Seconds = t1.Seconds + t2.Seconds;	//add the seconds  
  t3.Minutes = (60*(t1.Minutes + t2.Minutes));//add the minutes and convert to seconds
  t3.Hours = (3600*(t1.Hours + t2.Hours));	//add the hours and convert to seconds
  totalSeconds = t3.Hours + t3.Seconds + t3.Minutes;
  }

Time Seconds_To_Time(long totalSeconds)
{
	Time final;
	
	if(totalSeconds >= 3600)
	{
	totalSeconds -= 3600;
	final.Hours++;
    	}
	if(totalSeconds >= 60)
    	{
    	final.Seconds -= 60;
    	final.Minutes++;
    	}
	return final;
}

//Validate_Time()
int Validate_Time(Time final)
{
  int 0, 1;

  if
  {
	  final.Hours > 23 || final.Minutes > 59 || final.Seconds > 59;
	  cout << "1";
  }
  else
  {
	  cout << "0";
  }
  return;
}
  
//DisplayTime()
//Display structure of type Time in Hours, Minutes, and Seconds
void DisplayTime(Time)						//time tt of type Time
{
	cout << tt.Hours << "\n:" << tt.Minutes << "\n:" << tt.Seconds << "\n:";
}

void Print_Zero(int)  		//Accepts an hour, minute, or second value, 
			//and prints only a zero if the value is < 10.  
			//If the value passed is > 10, nothing is printed.
			//Called from Display_Time()

line 36: case is a c++ keyword so use another variable name in that loop.

line 38: you have to put variable names inside the parentheses, not data types.

Hi friends :P

I'm needing some help on an assignment. I'm really not sure about all the argument-passing and structure references I have been assigned for my C++ class. Any help is greatly appreciated. I'll continue to work on it in the meantime, but I cannot get it to compile yet without any errors. Thank you!

You have other problems before that. Take them one by one, look at the error messages. You need to program in stages. When you get an error message, generally look a the first one and ignore all the later ones. For one,

for(case=1; case < 9999; case++;)

This has three errors. One, what is case ? An integer, A char? A function? You never declare it, so the compiler has no idea. Two, use a different variable name from case since case is a reserved word. Three, get rid of the semicolon after ++ .

This makes no sense inside of a function:

t1 = Input_Time(Time&, int)

You don't use Time and int in the function call, you use it in the function declaration. You need to specify variable names in the function call. Leave Time and int out since the variables will already be of type Time and int .

Looks like Ancient Dragon beat me to it!

for(case=1; case < 9999; case++;)
	{	
                t1 = Input_Time(Time&, int)
	t2 = Input_Time()
	totalSeconds = Time_To_Seconds(Time t1, Time t2);

these lines should be replaced with:

for(int i=1; i < 9999; i++)
	{	
                Input_Time(t1)
	t2 = Input_Time()
	totalSeconds = Time_To_Seconds(t1, t2);

this line (18):

void Input_Time(Time&, int, int, int);

should be this instead:

void Input_Time(Time&);

and this line (19):

Time Input_Time(int, int, int);

should be:

Time Input_Time();

this line (52):

void Input_Time(Time&)

should be:

void Input_Time(Time& t1)

I'm not sure that overloading is at all neccessary here. In fact, I think it's a mistake. Get rid of the second, Time returning Input_Time function, and just use the void function.

Replace the , on line 69 with a ;

Your if statement on line 100 is syntactically erronous. That needs a fixin.

Validate_time and time_to_seconds aren't returning anything when they should be.

DisplayZero is redundant. Use the iomanip lib with setw() and setfill() to achieve this.

Wow...either your classes teaching style is rediculously fast or you haven't been paying attention in class!

I'm still not sure what to put in Print_Zero, but I've improved on the code quite a bit. The Validate_Time isn't working very well, but I'm beginning to understand the structure better. I need help with these two things, and I think the seconds aren't set up to wrap around if it reaches 24.. Thank you in advance for all the help :D

//      
//      
//	 
//	 
//	 
// 
// ----------------------------------------------------------------- 
// This program asks the user for two different time values and 
// returns the sum as the 'result'.  The user is able to enter an  
// unlimited number of cases and each one's results.
// ----------------------------------------------------------------- 

#include <iostream>
using namespace std;
		
struct Time					//declare Time structure
{	
	int Hours;					//Hour of Time
	int Minutes;				//Minute of Time
	int Seconds;				//Second of Time
};							//Necessary semi-colon

//declarations
void Input_Time(Time&);	//Inputs the time value & returns it using a reference parameter
Time Input_Time();		//Inputs the second time value & returns it using the "return" statement
long Time_To_Seconds(Time, Time); //Accepts a time value and returns a code indicating valid or invalid data
Time Seconds_To_Time(long);//Accepts a time value for printing and returns nothing
int Validate_Time(Time); 	//Accepts time value and returns the total seconds
void DisplayTime(Time); //Accepts total seconds and returns a time value in hours, minutes, seconds
void Print_Zero(int);

int main()
{
	Time t1, t2, final;
	long totalSeconds;

	cout << endl
	     << "This program will input two times in HR:MN:SC " << endl
	     << "and output the sum of both in HR:MN:SC." << endl
	     << endl;

	for(int i=1; i < 9999; i++)
	{
	Input_Time(t1);
	cout << endl;
	t2 = Input_Time();
	totalSeconds = Time_To_Seconds(t1, t2);	//totalSeconds is the sum of tt1 & tt2
	final = Seconds_To_Time(totalSeconds);//Time final is the sum of both times in military format
	int validTime1 = Validate_Time(t2);
	int validTime2 = Validate_Time(t1);
	int validTime3 = Validate_Time(final);
	if ((validTime1 == 1) && (validTime2 == 1) && (validTime3 == 1))
	{
		cout << "\nTime 1: "; DisplayTime(t1);
		cout << "\nTime 2: "; DisplayTime(t2);
		cout << "\nResult: "; DisplayTime(final);
		cout << "Invalid time was entered";
		cout << endl;
		continue;
	}
	else
	{
		cout << "\nTime 1: "; DisplayTime(t1);
		cout << "\nTime 2: "; DisplayTime(t2);
		cout << "\nResult: "; DisplayTime(final);
		cout << endl;
	}
	}
}

void Input_Time(Time& t1)
{
	cout << "Time 1: ";
	cin >> t1.Hours >> t1.Minutes >> t1.Seconds;
}

Time Input_Time()
{
	Time t2;
	cout << "Time 2: ";
	cin >> t2.Hours >> t2.Minutes >> t2.Seconds;
	return t2;
}

//Time_To_Seconds()
//Accepts two Time structures and returns the sum of total seconds as a long datatype
long Time_To_Seconds(Time t1, Time t2)			
  {
  Time t3;								//define a new structure for sum
  long totalSeconds;			 		
  
  t3.Seconds = t1.Seconds + t2.Seconds;		//add the seconds, will convert later
  t3.Minutes = (60*(t1.Minutes + t2.Minutes));	//add the minutes and convert to seconds
  t3.Hours = (3600*(t1.Hours + t2.Hours));		//add the hours and convert to seconds
  totalSeconds = t3.Hours + t3.Seconds + t3.Minutes;
  return totalSeconds;
  }

Time Seconds_To_Time(long totalSeconds)
{
	Time final;
	final.Hours = 0;
	final.Minutes = 0;
	final.Seconds = 0;
	if(totalSeconds >= 3600)
	{
		totalSeconds -= 3600;
		final.Hours++;
    }
	if(totalSeconds >= 60)
    	{
    	totalSeconds -= 60;
    	final.Minutes++;
    	}
	return final;
}

//Validate_Time()
//This 
int Validate_Time(Time tt)
{
  int validTime = 0;
  if (tt.Hours > 23 || tt.Minutes > 59 || tt.Seconds > 59)
  {
	  validTime = 1;
  }
  else
  {
	  validTime = 0;
  }
  return validTime;
}
  
//DisplayTime()
//Display structure of type Time in Hours, Minutes, and Seconds
void DisplayTime(Time tt)						//time tt of type Time
{
	cout << tt.Hours << ":" << tt.Minutes << ":" << tt.Seconds << endl;
}

void Print_Zero(int)  			//Accepts an hour, minute, or second value, 
{						//and prints only a zero if the value is < 10.  
						//If the value passed is > 10, nothing is printed.
}						//Called from Display_Time()

First of all, Valid_time is backwards. It is returning the opposite of what you are expecting. (it returns a 1 when you expect a 0 and vice versa). This should fix a few problems. Also, there are a few redundancies and bad habits i noticed: (lie 49)

int validTime1 = Validate_Time(t2);	int validTime2 = Validate_Time(t1);	int validTime3 = Validate_Time(final);	if ((validTime1 == 1) && (validTime2 == 1) && (validTime3 == 1))	{

You dont need to declara and assign a bunch of variables just to be used in the if statement. This is a waste of resources.

Instead, use the function directly in the if statement:

if (Validate_Time(t2) && Validate_Time(t1) && Validate_Time(final))	{

Also, the nature of the Validate_Time function sugguests that it will only return 2 values - true and false (1 and 0). So it would be preferable to make it of return type bool rather than int.

Instead of using Print_Zero to display zeros where there are no numbers, use this:

#include<iomanip>
#include<iostream>
using namespace std;

int main()
{
int MINUTES = 5;
int HOURS = 15;
int SECONDS = 3;
cout << setw(2) << right << setfill('0') << HOURS << ":" << setw(2) << right << setfill('0') << MINUTES << setw(2) << setfill('0') << right << SECONDS;
return 0;
}

Outputs: 15:05:03

setw(2) sets the spaces allowed to be filled by characters. setfill('0') sets the fill of whitespace within this area to 0's. right will allign the number to the right side of the whitespace (now filled with zeros).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.