Reputation:
int smallest(int x, int y, int z)
{
int smallest = x;
if (y < smallest)
smallest = y;
if (z < smallest)
smallest = z;
return smallest;
}
void printmessage(int smallest)
{
int w = 0;
if(smallest < 0 || smallest >= 10)
cout << "it is not possible to print the message in this case" << endl;
else
{
for(w < 0 ||w > 10; w < smallest; w++)
cout << "No" << endl;
cout << endl << endl;
}
}
This just basically reads in 3 values and finds the smallest. The message "No" is printed out based on the smallest value. If it's 5, "no" will be printed out 5 times. Not sure how to fix the part where I declare the smallest in printmessage
. Can somebody help me fix it?
Upvotes: 0
Views: 219
Reputation: 25928
Based on your comment, you just need to fix the first part of your for
loop:
void printmessage(int smallnum) {
if(smallnum < 0 || smallnum >= 10) {
cout << "it is not possible to print the message in this case" << endl;
} else {
for(int w = 0; w < smallnum; w++) {
cout << "No" << endl;
}
}
}
Note the change in the parameter name - right now, you already have a function called smallest()
, so you shouldn't create a variable with this name, too, or you're going to get weird things happening. Same thing for your smallest()
function itself - change the first line, int smallest = x;
to int smallnum = x
or similar, and change the references to it in the rest of your function.
And, as namfuak points out in the comments, change printmessage(smallest);
to printmessage(smallest(x, y, z));
, otherwise you're trying to pass the address of the function, instead of its result.
In a full working example:
#include <iostream>
void printmessage(int smallnum) {
if(smallnum < 0 || smallnum >= 10) {
std::cout << "it is not possible to print the message in this case"
<< std::endl;
} else {
for(int w = 0; w < smallnum; ++w) {
std::cout << "No" << std::endl;
}
}
}
int main(void) {
printmessage(5);
return 0;
}
Outputs:
paul@local:~/src/cpp/scratch$ ./smallest
No
No
No
No
No
paul@local:~/src/cpp/scratch$
Upvotes: 0
Reputation: 3224
for(w < 0 ||w > 10; w < smallest; w++)
The first section of this is effectively meaningless, while the statement technically is executed the first statement in a for loop is colloquially for initializing and/or assigning loop control variables. In your case, you probably want to use this:
for(int w = 0; w < smallest; w++)
This will initialize the a variable "w" within the scope of the loop, rather than the scope of the function (so after the loop exits, "w" will be discarded). Since you only need it within the loop, this should be what you are looking for.
If you want to check that w is also not less than 0 or greater than ten, you can use the following loop:
for(int w = 0; w < 0 || w > 10, w < smallest; w++)
You can also leave the initial w in the function scope if you need it later (or in other cases where you don't need a loop control variable) and have a blank statement in that space in the loop declaration instead:
for(; w < smallest; w++)
Upvotes: 0