Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

input reading

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi guys!
Ok, what Im trying to do is convert an input of time in hh:mm:ss to total seconds and vice versa. I have quite easily converted user input seconds to hh:mm:ss form (eg. 4356 s = 01:12:36)
Now im having trouble converting from hh:mm:ss to total seconds. The problem is I have to account for different inputs. eg. input 55 = 55 seconds or
input 22:55 = 1375 seconds or
input 1:22:55 = 4975 seconds.
the input can include none, one or two colons... Ive been trying to use cin.get(char c) while (c != ':') .... and ive tried using a counter to count the colons then
if (count == 2){...}
else if (count == 1) {...}
else if (count == 0) {...}
etc. But im still having trouble.
Can anyone possibly help a poor sap like me with any possible suggestions?? THnx
 
Ok now i have this:
char hms[20];
size_t size = 0;
..........
cin.getline(hms, 21, '\n');
size = strlen(hms);
for (int i = 0; i < size; i++)
{
c = hms;
if (c == ':')
count++;
}
this way i can store the string and count the colons.
But still, now i have to somehow separate the int's between the colons and store them separately. eg. store in int seconds, minutes, hours;
Any ideas on how to separate the values between the colons???? thnx
 
once u count the no of colons then u can find out whether hh,mm,ss
is available... based on that try storing the values into three
temporary variables and then use atoi funtion to convert those
strings to integer....

hope u get me... any doubts plz do reply...


regards


sridharan
 
try this ...

but u gotta do some checking to make sure that user doesn't enter more than 2 digits for hh mm or ss ...... otherwise this will give u an access violation error....

Code:
#include <stdio.h>
#include <stdlib.h>

main()
{
	char szString[50];
	char szHH[3]=&quot;&quot;,szMM[3]=&quot;&quot;,szSS[3]=&quot;&quot;;
	int nHour, nMinutes, nSeconds;
	int nColonCount=0,nCurrentCount=0;
	int i,j=0;
	
	printf(&quot;Enter The Time in HH:MM:SS format &quot;);
	scanf(&quot;%s&quot;,szString);

	printf(&quot;The Entered String Is : %s\n\n&quot;,szString);

	for(i=0;szString[i]!='\0';i++)
	{
		if(szString[i]==':')
		{
			nColonCount++;
		}
	}

	printf(&quot;The No Of Colons : %d\n\n&quot;,nColonCount);

	if(nColonCount>2)
	{
		printf(&quot;Invalid Time Format&quot;);
	}
	else
	{
		for(i=0;szString[i]!='\0';i++)
		{
			if(szString[i]!=':')
			{
				if(nColonCount==2)
				{
					if(nCurrentCount==0)
					{
						szHH[j]=szString[i];
						j++;
					}
					else if(nCurrentCount==1)
					{
						szMM[j]=szString[i];
						j++;
					}
					else
					{
						szSS[j]=szString[i];
						j++;
					}	
				}
				else if(nColonCount==1)
				{
					if(nCurrentCount==0)
					{
						szMM[j]=szString[i];
						j++;
					}
					else
					{
						szSS[j]=szString[i];
						j++;
					}	
				}
				else if(nColonCount==0)
				{
					szSS[j]=szString[i];
					j++;
				}
			}
			else
			{
				if(nColonCount==2)
				{
					if(nCurrentCount==0)
					{
						szHH[j]='\0';
						j=0;
						nCurrentCount++;
					}
					else if(nCurrentCount==1)
					{
						szMM[j]='\0';
						j=0;
						nCurrentCount++;
					}
					else
					{
						szSS[j]='\0';
						j=0;
					}	
				}
				else if(nColonCount==1)
				{
					if(nCurrentCount==0)
					{
						szMM[j]='\0';
						j=0;
						nCurrentCount++;
					}
					else
					{
						szSS[j]='\0';
						j=0;
					}	
				}
				else if(nColonCount==0)
				{
					szSS[j]='\0';
					j=0;
				}
			}
		}
	}

	nHour = atoi(szHH);
	nMinutes = atoi(szMM);
	nSeconds = atoi(szSS);

	printf(&quot;%s:%s:%s\n\n&quot;,szHH,szMM,szSS);

	printf(&quot;In Integer Values:\n&quot;);

	printf(&quot;%d:%d:%d\n&quot;,nHour,nMinutes,nSeconds);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top