gpt4 book ai didi

c++ - 无法计算字符类型的数量

转载 作者:行者123 更新时间:2023-11-28 02:03:02 26 4
gpt4 key购买 nike

我正在尝试为我的作业做一个特定的计数器,但我尝试了很多方法,但我仍然无法弄清楚。

我的程序允许用户根据他们的输入找出三角形的类型。

所以我有两个主要函数来确定三角形的形状和显示计数器。

  1. char shapeTriangle(int, int, int); //determine shape of triangle i.e isosceles ('I'), equilateral ('E')

  2. int summaryDisplay(char); // display counter

在我的 shapeTriangle 函数中,我有一个简单的 if else 语句,它根据用户输入返回类型

char shapeTriangle(int x, int y, int z)
{

char type;

if (x == y && y == z && x == z)

type = 'E';

else if (x == y || y == z || x == z)

type = 'I';

return type;
}

在我的 main 函数 中,我有一个循环,允许用户输入直到其中一个值为 0。

summaryDisplay 中,我试图计算确定某个三角形的次数

int finalSum(char type)

int eCount = 0, iCount = 0;

if (type == 'E')

eCount++;

if (type == 'I')

iCount++;

cout << "Equilateral" << eCount;
cout << "Isosceles" << iCount;

}

我设法获得了一个输出,但是,计数器返回了一些奇怪的值,例如 5409343453 等等,我无法真正弄清楚它们。

这就是我尝试在 int main 中调用我的函数的方式

int main()

{

int x, y, z;

do{

cout << "Enter 3 intgers : ";
cin >> x >> y >> z;

//some display output codes

}while ( x != 0 && y != 0 && z != 0);

finalSum(shapeTriangle(x, y, z));

}

感谢任何帮助。

编辑 1: 我尝试了初始化,但是对于所有不同的类型,它都返回 0。

最佳答案

只需用零初始化您的计数器,这将作为您的计数器起始值。像这样:

int eCount = 0, iCount = 0;

此外,如果您的条件都不满足,您希望 type 是什么?因为如果你没有初始化 type 并且你的条件都不满足 (if (x == y && y == z && x == z)if (x == y || y == z || x == z)) 然后你的两个计数器都将保持为 0。

关于c++ - 无法计算字符类型的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38641251/

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