So it compiles ok, but the output it is not the expected. The issue I think is in the OR operator which does not work properly, or it conflicts with the && operator. But perhaps it's bad written or something. Please let me know. Thanks in advance.
Here's the code:
if((len == 16) && secDigit == (51 | 52 | 53 | 54 | 55)
{
printf("MASTERCARD\n");
exit(0);
}
else if((len == 15) && secDigit == (34 | 37))
{
printf("AMEX\n");
exit(0);
}
else if((len == (16 | 13)) && firstDigit == 4)
{
printf("VISA\n");
exit(0);
}
else
{
printf("INVALID\n");
exit(0);
}
| is the Binary OR operator while what you need is || ,the Logical OR operator.
Syntax:
a|b
(Boolean 1) || (Boolean 2)
Note: Conditional operators evaluate to boolean values
if((len == 16) && (secDigit >= 51 && secDigit<= 55))
{
printf("MASTERCARD\n");
exit(0);
}
else if((len == 15) && (secDigit == 34 || secDigit == 37))
{
printf("AMEX\n");
exit(0);
}
else if((len == 16 || len == 13)) && firstDigit == 4)
{
printf("VISA\n");
exit(0);
}
else
{
printf("INVALID\n");
exit(0);
}
Related
So currently I am trying to write code for an arduino car/robot to solve a maze made of black tape on white background. The current sensors I am working with is a wheel encoder(lm393), yellow standard motors, and IR emmiters and detectors in paralell(QTR-8RC).
Currently I am having a problem where I have code to follow the line, and I have code to turn the car around 180 degrees by counting the encoder pulses once it exits the line. The problem is that the car just keeps spinning and that is because it is stuck in the function turnAround();. Is there any way I can go back to the lineFollow(); function once my car is done rotating 180 degrees?
The code used is here:
void turnAround()
{
// NOW THE problem is that the counter is not jumping back to the line follower
while(countRW <= 27 && countLW <= 27) {
printEncoderMesurements();
analogWrite(RWF, 180);
analogWrite(RWB, 0);
analogWrite(LWF, 0);
analogWrite(LWB, 180);
}
resetCounters();
followLine();
}
void followLine() {
if (
((s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 0))||
((s2 == 1) && (s3 == 1) && (s4 == 1) && (s5 == 0))||
((s2 == 0) && (s3 == 1) && (s4 == 1) && (s5 == 1))||
((s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 1))||
((s2 == 1) && (s3 == 1) && (s4 == 0) && (s5 == 0))||
((s2 == 0) && (s3 == 0) && (s4 == 1) && (s5 == 0))||
((s2 == 0) && (s3 == 1) && (s4 == 0) && (s5 == 0))){
forwards();
}else if(s0 == 1 || s1 == 1) {
slowLeft();
}else if(s7 == 1 || s6 == 1){
slowRight();
}else if((s0 == 0)&&(s1 == 0)&&(s2 == 0) && (s3 == 0) && (s4 == 0) && (s5 == 0) && (s6 == 0) && (s7 == 0)) {
turnAround();
}
}
void loop(){
makeIRReadings();
printEncoderMesurements();
followLine();
delay(50);
}
I have tried every variations of different exit(0) or return 0 but none worked.
Your while loop is checking if countRW and countLW are less than 28, but while its looping in that while loop, you never update countRW or countLW so they will forever be less than 28.
Also, I'm not sure how your sensors are setup, but it seems like you drive off the line before turning around. So then after it turns around, it's still off the line, so it will call turnAround() again. You need a way to "catch" the line after turning around so it could follow it again.
I am trying to make this short program work, but it gives a message of "expect expression" in the following part:
else
{
printf("program error!");
}
It looks like I am not using the if-elseif-else correctly. I've searched the web and found that the format is how I have it. Please help.
#include <stdio.h>
int main( )
{
double height, weight, bmi;
int bmievalcode;
while( 1 )
{
/* --> add code to input weight and height between here */
printf("Please enter weight in pounds and height in inches\n");
scanf("%lf %lf", &weight, &height);
bmi = (weight * 703.0)/(height * height);
bmievalcode = -1;
if (bmi < 18.5)
{
bmievalcode = 1;
}
else if (bmi >= 18.5 && bmi <25.0)
{
bmievalcode = 2;
}
else if (bmi >=25.0 && bmi < 30.0)
{
bmievalcode = 3;
}
else if (bmi >=30.0)
{
bmievalcode = 4;
}
/* bmievalcode:
* 1 = underweight
* 2 = normal
* 3 = overweight
* 4 = obese
*/
if (bmievalcode == 1 || bmievalcode == 2 || bmievalcode == 3 || bmievalcode == 4)
printf("bmi = %6.1lf, evaluation is ",bmi);
{
if (bmievalcode == 1)
printf("underweight");
else if (bmievalcode == 2)
printf("normal");
else if (bmievalcode == 3)
printf("overweight");
else if (bmievalcode == 4)
printf("obese");
printf(".\n");
}
else
{
printf("program error!");
}
}
return 0;
}
Your code is wrong here:
if (bmievalcode == 1 || bmievalcode == 2 || bmievalcode == 3 || bmievalcode == 4)
printf("bmi = %6.1lf, evaluation is ",bmi);
{
if (bmievalcode == 1)
printf("underweight");
else if (bmievalcode == 2)
printf("normal");
else if (bmievalcode == 3)
printf("overweight");
else if (bmievalcode == 4)
printf("obese");
printf(".\n");
}
else
{
printf("program error!");
}
The {} block isn't part of the if, it's separate. Then you have an else, but it's not directly after an if statement, which causes the error you see.
Move the line printf("bmi = %6.1lf, evaluation is ",bmi); to be after the opening {.
The statement directly after the if is what is affected by the condition. If this could compile, only that first printf would be conditionally executed. The curly-brace block would always run.
In this case, GCC is a little more helpful than Clang, telling you error: 'else' without a previous 'if'
I'm not able to find the write line for third case. I'm a beginner in programming world. Much appreciated if someone helped me with this Thanks in advance!
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n%3==0) {
printf("\n 1");}
else if(n%5==0){
printf("\n 2");}
else if((n%3==0) && (n%5==0)){
printf("\n 3");}
else{
printf("\n 4");
}
return 0;
}
The 3. test condition, else if ((n % 3 == 0) && (n % 5 == 0)), will never be true, because if n % 3 == 0 the 1. test, if (n % 3 == 0), will catch it before. The same goes for the case if n % 5 == 0 with the 2. test, else if (n % 5 == 0).
You need to place if ((n % 3 == 0) && (n % 5 == 0)) at the start instead to proof both sub-expressions before the testing for each sub-expression:
scanf("%d",&n);
if ((n % 3 == 0) && (n % 5 == 0)) {
printf("\n 1");
}
else if (n % 5 == 0) {
printf("\n 2");
}
else if (n % 3 == 0) {
printf("\n 3");
}
else {
printf("\n 4");
}
First you check if the number can be divided by 3, then you check if it can be divided by 5 and then if it can be divided by both.
If it can be divided by both (in other words it can be divided by 15), then it can be divided by 3, so you'll get out of the first loop and print "\n 1".
Change the order of your test. If you put the longest case first, then if both n%3 and n%5 == 0, it will print. Otherwise, it can be one or the other.
if (n%3 == 0 && n%5 == 0)
{
printf("\n 3"); // You might want this to be printf("3\n"); instead
}
else if... // other cases.
Another way to do it if you really want to keep the order is to test on the single cases that they aren't both true, ie:
if (n%3 == 0 && n%5 != 0)
{
// Only a multiple of 3
}
else if (n%5 == 0 && n%3 != 0)
{
// Only a multiple of 5
}
else if (n%3 == 0 && n%5 == 0)
{
// Multiple of 3 and 5
}
I'm not able to find the write line for third case.
else if((n%3==0) && (n%5==0))
3rd line never true as first if(n%3==0) would have also been true and code would have executed that block.
Simplify with
// v------v 1 for multiple of 3
// v------v 2 for multiple of 5
int m15 = (n%3 == 0)*1 + (n%5 == 0)*2;
if (m15 == 0) m15 = 4;
printf("\n %d", m15);`
I've created a simple guessing game. Pick a number between 0 and 100, depending on the number you enter the program will output hot, Warm, Cold or correct!. The number I've chosen to be the correct answer is 51. My program compiles and runs but always outputs correct! for every value I enter. Thanks for helping if you decide to.
int main(void)
{
int number, answer;
answer = 51;
printf("Enter a number between 0 and 100:\n");
scanf("%d", &number);
if ((number > 51) && (number <= 56) || (number < 51) && (number >= 46))
{
printf("Hot\n");
}
else if ((number > 56) && (number <= 66) || (number < 46) && (number >= 36))
{
printf("Warm\n");
}
else if ((number > 66) && (number <= 100) || (number <36) && (number >= 0))
{
printf("Cold\n");
}
else if ((number > 100) || (number < 0))
{
printf("Error number has to be between 0 and 100 - re run\n");
}
else (number = 51);
{
printf("Correct!\n");
}
system ("pause");
return 0;
}
This little snippet:
else (number = 51);
{
printf("Correct!\n");
}
is actually equivalent to:
else
(number = 51);
//=======================
{
printf("Correct!\n");
}
The first part of that is the else clause which, if activated, will set number to 51.
The second part is totally disconnected from the if statement section and is a standalone block that will execute no matter what.
If you must put the condition there for readability (and it would be == rather than =), you could do:
else // (number == 51)
{
printf("Correct!\n");
}
As an aside, you could make your code shorter and more readable by starting at the specific and moving to the general, something like (just replacing the if statement):
if (number == 51)
printf ("Correct!\n");
else if ((number >= 46) && (number <= 56))
printf ("Hot\n");
else if ((number >= 36) && (number <= 66))
printf ("Warm\n");
else if ((number >= 0) && (number <= 100))
printf ("Cold\n");
else
printf ("Error, number has to be between 0 and 100 - re run\n");
}
You don't need to ever check the two ranges (above and below) for "temperature" since the inner ranges have already been checked.
else (number = 51);
{
printf("Correct!\n");
}
can be re-written as
else
{
number = 51;
}
{
printf("Correct!\n");
}
So irrespective of what your number is the printf("Correct!\n"); gets executed.
It can be fixed like
else
{
printf("Correct!\n");
}
remove the condition from the else statement.
int main(void)
{
int number, answer;
answer = 51;
printf("Enter a number between 0 and 100:\n");
scanf("%d", &number);
if ((number > 51) && (number <= 56) || (number < 51) && (number >= 46))
{
printf("Hot\n");
}
else if ((number > 56) && (number <= 66) || (number < 46) && (number >= 36))
{
printf("Warm\n");
}
else if ((number > 66) && (number <= 100) || (number <36) && (number >= 0))
{
printf("Cold\n");
}
else if ((number > 100) || (number < 0))
{
printf("Error number has to be between 0 and 100 - re run\n");
}
else
{
printf("Correct!\n");
}
return 0;
}
your code says: if none of the if statements is satisfied, assign 51 to number. and then always print "Correct!\n".
In the last else statement, you should either use
else if (number == 51)
{
...
}
or
else
{
...
}
I am trying to execute a program that prints the numerical value when the && operator returns true and when it returns false. The code is as follows:-
#include <stdio.h>
main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("Part I\n");
printf("(a%2 == 0) && (b%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
printf("(a%3 == 0) && (b%3 == 0): %d\n",(a%3 == 0) && (b%3 == 0));
printf("(a%5 == 0) && (b%5 == 0): %d\n",(a%5 == 0) && (b%5 == 0));
printf("(a%7 == 0) && (b%7 == 0): %d\n",(a%7 == 0) && (b%7 == 0));
printf("Part II\n");
printf("The AND operator yields: %d\n",(a%2 == 0) && (b%2 == 0));
printf("The AND operator yields: %d\n",(a%3 == 0) && (b%3 == 0));
printf("The AND operator yields: %d\n",(a%5 == 0) && (b%5 == 0));
printf("The AND operator yields: %d\n",(a%7 == 0) && (b%7 == 0));
return 0;
}
The output ( along with my input ) is as follows:-
210
210
Part I
(a%2 == 0) && (b%2 == 0): %d
(a%2 == 0) && (b%2 == 0): %d
(a%2 == 0) && (b%2 == 0): %d
(a%2 == 0) && (b%2 == 0): %d
Part II
The AND operator yields: 1
The AND operator yields: 1
The AND operator yields: 1
The AND operator yields: 1
Why is the first part behaving in such a manner? This is happening even when I replace && by ||. I am using a Borland C++ Compiler 5.5 . Please Help.
Because if you want to actually display a %, then you must escape it in the printf format string with another %. e.g.
printf("(a%%2 == 0) && (b%%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
^ ^
I've tested this with http://codepad.org/, which I think uses gcc, and the code worked ok. But you might try to add an extra % before a literal % (i.e, %%) so the compiler knows the % that follows is an actual character. Like this:
printf("Part I\n");
printf("(a%%2 == 0) && (b%%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
printf("(a%%3 == 0) && (b%%3 == 0): %d\n",(a%3 == 0) && (b%3 == 0));
printf("(a%%5 == 0) && (b%%5 == 0): %d\n",(a%5 == 0) && (b%5 == 0));
printf("(a%%7 == 0) && (b%%7 == 0): %d\n",(a%7 == 0) && (b%7 == 0));
You are actually using illegal escape sequence character to print % in the first part. Thats why printf is yielding garbage values.
printf("(a%2 == 0) && (b%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
^ ^
Here is you are mistaking
It should be like
printf("(a%%2 == 0) && (b%%2 == 0): %d\n",(a%2 == 0) && (b%2 == 0));
You can also read about all format specifiers used in C.