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.
I need to implement the function here
and probably add a counter inside but I dont know how.
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
cin >>th1>>th2>>th3>>th4;
Looks like this is correct.
if 2 of those are -100
The answer is in your question itself. Focus on the word 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 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. This function will take an int
as parameter and check if that parameter is equal to 100 or not. If it is equal to 100, we will return
a value: 1
to the caller. Otherwise we will return a value: 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:
=
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:
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. 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). If not, we simply return a 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. 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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.