I'm very new to this, and i'm trying to create a text based minesweeper.
I want the player to decide how big he want the grid to be.
My problem is, that the if-statement, that should make sure, the user types in a number from 1 to 10 doesn't work. Please have a look.
scanf ("%i/%i",&x,&y);
if (0 < x < 11 && 0 < y < 11)
{
printf ("you have selected %i by %i\n",x,y);
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
grid[x][y] = 'O';
printf ("%c ", grid[x][y]);
}
printf ("\n");
}
}
else
printf ("Wrong gridsize");
C does not support double comparisons like:
0 < x < 11
you should write instead
0 < x && x < 11.
It may be misleading, because the first statement is syntaxically correct (it compiles), but it does not do what you may believe: check both boundaries like in a mathematical expression (what python does for instance).
It's like if you had written
(0 < x) < 11
The first binary expression returns a boolean (well, really an int in C, a boolean in C++). This boolean once casted to int is 0 or 1, always below 11, henceforth the expression is always true.
Of course the same is true for checking y boundaries. Now you should be able to fix the problem by yourself.
The if statement has to be like
if ((x > 0 && x < 11) && (y > 0 && y < 11))
you have written wrong if statement.
Here is correct form.
if ((x > 0 && x < 11) && (y > 0 && y < 11))
Here are the relational operators
> greater than 5 > 4 is TRUE
< less than 4 < 5 is TRUE
>= greater than or equal 4 >= 4 is TRUE
<= less than or equal 3 <= 4 is TRUE
== equal to 5 == 5 is TRUE
!= not equal to 5 != 4 is TRUE
C does not support double comparisons.
In your scanf statement remove / operator change it to
scanf ("%i%i",&x,&y);
And what your if statement is doing is what you expect to do
if (0 < x < 11 && 0 < y < 11)
first when you enter the value x and y (x = 4 and y = 6)
It checks if x is greater than 0 (which is true ) so 1 is substituted in place of 0 < x
now its something like this for compiler 1 < 11
next it checks that which is also true
similarly for y whichever value you enter will always be true.
0 < x < 11
means
(0 < x)<11. If x is 5, 0 < x will be 1 (true). Next evaluation will be 1 < 11, that will be true so the result is true.
But, if x = 20, 0<20 is 1, 1<11 is true as well, but you would expect a false result.
Related
When I am trying to get the input for my variable, it is only meeting one of the requirements (ie: the < 1 requirement) and skips the other requirement even though im using the && operator.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int x;
do {
x = get_int("what is the height of the pyramid?:");
} while (x > 0 && x < 8);
printf("%i", x);
}
I tried just using the x < 8 for the requirement but it still went through when I entered 9, 10, 11 etc.
If you want x to be between 0 and 8 (both ends exclusive), then you need to repeatedly ask for input when this condition is not satisfied.
In other words, when x is outside this range it means x is less than or equal to 0 OR greater than or equal to 8.
That said, I believe the proper input range for that problem set is actually 1-8 (both ends inclusive):
do {
x = get_int("What is the height of the pyramid?: ")
} while (x < 1 || x > 8);
The test is exactly the opposite of your intent. The do/while condition should test a condition for repeating the input and write while (!(x > 0 && x < 8)); or equivalently: while (x < 1 || x >= 8);
It is unclear what your requirements are, but it seems the number should be between 1 and 7 inclusively. If 8 should be included, the test should be modified as while (!(x > 0 && x <= 8)); or equivalently: while (x < 1 || x > 8);
do/while loops are often confusing and error prone. I suggest using a for(;;) aka for ever loop and break statements when conditions are met:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int x;
for (;;) {
x = get_int("What is the height of the pyramid? ");
if (x == INT_MAX) {
printf("Input error or end of file\n");
return 1;
}
if (x > 0 && x < 8) {
break
}
printf("The height should be between 1 and 7\n");
}
printf("%i\n", x);
return 0;
}
This question already has answers here:
Why is a condition like (0 < a < 5) always true?
(4 answers)
Closed 9 years ago.
I have this question & in the answer it says that due to left to right associativity the result would be 1 that is true for this statement. This is the code.
#include<stdio.h>
int main ()
{
int i=0,x=10,y=10,z=5;
i=x<y<z;
printf("\n\n%d",i);
return 0;
}
But x is greater than z here so how is this happening ?
The expression x
(x < y) < z
so it becomes
(10 < 10) < 5
which further is evaluated into
0 < 5
which is true.
I think you wanted something like this:
x < y && y < z
Because of operator precedence and associativity
i = x < y < z;
is parsed as:
i = ((x < y) < z);
After substituting the variable values, this becomes:
i = ((10 < 10) < 5);
Since 10 < 10 is false, this becomes:
i = (0 < 5);
Since 0 < 5 is true, that becomes:
i = 1;
x<y<z is not a single valid expression. Instead it evaluates x<y first (operator precedence is done left to right here) as true/false (false in this case as they're equal), converts it to an int value of 0, and then compares this value with z.
Use (x < y && y < z) instead.
It first evaluates x < y which is false (0), then 0 < z which is true (1).
WHat C compiler does is, in x<y<z;
starts from left, so as x is not less than y therefore it replaces that expression with '0'
so it becomes 0<z and as that is true. it set the variable to 1.
This question already has answers here:
Chaining multiple greater than/less than operators
(6 answers)
Closed 6 years ago.
im having trouble using multiple operators while programing a simple FizzBuz in C
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
if (0 < n < 10000){
for (int i = 1; i <= n; i ++) {
if (i % 15 == 0) {
puts("TikTak");
}
else if (i % 3 == 0) {
puts("Tik");
}
else if (i % 5 == 0) {
puts("Tak");
}
else {
printf("%d\n", i);
}
}
}else{
printf("-1");
}
}
Now the "if (0 < n < 10000)" comparison operators is ignored for some reason, but if I rewrite it as:
if (n < 10000 && n > 0){
it will work as intended.
Am I missing something? Ill be honest Im newbie in C programing. haha
EDIT: Thanks everybody, haha that was quite simple. I thought that might be the issue I just waned to make surebecause "0 < n < 10000" is litteraly how the assigment says it should look like.
Again, thanks!
Rewrite this condition
if (0 < n < 10000){
like
if (0 < n && n < 10000){
Otherwise the original condition looks like
if (( 0 < n ) < 10000){
and the result of the expression 0 < n is either 1 or 0. So in fact you are comparing 0 or 1 with 10000.
From the C Standard (6.5.8 Relational operators)
6 Each of the operators < (less than), > (greater than), <= (less than
or equal to), and >= (greater than or equal to) shall yield 1 if the
specified relation is true and 0 if it is false.107) The result has
type int.
The expression 0 < n < 10000 is equivalent to (0 < n) < 10000 which means you check if 0 < n is less than 10000, which it will always be (the result of a comparison like 0 < n will be zero or one).
I have this part of an if statement and I'm getting a weird output.
int x = 10;
if(1 < x < 5){
printf("F\n");
}
Why does it print "F"? Logically isn't the if statement false because x is greater than 1 but not less than 5?
In C, you can't chain comparisons like that. The expression 1 < x < 5 is evaluated as (1 < x) < 5: so for x = 10, the expression is (1 < 10) < 5. (1 < 10) is true, which C represents as the value 1, so the expression reduces to 1 < 5. This is always true, and your printf() if executed.
As level-999999 says, in C you need to explicitly combine single comparisons with && and ||.
If you are using C, you should have broken down the condition into two arguments :
if ( x > 1 && x < 5) {
printf("F\n");
}
This question already has answers here:
Why is a condition like (0 < a < 5) always true?
(4 answers)
Closed 9 years ago.
I have this question & in the answer it says that due to left to right associativity the result would be 1 that is true for this statement. This is the code.
#include<stdio.h>
int main ()
{
int i=0,x=10,y=10,z=5;
i=x<y<z;
printf("\n\n%d",i);
return 0;
}
But x is greater than z here so how is this happening ?
The expression x
(x < y) < z
so it becomes
(10 < 10) < 5
which further is evaluated into
0 < 5
which is true.
I think you wanted something like this:
x < y && y < z
Because of operator precedence and associativity
i = x < y < z;
is parsed as:
i = ((x < y) < z);
After substituting the variable values, this becomes:
i = ((10 < 10) < 5);
Since 10 < 10 is false, this becomes:
i = (0 < 5);
Since 0 < 5 is true, that becomes:
i = 1;
x<y<z is not a single valid expression. Instead it evaluates x<y first (operator precedence is done left to right here) as true/false (false in this case as they're equal), converts it to an int value of 0, and then compares this value with z.
Use (x < y && y < z) instead.
It first evaluates x < y which is false (0), then 0 < z which is true (1).
WHat C compiler does is, in x<y<z;
starts from left, so as x is not less than y therefore it replaces that expression with '0'
so it becomes 0<z and as that is true. it set the variable to 1.