简体   繁体   English

我如何编写带有计数器的 function?

[英]How do i write a function with a counter inside?

First week coding please be kind.第一周编码请善待。
I need to get 4 inputs from the user and if 2 of those are -100 I need function to say you fail.我需要从用户那里得到 4 个输入,如果其中 2 个是-100 ,我需要 function 说你失败了。

I need to implement the function here and probably add a counter inside but I dont know how.我需要在function here并且可能在里面添加一个计数器,但我不知道如何。

Code looks something like this代码看起来像这样

cout << "What are the grades of your 4 take-home exams? ";
    cin >>th1>>th2>>th3>>th4;
function here

I need to get 4 inputs from the user我需要从用户那里得到 4 个输入

cin >>th1>>th2>>th3>>th4;

Looks like this is correct.看起来这是正确的。

if 2 of those are -100如果其中 2 个是 -100

1. Conditionals 1. 条件

The answer is in your question itself.答案就在你的问题本身。 Focus on the word if .专注于if一词。

Programming languages have all sorts of different ways to check things, to check if something is equal to something else, or if something is less than something else, run a certain piece of code.编程语言有各种不同的方法来检查事物,检查某事物是否等于其他事物,或者如果某事物小于其他事物,则运行一段代码。 We call these conditions .我们称这些条件 If a certain condition is true , do something, else , do something different.如果某个条件为,做某事,否则,做一些不同的事情。 Example:例子:

int x = 0;
cin >> x; // take input from user
if (x > 20)
{
   cout << "x is greater than 20";
}
else 
{
   cout << "x is less than 20";
}

Moreover, You can even check two variables at the same time!!此外,您甚至可以同时检查两个变量!

int x = 1;
int y = 2;
if (x == 1 && y == 2)
   cout << "X is 1, and y is 2";
else
   //print something else

Functions功能

Functions are a way to put some code in a block, that you can reuse whenever you want.函数是一种将一些代码放入块中的方法,您可以随时重复使用。 Think of this like, suppose you are making a cake and you need a certain syrup for it.想象一下,假设您正在制作蛋糕,并且需要某种糖浆。 Now if you want, you can make that syrup every time from zero, or you can just make a lot of syrup, and use it every time when you want to make a cake.现在,如果您愿意,您可以每次从零开始制作糖浆,或者您可以制作大量糖浆,并且每次您想制作蛋糕时都使用它。 It's definitely a bad example, but you get the point.这绝对是一个不好的例子,但你明白了。

Lets make a small function.让我们做一个小的 function。 This function will take an int as parameter and check if that parameter is equal to 100 or not.此 function 将采用int作为参数并检查该参数是否等于 100。 If it is equal to 100, we will return a value: 1 to the caller.如果它等于 100,我们将return一个值: 1给调用者。 Otherwise we will return a value: 0 :否则我们将返回一个值: 0

int checkIfHundredOrNot(int value)
{
    if (value == 100)
       return 1;
    else
       return 0;
}

//caller
int main()
{
    int x = 0;
    cin >> x;
    int result = checkIfHundredOrNot(int value);
    cout << "Result is: " << result;
    return 0;
}

With this information, try and solve your problem.使用此信息,尝试解决您的问题。 It should be fairly straightforward.它应该相当简单。 If you find anything difficult, google it.如果您发现任何困难,请谷歌它。 You will find all your answers there.你会在那里找到你所有的答案。 Specifically google this:具体谷歌这个:

  • What is a function parameter in C++? C++ 中的 function 参数是什么?
  • What is return value in C++? C++ 的返回值是多少?
  • What is a function in C++? C++ 中的 function 是什么?
  • Why do we use functions in C++?为什么我们使用 C++ 中的函数?
  • What are operators in C++? C++ 中的运算符是什么?
  • What are comparison operators in C++? C++ 中的比较运算符是什么?
  • Is there a difference between = and == ? ===之间有区别吗?

Then read them carefully and write down every piece of code with your own hands and repeat till you understand it.然后仔细阅读它们,并用自己的双手写下每一段代码,然后重复直到你理解为止。

So I m kind of a noob myself but I ll give it a shot.所以我自己是个菜鸟,但我会试一试。 I would recommand taking a look at my solution and try doing yours in a "you" way since there are manny more paths to achieving the right answer.我建议看看我的解决方案,并尝试以“你”的方式做你的,因为还有更多的途径可以获得正确的答案。 If you are really specific about only having 4 inputs you can try this:如果你真的很明确只有 4 个输入,你可以试试这个:

bool isFail(int a, int b, int c, int d) {

int counter = 0;
if (a == -100) {
    counter++;
}
if (b == -100) {
    counter++;
}
if (c == -100) {
    counter++;
}
if (d == -100) {
    counter++;
}

if (counter >= 2) {
    return true;
}
else
{
    return false;
}

} }

The function basically takes 4 ints and checks each one of them against -100. function 基本上需要 4 个整数,并根据 -100 检查每个整数。 If they turn out to be -100 the counter increases by 1. Afterwards we simply check if the counter is equal to or bigger than two and if so we return a true value since it is failed (tow or more inputs are -100).如果结果是 -100,则计数器增加 1。之后,我们只需检查计数器是否等于或大于 2,如果是,则返回真值,因为它失败了(两个或更多输入为 -100)。 If not, we simply return a false.如果不是,我们只返回一个 false。

In your main function you can simply initialize your variables, input them and simply do a function call that you can compare against another "true" value.在您的主要 function 中,您可以简单地初始化变量,输入它们并简单地执行 function 调用,您可以将其与另一个“真实”值进行比较。 If the result is positive you print out a fail message.如果结果是肯定的,则打印出失败消息。

int a, b, c, d;
std::cin >> a >> b >> c >> d;

if (isFail(a, b, c, d) == true) {
    std::cout << "Fail!" << std::endl;;
}
else
{
    std::cout << "Passed!" << std::endl;
}

Best regards!此致!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM