#include <iostream>
#include <stdlib.h>
using namespace std;

void GetTime ( int*, int*, int* );
int CalcSeconds ( int, int, int );

int main()
{
    int hr, min, sec;
    char another;
    int total_sec;
    
    cout << " \n\n Welcome to a time-conversion program ! \n\n " ;
    
    do
    {
         GetTime( &hr,&min, &sec);
         total_sec = CalcSeconds( hr, min, sec);
         cout << "\n\n You entered " << hr << ":" << min << ":" << sec;
         cout << "\n It is" << total_sec << " seconds " ;
         
         cout << "\n\n Do another ? y or n ";
         cin >> another ;
    } while ( another == 'y'|| another =='Y');
    cout << "\n THanks for playing! \n ";
    return 0 ;
}
void GetTime ( int *h_ptr , int *m_ptr, int *s_ptr)
{
     int ask_them_again = 1;
     char colon;
     while ( ask_them_again =1 )
     {
                cout << " \n Please enter the time in hours minutes second";
                cout << " \n Such as 4:15:34 ( Note : Minutes and Seconds < 60 ) \n\n==> ";
                cin >> *h_ptr >> colon >> *m_ptr >> colon >> *s_ptr;
                ask_them_again =0 ;
                
                if ( *m_ptr > 59 || *s_ptr > 59 )
                {
                     cout << " \n WHOA !! Invalid Time !!! " ;
                     ask_them_again = 1; 
                     }
                     }
                     }
int CalcSeconds ( int hr, int min, int sec)
{
    int total_sec;
    total_sec = hr*3600 + min*60+ sec ;
    return total_sec;
}

the program was running, but it still left some step

Recommended Answers

All 10 Replies

cin >> *h_ptr >> colon >> *m_ptr >> colon >> *s_ptr;
This statement is wrong. Tell your user to separate hours, minutes, second by spaces (Like this : "5 15 30") and remove the colon cin >> *h_ptr >> *m_ptr >> *s_ptr; If you want the user to be able to input time like HH:MM:SS then you will need to take the input in a string and then parse it.

no man, I tried either way, but its not work and still repete the loop ( do while )

Thats because you are mistakenly using an = instead of == in the
while ( ask_them_again =1 )

change it to
while ( ask_them_again ==1 )

Here's an example on how to get the time:
I don't know whether it's good to recommend you, but you can also achieve the same by using scanf from the standard C library (cstdio):

int hours, minutes, seconds;
scanf("%d%*c%d%*c%d", &hours, &minutes, &seconds);

or you could rewrite it in C++ (recommended way) as:

int hours, minutes, seconds;
char c;
cin >> hours >> c >> minutes >> c >> seconds;

These snippets will allow the user to input the time as 15:43:22 for example :)

BTW, if your compiler supports it, then use #include <cstdlib> instead of #include <stdlib.h> .

add

system("CLS")

so u can see clearly each time.

#include <iostream>
#include <stdlib.h>
using namespace std;

void GetTime ( int*, int*, int* );
int CalcSeconds ( int, int, int );

int main()
{
    int hr, min, sec;
    char another;
    int total_sec;
    
    cout << " \n\n Welcome to a time-conversion program ! \n\n " ;
    
    do
    {
         GetTime( &hr,&min, &sec);
         total_sec = CalcSeconds( hr, min, sec);
         cout << "\n\n You entered " << hr << ":" << min << ":" << sec;
         cout << "\n It is" << total_sec << " seconds " ;
         
         cout << "\n\n Do another ? y or n ";
         cin >> another ;
    } while ( another == 'y'|| another =='Y');
    cout << "\n THanks for playing! \n ";
    return 0 ;
}
void GetTime ( int *h_ptr , int *m_ptr, int *s_ptr)
{
     int ask_them_again = 1;
     char colon;
     while ( ask_them_again ==1 )
     {
				system("CLS");
                cout << " \n Please enter the time in hours minutes second";
                cout << " \n Such as 4:15:34 ( Note : Minutes and Seconds < 60 ) \n\n==> ";
                cin >> *h_ptr >> colon >> *m_ptr >> colon >> *s_ptr;
                ask_them_again =0 ;
                
                if ( *m_ptr > 59 || *s_ptr > 59 )
                {
                     cout << " \n WHOA !! Invalid Time !!! " ;
                     ask_them_again = 1; 
                     }
                     }
                     }
