一个C++的程序问题

来源:百度知道 编辑:UC知道 时间:2024/05/23 20:11:55
编写一个程序,当用户输入两个时刻以后,求出这两个时刻的时间差(按秒计算)并打印到屏幕上?

急求大侠的帮助~~~

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

int main()
{
int h1,m1,s1,h2,m2,s2,sum;
cout<<"enter a time :";
cin>>h1>>m1>>s1;

cout<<"\nenter another time :";
cin>>h2>>m2>>s2;
sum=fabs(h2-h1)*3600+fabs(m2-m1)*60+fabs(s2-s1);

cout<<endl<<sum<<endl;
}

#include<stdio.h>
void main()
{
int h1,m1,s1,h2,m2,s2,output;
printf("Input first time(H:M:S):");
scanf("%d:%d:%d",&h1,&m1,&s1);
while(h1>23 || h1<0 || m1>59 || m1<0 || s1>59 || s1<0)
{
printf("Error,input again:");
scanf("%d:%d:%d",&h1,&m1,&s1);
}
printf("Input second time(H:M:S):");
scanf("%d:%d:%d",&h2,&m2,&s2);
while(h2>23 || h2<0 || m2>59 || m2<0 || s