C++ 在C+中使用指针进行分支和循环时存在输出问题+;?
这是一个家庭作业,当输入值可被3、5、3&5整除时,需要使用指针输出消息。代码编译并运行,但我没有得到所需的输出。此外,我不确定什么需要改变。这是我的密码:C++ 在C+中使用指针进行分支和循环时存在输出问题+;?,c++,pointers,if-statement,for-loop,C++,Pointers,If Statement,For Loop,这是一个家庭作业,当输入值可被3、5、3&5整除时,需要使用指针输出消息。代码编译并运行,但我没有得到所需的输出。此外,我不确定什么需要改变。这是我的密码: #include <iostream> #include <iomanip> using namespace std; using std::istream; int main() { const int i{3}; // Variable initializat
#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;
}
#包括
#包括
使用名称空间std;
使用std::istream;
int main()
{
const int i{3};//变量初始化和声明
long*pnumber{};//指针定义和初始化
long number1{};//变量number1的声明和初始化
long number2[i];//包含3L数字数组的变量的声明和初始化
字符指示符{'n'};//是否继续?
pnumber=&number1;//在指针中存储地址
对于(;;)
{
cout数1;
if(*pnumber%3==0)//测试被3除后的余数是否为0
{
从你自己的例子中我可以看出,当输入可以被3
整除时,你想打印Fizz
,当输入可以被5
整除时,打印Buzz
,当输入可以被3
和5
整除时,打印FizzBuzz
。现在,如果我的假设是正确的话,请韩:你在这里犯了一个合乎逻辑的错误。我将用一个例子说明这一点
假设输入的数字是30
。你的程序会发现它可以被3
整除,所以它会打印Fizz
,然后同样地,它会发现它可以被5
整除,所以它也会打印Buzz
。最后,出于同样的原因,它也会打印FizzBuzz
仅限intFizzBuzz
这就是你应该如何解决这个问题
// First check if it is divisible by both 3 and 5 ?
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
}
// if not, then is it divisible by 3 only ?
else 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 not, then is it divisible by 5 only?
else 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
}
// everything else ...
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
}
//首先检查它是否可以被3和5整除?
if((*pnumber%3==0)和&(*pnumber%5==0))//测试除以5和3后的余数是否为0
{
那么,你到底遇到了什么错误呢?看看这篇文章,关于在请求家庭作业帮助时的一些建议:问题是代码第二部分中的数字2在输入几个数字时输出。它输出数字:008FFA88 Fizz,而不是如果我输入6,它应该输出数字:6-Fizz。为什么使用std::istream
与一起使用命名空间std
?现在的问题是在输入多个数字时在代码的第二部分输出数字2。它输出数字:008FFA88 Fizz,而不是如果我输入6,它应该输出数字:6-Fizz。这是因为您没有正确地将整数数组分配给pnumber
指针。Lo好的,为了更好地理解使用指针处理数组,我尝试在没有[i]的情况下使用&number2但是它抛出了一个错误。正如您所声明的那样,number2
是一个数组。因此number2本身将是指向数组中第一个元素地址的指针。因此,您应该使用pnumber=number2
而不是pnumber=&number2
。当代码作为pnumber=number2写入时,它会导致逻辑错误,并且不会运行corr显然,程序不会输出输入的数字