C++菜鸟编程问题

来源:百度知道 编辑:UC知道 时间:2024/05/15 23:47:29
我是自己学的完全菜鸟入门
看的事C++ primer plus (5th)
本来看网上都推荐‘C++大学教程’, 但我书店找不到,也没电子书,不想在网上买····所以就看了前者。

做习题的时候有一个不会··求人给答案,,书没··

是第2章的最后一题,关于用户定义函数的
题目:
编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:
Enter the number of hours:9
Enter the number of minutes:28
Time 9:28

只用一个用户定义函数能显示出时间么?

#include <iostream.h>
void showtime(int h, int m)
{
cout<<h<<":"<<m<<endl;
}
void main()
{
int a,b;
cout<<"Enter the number of hours:"<<endl;
cin>>a;
if (a>24&&a<0)
cout<<"请输入0~24之间地数字"<<endl;
cout<<" Enter the number of minutes"<<endl;
cin>>b;
if (b>60&&b<0)
{
cout<<"请输入0~60之间的数字"<<endl;
}

showtime(a,b);

}

C语言的

#include <stdio.h>
void ShowTime(int h, int m)
{
printf("Time %d:%d\n", h,m);
}

main()
{
int h, m;
printf("Enter the number of hours:");
scanf("%d", &h);
printf("Enter the number of minutes:");
scanf("%d", &m);

ShowTime(h, m);