これは、入力値が3、5、および3 &で割り切れる場合にメッセージを出力するためのポインタの使用を必要とする宿題です。コードはコンパイルされて実行されますが、目的の出力が得られません。また、私は何が変わる必要があるか分からない。入力が5
と印刷FizzBuzz
その割り切れるによって完全に割り切れるとき、あなた自身からC++での分岐とループのためのポインタを利用した出力の問題はありますか?
#include <iostream>
#include <iomanip>
using namespace std;
using std::istream;
int main()
{
const int i{3}; // Variable initialization and declaration
long* pnumber{}; // Pointer definition and initialization
long number1 {}; // Declaration and initialization of variable number1
long number2 [i]; // Declaration and initialization of variable containing array of 3L numbers
char indicator{ 'n' }; // Continue or not?
pnumber = &number1; // Store address in a pointer
for (;;)
{
cout << "Please enter any number less than 2 billion, "
<< "then press the Enter key: ";
cin >> number1;
if (*pnumber % 3 == 0) // Test if remainder after dividing by 3 is 0
{
cout << endl
<< "Number: " << number1
<< "-Fizz" << endl
<< endl << "Do you want to enter another value (please enter y or n? ";
cin >> indicator; // Read indicator
if (('n' == indicator) || ('N' == indicator))
break; // Exit from loop
}
if (*pnumber % 5 == 0) // Test if remainder after dividing by 5 is 0
{
cout << endl
<< "Number: " << number1
<< "-Buzz" << endl
<< endl << "Do you want to enter another value (please enter y or n? ";
cin >> indicator; // Read indicator
if (('n' == indicator) || ('N' == indicator))
break; // Exit from loop
}
if ((*pnumber % 3 == 0) && (*pnumber % 5 == 0)) // Test if remainder after dividing by 5 and 3 is 0
{
cout << endl
<< "Number: " << number1
<< "-FizzBuzz" << endl
<< endl << "Do you want to enter another value (please enter y or n? ";
cin >> indicator; // Read indicator
if (('n' == indicator) || ('N' == indicator))
break; // Exit from loop
}
else
{
cout << endl // Default: here if not divisible by 3 or 5
<< "Please enter another number."
<< endl << "Do you want to enter another value (please enter y or n? ";
cin >> indicator; // Read indicator
if (('n' == indicator) || ('N' == indicator))
break; // Exit from loop
}
}
pnumber = &number2 [i]; // Change pointer to address of number2
for (;;)
{
cout << endl << endl
<< "Please enter an array of number(s), where the number is less than 2 billion, "
<< "then press the Enter key: ";
cin >> number2 [i];
if (*pnumber % 3 == 0) // Test if remainder after dividing by 3 is 0
{
cout << endl
<< "Number: " << number2
<< "-Fizz" << endl
<< endl << "Do you want to enter another array (please enter y or n? ";
cin >> indicator; // Read indicator
if (('n' == indicator) || ('N' == indicator))
break; // Exit from loop
}
if (*pnumber % 5 == 0) // Test if remainder after dividing by 5 is 0
{
cout << endl
<< "Number: " << number2
<< "-Buzz" << endl
<< endl << "Do you want to enter another array (please enter y or n? ";
cin >> indicator; // Read indicator
if (('n' == indicator) || ('N' == indicator))
break; // Exit from loop
}
if ((*pnumber % 3 == 0) && (*pnumber % 5 == 0)) // Test if remainder after dividing by 5 and 3 is 0
{
cout << endl
<< "Number: " << number2
<< "-FizzBuzz" << endl
<< endl << "Do you want to enter another value (please enter y or n? ";
cin >> indicator; // Read indicator
if (('n' == indicator) || ('N' == indicator))
break; // Exit from loop
}
else
{
cout << endl // Default: here if not divisible by 3 or 5
<< "Please enter another number."
<< endl << "Do you want to enter another value (please enter y or n? ";
cin >> indicator; // Read indicator
if (('n' == indicator) || ('N' == indicator))
break; // Exit from loop
}
}
cout << endl;
return 0;
}
だから、あなたはどうしているのですか?この投稿を見て、宿題に助けを求めるときにいくつかの提案をしてください:https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – sas
問題はいくつかの数字を入力しながら、コードの2番目の部分にnumber2を出力します。入力した番号の代わりにNumber:008FFA88-Fizzを出力します.6を入力するとNumber:6-Fizzが出力されます。 – phoenixCoder
なぜ 'std :: istream'を' using namespace std'と使うのですか? – Raindrop7