私はゆっくりと自分で c++ を学ぼうとしていますが、関数の使用に行き詰まりました。最初の問題を乗り越える方法を見つけましたが、最初に意図したようにできなかった理由がわかりません。これが作業プログラムです。
// ex 6, ch 2
#include <iostream>
using namespace std;
void time(int, int);
int main()
{
int h, m;
cout << "Enter the number of hours: ";
cin >> h;
cout << endl;
cout << "Enter the number of minutes: ";
cin >> m;
cout << endl;
time(h, m);
cin.get();
cin.get();
return 0;
}
void time(int hr, int mn)
{
cout << "The time is " << hr << ":" << mn;
}
そして、これが私がやりたい方法です。
// ex 6, ch 2
#include <iostream>
using namespace std;
void time(int, int);
int main()
{
int h, m;
cout << "Enter the number of hours: ";
cin >> h;
cout << endl;
cout << "Enter the number of minutes: ";
cin >> m;
cout << endl;
cout << "The time is " << time(h, m);
cin.get();
cin.get();
return 0;
}
void time(int hr, int mn)
{
cout << hr << ":" << mn;
}
私の頭では、どちらも同じものを返しますが、コンパイラはそうではないと考えています(理由を知りたいです)。
編集:奇妙な理由でこのように機能するようです。
cout << "The time is ";
time(h, m);
それ以上ではないにしても、それは私をより混乱させました。