gpt4 book ai didi

c++ - 错误:尝试使用模数时出现 “expression must have integral or unscoped enum type”

转载 作者:行者123 更新时间:2023-12-02 10:23:31 25 4
gpt4 key购买 nike

我使用VS Code for C++编译器,而我试图找到模数来计算用户需要多少加仑油漆,因此我为其创建了一个变量,但随后它说“表达式必须具有整数或无作用域的枚举类型”,怎么解决呢?

考虑以下:

-Figure out the size of our room. -Determine the amount of paintable space for a room using variables. -Add variables for unpaintable spaces such as doors and windows. Assume all --other wall space is paintable. -One gallon of paint will cover 400 sq. ft. Write an application that will print the number of square feet that needs to be covered and the number of gallons of paint that you will need. Create a reusable code by using variables for storing values.



如果我将模数切换为除法,那么误差将消失,但这不是我想要的。
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
//creating variables
double space;
int numberOfWindows;
int paint = 400;
int gallonOfPaint;
double sizeOfWindow;
double squarefeet = sizeOfWindow * numberOfWindows - space;
int modulus = squarefeet % paint;

//getting data
cout << "Welcome, this is a calulator to calculate how much paint you need." << endl;
cout << "Enter the square feet of your room: ";
cin >> space;
cout << "Enter how many windows you have: ";
cin >> numberOfWindows;

//Determine how big the windows is
if(numberOfWindows > 0){
cout << "How big is your windows? ";
cin >> sizeOfWindow;
//Determine how much paint the user need
if(modulus > 0.1){
gallonOfPaint++;
cout << "You need " << gallonOfPaint << " gallon(s) of paint." << endl;
} else if(modulus < 0.1){
int answer = space / paint;
cout << "You need " << answer << "gallon(s) of paint." << endl;
}
} else if(numberOfWindows = 0){
if(modulus > 0.1){
gallonOfPaint++;
cout << "You need " << gallonOfPaint << " gallon(s) of paint." << endl;
} else if(modulus < 0.1){
int answer = squarefeet / paint;
cout << "You need " << answer << "gallon(s) of paint." << endl;
}
}

system("pause");
return 0;
}

错误消息:表达式必须具有整数或无作用域的枚举类型

最佳答案

方呎是两倍。有时会发生这种情况-您想使用double,但您知道这是可能的,但是c++不允许这样做。
但是几乎总是有其他方法:

1)将小数部分存储在double decPart = squarefeet - (int)squarefeet;

2)在整数int intPart = (int)squarefeet % paint;上使用模数

3)计算double modulus = decPart + intPart;
或使用整数类型作为其他答案建议。

关于c++ - 错误:尝试使用模数时出现 “expression must have integral or unscoped enum type”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58109760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com