私はC++を新しく使い、Do-Whileループを使用して1からnまでの合計を計算するプログラムを作成しようとしましたが、nは入力パラメータで、 nの階乗を計算するforループの階乗関数。しかし、プログラムがコンパイルされると、次のような結果が得られます。C++プログラムで正しい階乗値が返されない
合計1からn(この例ではnは5)は、001ED2A8またはその他の奇妙な数字と文字の組み合わせです。私の階乗結果についても同じことが起こります。私が得ることができるすべての助けに感謝します。ここで私はこれまで持っているものです。
#include "stdafx.h"
#include <iostream>
using namespace std;
int total(int);
int factorial(int);
void main()
{
int n;
cout << "Please enter a positive number:";
cin >> n;
cout << "The total from 1 to " << n << "is " << total << endl;
cout << "The factorial of " << n << " is: " << factorial << endl;
}
int total (int n)
{
int i, total;
total = 0;
i = 1;
do
{
total = total + i;
i = i + 1;
} while (total <= n);
return total;
}
int factorial (int n)
{
int product = 1;
for (;n>0; n--)
{
product = n * product;
}
return product;
}
デバッガ。デバッガを使用します。デバッガを使用すると、個々のステートメントを個別に実行し、変数の値を監視することができます。デバッガを使用すると、StackOverflowに投稿して誰かがあなたのためにデバッガを使用するのを待っているよりもずっと**高速です**。 –
'main'の返り値は' void'ではなく 'int'でなければなりません。私は関数を呼び出す方法を読むことをお勧めします。関数ポインタを表示するだけです。 –