使用功能 [英] using functions
问题描述
好了,现在我开始工作这部分了,如何使用已定义的函数使它工作呢?以及我应该使用哪些项目使其成为我在较早的帖子中尝试过的myfuncion,并且您可以看到它不起作用,所以我回到了基础知识上,现在需要帮助弄清楚如何实现和创建自己的函数来做这个计算.感谢您的帮助
Ok now I got this part working how should I make this to work using a defined function? and what items should I use to make it a myfuncion I haves tried by the earlier posts and as you can see it is not working so I went back to the basic and now need help figuring out how to implement and create my own function to do this calculation. thanks for the help
#include <iostream>
#include <string>
const int SQUARE = 1;
const int TRIANGLE = 2;
const int RECTANGLE = 3;
const int CIRCLE = 4;
using namespace std;
int main()
{
// Declare Variable
int choice;
int width;
int height;
double base;
int area;
int distance;
cout << "What is the area of a square, triangle, rectangle, or circle?" ;
// Display Choice
cout << SQUARE << " = Square: " << endl;
cout << TRIANGLE << " = Triangle: " << endl;
cout << RECTANGLE << " = Rectangle: " << endl;
cout << CIRCLE << " = Circle: " << endl;
cout << "Make your choice? ";
cin >> choice ;
// Display Proper feedback based on users choice
switch (choice)
{
case SQUARE:
// Get Square information
cout << "Enter the width of the square: " << endl;
cin >> width;
cin >> width;
area = width * width;
cout << "The area of the square is: " << area << endl;
break;
case TRIANGLE:
// Get Triangle information
cout << "Enter the base: " << endl ;
cin >> base;
cout << "Enter the height: " << endl ;
cin >> height;
area = (base * 2) * height;
cout << "The area of a triangle is " << area << endl;
break;
case RECTANGLE:
//Get the Rectangle inforamtion
cout <<"Enter the height: " << endl;
cin >> height;
cout << "Enter the width: " << endl;
cin >> width;
area = height * width;
cout << "The area of a rectancle is: " << area << endl;
break;
case CIRCLE:
//Get the circle circumference
cout << "Enter the distance: " << endl;
cin >> distance;
area = distance * 3.14;
cout << "The circumference of a circle is: " << area << endl;
break;
}
system("pause");
return 0;
}
推荐答案
您需要更好地了解如何使用if
和else
.请看下面的代码片段:
You need to better understand how to useif
andelse
. Look at the code snippet below:
if (mychoice == square)
{
// Work with square
}
else if (mychoice == rectangle)
{
// Work with rectangle
}
else
{
// Not a square nor a rectangle...
}
您有两次else (mychoice == XXX)
,确定编译器可以理解该构造吗?
You have twiceelse (mychoice == XXX)
, are you sure your compiler can understand that construct?
else (mychoice == rectangle){ // AT THIS POINT THERE IS AN ERROR THAT SAYS EXPECTED ; WHY IS THAT?
如果我是C ++编译器,我会期望else; (mychoice == rectangle)
.你知道为什么吗?
欢呼声,
AR
OP:不,我不能解释这个问题
1.阅读文章控制结构
[ ^ ]从顶部到 迭代结构(循环) .
2.再次阅读:)寻找句子:如果(条件)statement1 else statement2 .
3.在您的代码中找到:
If I was a C++ compiler I would expect else; (mychoice == rectangle)
. Do you understand why?
cheers,
AR
OP: no I do not can you explain this
1. Read the article Control Structures
[^] from top to Iteration structures (loops).
2. Read it again :) looking for the sentence : if (condition) statement1 else statement2.
3. In your code find:
if (mychoice == square){
cout << "Enter the width: ";
cin >> width;
cout << width * 2;
}
else (mychoice == rectangle)
4.从此代码中,在 if(条件)statement1 else statement2 中确定与 condition , statement1 和 statement2 匹配的部分. >.
5. (mychoice == rectangle)
是声明吗?
6.编译器在else
之后找到丢失的语句,并建议;
最简单,最短的语句.
您应该在else (mychoice == rectangle)
中插入什么以使编译器满意?
4. From this code identify the parts matching condition, statement1, and statement2 in if (condition) statement1 else statement2.
5. Is (mychoice == rectangle)
a statement?
6. The compiler finds a missing statement after else
and suggests ;
the simplest and shortest possible statement.
What should you insert in else (mychoice == rectangle)
to make your compiler happy?
这篇关于使用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!