int CalcSeconds ( int hr, int min, int sec)
{
    int total_sec;
    total_sec = hr*3600 + min*60+ sec ;
    return total_sec;
}

>>add system("CLS")
Oh really? Did you see Tux's signature, the last point? It is definitely written for system("PAUSE") but if you ever opened it, you will find that system() commands are evil. Never use them.

So what to do? Nothing. Why would you want that your user terminal should be clear?
We, the Linux User, works much on the Command Prompt(we call it Terminal or Shell). We get really angry if a application clears our console windows.
So please don't clear the screen, your user may not like it. (it is Rude).

To the OP:Please mark this thread as solve, if you have done with your problem.

but you guys, when I enter the number, it doesn't show correctly

Welcome to a time-conversion program !


Please enter the time in hours minutes second
Such as 4:15:34 ( Note : Minutes and Seconds < 60 )

==> 4 15 34


You entered 4:5:4
It is 14704 seconds

Do another ? y or n

but you guys, when I enter the number, it doesn't show correctly

What's the code of this?
You probably did something wrong.

BTW, why don't you just pass these variables as a reference?
Let's do it the C++ way !

>but you guys, when I enter the number, it doesn't show correctly
It shows correctly on my machine. Here is the code I used:

#include <iostream>
#include <stdlib.h>
using namespace std;

void GetTime ( int*, int*, int* );
int CalcSeconds ( int, int, int );

int main()
{
    int hr, min, sec;
    char another;
    int total_sec;

    cout << " \n\n Welcome to a time-conversion program ! \n\n " ;

    do
    {
        GetTime( &hr,&min, &sec);
        total_sec = CalcSeconds( hr, min, sec);
        cout << "\n\n You entered " << hr << ":" << min << ":" << sec;
        cout << "\n It is" << total_sec << " seconds " ;

        cout << "\n\n Do another ? y or n ";
        cin >> another ;
    }
    while ( another == 'y'|| another =='Y');
    cout << "\n THanks for playing! \n ";
    return 0 ;
}
void GetTime ( int *h_ptr , int *m_ptr, int *s_ptr)
{
    int ask_them_again = 1;
    char colon;
    while ( ask_them_again ==1 )
    {
        cout << " \n Please enter the time in hours minutes second";
        cout << " \n Such as 4:15:34 ( Note : Minutes and Seconds < 60 ) \n\n==> ";
        cin >> *h_ptr >>*m_ptr >> *s_ptr;
        ask_them_again =0 ;

        if ( (*m_ptr > 59) || (*s_ptr > 59) )
        {
            cout << " \n WHOA !! Invalid Time !!! " ;
            ask_them_again = 1;
        }
    }
}
int CalcSeconds ( int hr, int min, int sec)
{
    int total_sec;
    total_sec = hr*3600 + min*60+ sec ;
    return total_sec;
}

Output:

Welcome to a time-conversion program ! 

  
 Please enter the time in hours minutes second 
 Such as 4:15:34 ( Note : Minutes and Seconds < 60 ) 

==> 4 15 34


 You entered 4:15:34
 It is15334 seconds 

 Do another ? y or n n

 THanks for playing!

Yes, on my machine it did also run correctly, did you follow siddhantes' instructions carefully?
(Nope, otherwise it would work)

while ( ask_them_again [B]=[/B]= 1 )
{
    cout << " \n Please enter the time in hours minutes second";
    cout << " \n Such as 4:15:34 ( Note : Minutes and Seconds < 60 ) \n\n==> ";
    cin >> *h_ptr >> colon >> *m_ptr >> colon >> *s_ptr;
.
.
.
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.