Issues with if/else statements in C - c

Trying to write a simple calculater in C. I'm stuck in having the program terminated if letter 'A', 'B', 'C' or 'D' is not entered, otherwise continue. Even though I enter a valid character it never proceeds.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char letter;
float num1,num2;
printf("What operation would you like to perform?\n\tA) Addition\n\tB) Subtraction\n\tC) Multiplication\n\tD) Division\n\n");
scanf("%c", &letter);
if (letter != 'A' || letter != 'B' || letter != 'C' || letter != 'D')
printf("Not a valid operation\n");
return 1;
printf("Please enter first number: ");
scanf("%f", &num1);
printf("Please enter second number: ");
scanf("%f", &num2);
if (letter == 'A' || letter == 'a')
printf("The sum of %f and %f, is %f\n", num1, num2, num1 + num2);
else if (letter == 'B' || letter == 'b')
printf("The difference of %f and %f, is %f\n", num1, num2, num1 - num2);
else if (letter == 'C' || letter == 'c')
printf("The product of %f and %f, is %f\n", num1, num2, num1 * num2);
else if (letter == 'D' || letter == 'd')
printf("The quoation of %f and %f, is %f\n", num1, num2, num1 / num2);
else
printf("The operation was not valid");
return 0;
}
Thank you for your help.

if (letter != 'A' || letter != 'B' || letter != 'C' || letter != 'D')
printf("Not a valid operation\n");
return 1;
This part is the problem. Although return 1; is indented, it will execute regardless because it is not part of the if block. Additionally, you are using the wrong operator, your condition statement reads "if letter is not A or letter is not B ..." which is always going to be true because letter cannot be both A and B at the same time. You need to envelope the two statements and use the && operator instead, like this:
if (letter != 'A' && letter != 'B' && letter != 'C' && letter != 'D')
{
printf("Not a valid operation\n");
return 1;
}
In C, indentation is meaningless to the compiler. If you need to execute multiple statements as a result of a condition, they must be wrapped up into a compound statement using { and }.

You check
if (letter != 'A' || letter != 'B' ...)
Ok - if letter is 'B', then it is not A, and the program stops testing there and prints your failure condition and returns.
On the other hand, if letter was 'A', it would not be 'B', and so it would fail the second test instead, and fail there.
What you want:
if (letter != 'A' && letter != 'B' && letter != 'C' && letter != 'D')
Alternately, you could use the C function "strchr" which searches for a character in a string.
if (!strchr("ABCDabcd", letter)) // returns NULL, which is false, if no match
{
printf("Invalid operation");
return 1;
}

Try:
if (letter != 'A' && letter != 'B' && letter != 'C' && letter != 'D')
...

Related

I need help writing a Loop that finds vowels in C

