将bool值返回到main函数

时间:2014-04-16 20:57:39

标签: c++ function boolean return

好的,所以我的问题是我正在丢弃1000千米的0-999杆。使用rand函数模拟2个随机断点。拿这两个断点来计算三个碎片。 然后检查这三个碎片,看看它们是否形成一个三角形,并将bool值true或false返回到main函数,具体取决于它们是否形成了三角形。

我遇到的问题是返回bool函数并将其显示在我的main函数中。

任何帮助ID都要提前感谢你。

#include <iostream>
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h>
using namespace std;

bool triangleCheck(int & side1, int & side2, int & side3);
void generateRandomNumbers ( int & breakLocation, int & breakLocation2);
void calculateBrokenSides (int & breakLocation, int & breakLocation2, int & side1, int & side2, int & side3);




void main ()
{


srand(time(NULL));
int breakLocation, breakLocation2, side1, side2, side3;


generateRandomNumbers (breakLocation, breakLocation2);
cout << breakLocation << endl;
cout << " " << endl;
cout << breakLocation2 << endl;


calculateBrokenSides ( breakLocation, breakLocation2, side1, side2, side3);
cout << side1 <<" "<<side2 <<" "<<side3<< endl;

triangleCheck (side1, side2, side3);
}




void generateRandomNumbers(int & breakLocation, int & breakLocation2)
{

//make 2 random break locations
do  {
breakLocation = rand() % 999 +1 ;
breakLocation2 = rand() % 999 +1 ;
} while(breakLocation == breakLocation2 );

}


// this function takes the 2 random numbers symbolizing break points and calculates 3 lengths of broken glass then returns them using pass by reference
void calculateBrokenSides (int & breakLocation, int & breakLocation2, int & side1, int & side2, int & side3)
{

if (breakLocation < breakLocation2)
(side1 = breakLocation +1) && (side2 = breakLocation2 - (breakLocation +1)) && (side3 = 1000 - (side1 + side2));    
else 
(side1 = breakLocation2 +1) && (side2 = breakLocation - (breakLocation2 +1)) && (side3 = 1000 - (side1 + side2));  

}   




//This function tests the three lengths of glass created in generateRandomNumbers and CalculateBrokenSides to see if they form a triangle
bool triangleCheck(int & side1, int & side2, int & side3)
{
if (side1 + side2 > side3 || side2 + side3 > side1 || side1 + side3 > side2)
return true;
else
return false;}

1 个答案:

答案 0 :(得分:1)

这应该足够了。

if(triangleCheck (side1, side2, side3))
    cout << "YAY True" << endl;
else
    cout << "WRONG..Sorry" << endl;