[英]the call to “conclusionaryOutput(number1, number2, isASquare1, isASquare2)” is throwing an error and I'm not sure why
The purpose of this program is to accept two numbers from the user, and figure out the two inputted numbers' factors and if they have any factors that are squares.该程序的目的是从用户那里接受两个数字,并计算出两个输入数字的因数,以及它们是否有任何平方数的因数。
When I try to run it, it throws an error on the function call of conclusionaryOutput()
:当我尝试运行它时,它会在 function 调用的
conclusionaryOutput()
中引发错误:
[cquery] address of function 'listSquares' will always evaluate to 'true' [-Wpointer-bool-conversion]
[cquery] function 'listSquares' 的地址将始终评估为'true' [-Wpointer-bool-conversion]
and:和:
use of undeclared identifier 'isASquare1';
使用未声明的标识符“isASquare1”; did you mean 'listSquares'?
你是说'listSquares'吗?
#include <iostream>
using namespace std;
void conclusionaryOutput(int number1, int number2, bool isASquare1, bool isASquare2){
if(isASquare1 == false && isASquare2 == false){
cout << "Therefore the ordered pair (" << number1 << "," << number2 << ") is SWEET." << endl;
}
else if(isASquare1 == false && isASquare2 == false){
cout << "Therefore the ordered pair (" << number1 << "," << number2 << ") is SOUR." << endl;
}
else if(isASquare1 == true && isASquare2 == false){
cout << "Therefore the ordered pair (" << number1 << "," << number2 << ") is SALTY." << endl;
}
else{
cout << "Therefore the ordered pair (" << number1 << "," << number2 << ") is BITTER." << endl;
}
}
void listSquares(int divisor, int numOfSquares){
if (numOfSquares !=1){
cout << ", " << divisor;
}
else{
cout << divisor;
}
}
void squareInquiry(int number1, int number2){
bool containSquareFactor1;
bool containSquareFactor2;
int counter;
int divisor;
int numOfSquares;
counter = 2;
while (counter <= 70){
divisor = counter * counter;
if (number1 % divisor == 0){
numOfSquares++;
listSquares(divisor, numOfSquares);
}
counter++;
}
if (numOfSquares > 0){
cout << number1 << " has these factors (>1) that are square: " << numOfSquares << endl;
cout << number1 << "is not square-free" << endl;
containSquareFactor1 = true;
}
else{
cout << number1 << " has these factors (>1) that are square: (none) " << endl;
cout << number1 << "is square-free" << endl;
containSquareFactor1= false;
}
counter =2;
numOfSquares =0;
while(counter <=70){
divisor = counter * counter;
if (number2 % divisor == 0){
numOfSquares++;
listSquares(divisor, numOfSquares);
}
counter++;
}
if(numOfSquares > 0){
cout << number2 << " has these factors (>1) that are square: " << numOfSquares << endl;
cout << number2 << "is not square-free" << endl;
containSquareFactor2 = true;
}
else{
cout << number2 << " has these factors (>1) that are square: (none) " << endl;
cout << number2 << "is square-free" << endl;
containSquareFactor2= false;
}
conclusionaryOutput(number1, number2, isASquare1, isASquare2);
}
int main(){
int firstInt;
int secondInt;
cout << "Enter the 1st integer of the pair, between 2 and 5000: ";
cin >> firstInt;
while(firstInt < 2 || firstInt > 5000){
cout << "Invalid entry, Please try again: ";
cin >> firstInt;
}
cout << "Enter the 2nd integer of the pair, between 2 and 5000: ";
cin >> secondInt;
while(secondInt <2 || secondInt > 5000){
cout << "Invalid entry, Please try again: ";
cin >> secondInt;
}
squareInquiry(firstInt, secondInt);
return 0;
}
When you call your conclusionaryOutput
function (at the end of squareInquiry
) you must give it the names of actual variables in the calling module rather than use the names of the parameters given in the function's definition.当您调用您的
conclusionaryOutput
输出 function (在squareInquiry
)时,您必须为其提供调用模块中实际变量的名称,而不是使用函数定义中给出的参数名称。 (This may be a basic misunderstanding of function calling in C++ because, everywhere else in your code, you appear to use the same names for local variables and formal parameters.) (这可能是对 function 在 C++ 中调用的基本误解,因为在代码中的其他任何地方,您似乎对局部变量和形式参数使用相同的名称。)
So, rather than:所以,而不是:
conclusionaryOutput(number1, number2, isASquare1, isASquare2);
you should use:你应该使用:
conclusionaryOutput(number1, number2, containSquareFactor1, containSquareFactor2);
With this change, the program will compile (but with a warning, if you're lucky) … but it (probably) won't work.通过此更改,程序将编译(但如果幸运的话,会出现警告)......但它(可能)不会工作。 What you have forgotten to do (also in the
squareInquiry
function) is to give the numOfSquares
variable an initial value (the compiler is not forced to do this for you).您忘记做的事情(也在
squareInquiry
函数中)是给numOfSquares
变量一个初始值(编译器不会强制为您执行此操作)。
So, near the start of that function, where you declare that variable, give it an initial (zero) value:因此,在声明该变量的 function 的开头附近,给它一个初始(零)值:
void squareInquiry(int number1, int number2)
{
bool containSquareFactor1;
bool containSquareFactor2;
int counter;
int divisor;
int numOfSquares = 0;// Don't use this (below) without initializing it (to zero)
counter = 2;
//...
Please feek free to ask for any further clarification and/or explanation.请随时要求任何进一步的澄清和/或解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.