I need to prompt the user for a letter. If that letter is not a vowel, keep prompting until a vowel is entered.
My desired outcome is:
Enter a vowel:z
That is not a vowel. Try again:h
That is not a vowel. Try again:w
That is not a vowel. Try again:t
That is not a vowel. Try again:o
Congrats. You picked a vowel.
So far my code is:
do
{
printf("Enter a vowel: ");
scanf(" %c", &letter);
}
while (letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u' && letter != 'A' && letter != 'E' && letter != 'I' && letter != 'O' && letter != 'U');
{
printf("That is not a vowel. Try again: ");
scanf(" %c", &letter);
}
printf("Congrats. You picked a vowel!!!");
Your braces, indentation and spacing are deceptive. It looks like the do has no while and that the while controls the block below it...
Try this:
/*...*/
while( 1 ) {
printf( "Enter a vowel: " );
char letter;
if( scanf( " %c", &letter ) != 1 )
exit( -1 );
if( strchr( "aeiouAEIOU", letter ) != NULL ) // found!
break;
printf( "That is not a vowel. Try again:\n\n" );
}
printf( "Congrats. You picked a vowel!!!\n" );
// NB: 'letter' has "gone out of scope".
/*...*/
strchr() searches the string supplied for a matching character, returning NULL if it is NOT found (ie: not a vowel, in this case.)
The "infinite loop" can only "break" when the user supplies a vowel.
Add a few LFs (\n) to the print statements.
Do and While should go together. So you are looking for a code that will be something like below.
Your objective should be to ensure the user is in loop till a vowel is received. Also you can further improve the code by using functions for the vowel check. Find a sample below.
do
{
printf("Enter a vowel: ");
scanf(" %c", &letter);
if (letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u' && letter != 'A' && letter != 'E' && letter != 'I' && letter != 'O' && letter != 'U')
printf("Sorry, Try again \n");
} while (letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u' && letter != 'A' && letter != 'E' && letter != 'I' && letter != 'O' && letter != 'U');
printf("Congrats. You picked a vowel!!!");
}
The applicable loops in C are do-while, for and while. There is no do.
Hopefully OP will see the problem once OP's code is properly formatted.
do {
printf("Enter a vowel: ");
scanf(" %c", &letter);
} while (letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o'
&& letter != 'u' && letter != 'A' && letter != 'E' && letter != 'I'
&& letter != 'O' && letter != 'U');
{
printf("That is not a vowel. Try again: ");
scanf(" %c", &letter);
}
There is no error message in the loop.
A more concise way to achieve that would be using strchr(), as suggested by #tadman in the comments.
#include <stdio.h>
#include <string.h>
int main () {
char * vowels = "aeiouAEIOU";
char letter;
do {
printf("Enter a vowel: ");
scanf(" %c", &letter);
} while (strchr(vowels, letter) == NULL);
printf("Congrats. You picked a vowel!!!");
}

how to put if..else statement in do..while loop c program

so I have been working on my assignment and I can't figure out how to put if...else statement in my do..while loop because want I run the program it doesn't loop. the output is like this.
[this the output that I got][1]
#include <stdio.h>
#include <ctype.h>
int main(void){
char alphabet;
char confirm;
int lowercase_vowel, uppercase_vowel;
int a =1;
do{
printf("Enter an alphabet: ");
scanf("%c", &alphabet);
// evaluates to 1 if variable c is a lowercase vowel
lowercase_vowel = (alphabet == 'a' || alphabet == 'e' || alphabet == 'i' || alphabet == 'o' || alphabet == 'u');
// evaluates to 1 if variable c is a uppercase vowel
uppercase_vowel = (alphabet == 'A' || alphabet == 'E' || alphabet == 'I' || alphabet == 'O' || alphabet == 'U');
if(!isalpha(alphabet))
{
printf("Error! Non-alphabetic character.\n");
}
else if(lowercase_vowel || uppercase_vowel)
{
printf("%c is a vowel.\n", alphabet);
}
else
{
printf("%c is a consonant.\n", alphabet);
}
printf("\nif u want to proceed enter 1, if not enter 0\n");
scanf("%d", &a);
}while( a == 1);
}
``
[1]: https://i.stack.imgur.com/oRK1G.png
do
{
// code
}while (a = 1);
This will create an infinite loop, because it will assign 1 to a, and because a is now a nonzero value, the condition is true, and it will loop again:
Maybe what you want is:
do
{
// code
}while (a == 1); // comparison, not assignment
Also:
scanf("%d", a);
should be:
scanf("%d", &a);

How to do loop menu in C

I'm trying to do loop menu with some basic functions, everything is working fine apart of looping menu
in my opinion i have something wrong with while loop but i can't figure out what it is.
int main(void) {
char letter;
char status = 0;
printf ("--------------------------------------\n");
printf("a – Calculate the area of a rectangle\n");
printf("b – Calculate the area of a circle\n");
printf("c – Display a multiplication table\n");
printf("d – Add two numbers\n");
printf("x - exit program\n");
printf ("--------------------------------------\n");
scanf("%c",&letter);
while (status == 0)
{
if (letter == 'a' || letter == 'A')
{
}
if (letter == 'b'|| letter == 'B')
{
}
if(letter == 'c'|| letter == 'C')
{
}
if (letter == 'd'|| letter == 'D')
{
}
if(letter == 'x' || letter == 'X')
{
printf("shut down\n");
break;
}
status ++
}
return 0;
}
You need to pace a scanf(" %c",&letter); inside the loop body; otherwise, you will not get a chance to ever enter an x...
Please note the space before the %c, i.e. the " %c"-format, which captures any new line in the input buffer from a previous input.
Maybe you meant that status will be int and not char?
You should read input also in the beginning of loop, otherwise you will take only one input
After the first iteration, you advance status so it will exit the loop. Is this what you tried to achieve? I guess you meant for:
if(letter == 'x' || letter == 'X')
{
printf("shut down\n");
status ++
break;
}
Your loop will run only one time cause 'status ++' will work no matter the condition you should use it inside the x case -
if(letter == 'x' || letter == 'X')
{
printf("shut down\n");
status ++;
}
This should break your loop only after 'x' is entered.

I'm learning C language but I cannot run the 'y' to continue the program

Here i get the answers for vowels but not for continue it says error for the continue and also for break.
#include < stdio.h >
main() {
char c, d;
printf("say your word to find the vowels\n");
scanf("%c", & c);
if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U')
printf("you got a vowel\n");
else
printf("cool no vowel word\n");
printf("continue\n");
printf("(y/n)\n");
scanf("%c", & d);
if (d == 'y' || d == 'Y')
continue;
else
break;
return 0;
}
if (d == 'y' || d == 'Y')
continue; // where?
else
break; // what?
You have nothing to continue or break in your main function.
Both continue and break only make sense within a loop, so you should add a while(true) loop around the code in main() to repeat the whole thing until the user decides to quit.
Continue and break works in the loop block.. but here you have not added them in loop. Put the whole if block in the while(1) and it will work
#include <stdio.h>
int main()
{
char c;
int d;
while (1) {
printf("say your word to find the vowels\n");
scanf("%c", &c);
if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U')
printf("you got a vowel\n");
else
printf("cool no vowel word\n");
printf("To continue enter 1\n");
scanf("%d", &d);
if (d == 1)
continue;
else
break;
}
return 0;
}
Check the continue and break statement syntax in the following link.
https://www.codingunit.com/c-tutorial-for-loop-while-loop-break-and-continue

Making simple calculator in C

I'm making a simple calculator in C with operators +-*/. I tried to make a calculator but I have some problem with using do..while. I think while (op != 'A', 'Q', 'M', 'D', 'S'); is incorrect. What should I require here?
#include<stdio.h>
int main(void)
{
int x, y, result;
char op;
printf("****************\n");
printf("A-----Add\n");
printf("S-----Substract\n");
printf("M-----MUltiply\n");
printf("D-----Divide\n");
printf("Q-----Quit\n");
printf("****************\n");
printf("Enter the operation:");
scanf("%c", &op);
printf("Enter the two variables:");
scanf("%d %d", &x, &y);
do {
if (op == 'A')
result = x + y;
else if (op == 'S')
result = x - y;
else if (op == 'M')
result = x*y;
else if (op == 'D')
result = x / y;
else if (op == 'Q')
break;
} while (op != 'A', 'Q', 'M', 'D', 'S');
printf("%d %c %d = %d\n", x, op, x, result);
return 0;
}
!= is a binary operator, it takes one value on the left and one value on the right. You can't compare multiple values at once by just listing several values on the right.
To get this code working with minimal changes, you can compare op to each the variables individually, and combine them using the standard logical operators (e.g. &&).
You also need to think about the logic of your code. You're using a do ... while loop, which means that code will run at least once, and each time it reaches the end it will run again if the while condition is true. What happens after its first run? What happens if the user input (op) is none of the options specified? And you've got a 'quit' option, but what happens when it's used?
Four issues:
This: while (op != 'A', 'Q', 'M', 'D', 'S') doesn't do what you expect. You need to compare op against each value individually and do a logical AND (&&) between them.
If you don't enter a proper letter, your while loop will be an infinite loop because the prompt for user data is outside of the loop instead of inside. Move the printf and scanf calls to the inside of the loop.
If you do get an invalid input, the newline pressed by the user will get read in as the value to the first scanf. Put a blank space at the beginning of each scanf pattern to eat any newlines that may be in the buffer.
When you're printing the result, you're printing x as the second operand instead of y.
When you apply the above, you should have this:
#include<stdio.h>
int main(void)
{
int x, y, result;
char op;
printf("****************\n");
printf("A-----Add\n");
printf("S-----Substract\n");
printf("M-----MUltiply\n");
printf("D-----Divide\n");
printf("Q-----Quit\n");
printf("****************\n");
do {
// these four lines go inside the loop to prompt again
printf("Enter the operation:");
scanf(" %c", &op); // the space at the start eats the newline
printf("Enter the two variables:");
scanf(" %d %d", &x, &y); // the space at the start eats the newline
if (op == 'A')
result = x + y;
else if (op == 'S')
result = x - y;
else if (op == 'M')
result = x*y;
else if (op == 'D')
result = x / y;
else if (op == 'Q')
break;
// compare op against each value individually
} while (op != 'A' && op != 'Q' && op != 'M' && op != 'D' && op != 'S');
printf("%d %c %d = %d\n", x, op, y, result);
return 0;
}
while (op != 'A' && op != 'Q' && op !='M' && op !='D' && op !='S');

Resources