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);
Related
Q : Write a program to count the number of occurrences of any two vowels in succession in a line of text. For example the following sentence :
"Please read the application and give me gratuity"
Such occurrences in the sentence are :- "ea" , "ea" , "io" , "ui". Ultimately the question is to count the number of such occerrences in the line which is a user input string.
Problem : My program is just recieve a line but did not give any output.
It's my first question in stackoverflow. I am a beginner in programming.
My code:
# include <stdio.h>
int main () {
char line[100];
printf("\nEnter a line\n");
gets(line);
//printf("You entered : %s\n", line);
char A,E,I,O,U,a,e,J,o,u;
A = 'A';
E = 'E';
I = 'I';
O = 'O';
U = 'U';
a = 'a';
e = 'e';
J = 'i';
o = 'o';
u = 'u';
int occurence =0,i =0 ;
while (line[i] =! '\0'){
if((line[i] == A || E || I || O || U || a || e || J || o || u) && (line[i+1] == a || e || J || o || u)){
occurence++;
}
i++;
}
printf("Number of occurence of any two vowels in succession in the line is : %d\n", occurence);
return 0;
}
Your code has multiple issues, some of which are:
You use gets to read the string. This function is now at least deprecated (I think it was even removed from recent versions of the standard library) because it is unsafe. Use fgets instead!
You use the || operator wrong - this was already pointed out in mediocrevegetable1's comment.
You could use a code similar to this one to solve your problem. The code contains comments, so it should be easy to understand. However, if this is a homework or project for school, do NOT use this exact code, as this would most likely be considered plagiarism!
#include <stdio.h>
#include <ctype.h>
#define STRLEN 100
int main () {
char line[STRLEN];
char* ch;
char incrementIfVowel;
int occurences;
/* Read line */
printf("\nEnter a line\n");
fgets(line, STRLEN, stdin);
/* Init variables */
incrementIfVowel = 0;
occurences = 0;
/* Iterate through characters */
for (ch = line; *ch != '\0'; ch++) {
/* Test if the current character is a vowel. Three cases can occur: */
if (toupper(*ch) == 'A' || toupper(*ch) == 'E' || toupper(*ch) == 'I' || toupper(*ch) == 'O' || toupper(*ch) == 'U') {
if (incrementIfVowel == 1) {
/* Case 1: The current character is a vowel, and its predecessor was also a vowel */
incrementIfVowel = 0;
occurences++;
}
else {
/* Case 2: The current character is a vowel, but its predecessor was not a vowel or it was the second vowel in a row */
incrementIfVowel = 1;
}
}
else {
/* Case 3: The current character is not a vowel */
incrementIfVowel = 0;
}
}
/* Print result */
printf("Number of occurence of any two vowels in succession in the line is : %d\n", occurences);
return 0;
}
There are 3 main issues with your code:
gets(line);
gets doesn't check the length of the buffer and for this reason, is susceptible to buffer overflows. gets had been deprecated since C99 and was removed in C11. You're probably compiling with an older version of the standard; I'd suggest you switch to newer versions. As for an alternative, see fgets.
while (line[i] =! '\0'){
=! is a typo. Replace it with !=
if((line[i] == A || E || I || O || U || a || e || J || o || u) && (line[i+1] == a || e || J || o || u))
This will always evaluate to true because || doesn't chain like that. Ideally, you should put this in a function:
_Bool is_vowel(char ch)
{
return toupper(ch) == 'A' || toupper(ch) == 'E' || toupper(ch) == 'I' || toupper(ch) == 'O' || toupper(ch) == 'U';
}
toupper is defined in <ctype.h> so be sure to include that. You could also shorten this behemoth of a line with return strchr("AEIOUaeiou", ch), but if you haven't used strchr and are not comfortable with using it yet, that's okay.
Modifying only the incorrect parts, your final code will can look something like this:
#include <stdio.h>
#include <ctype.h>
_Bool is_vowel(char ch)
{
return toupper(ch) == 'A' || toupper(ch) == 'E' || toupper(ch) == 'I' || toupper(ch) == 'O' || toupper(ch) == 'U';
}
int main () {
char line[100];
printf("\nEnter a line\n");
fgets(line, sizeof line, stdin);
int occurence = 0, i = 0;
while (line[i] != '\0') {
if(is_vowel(line[i]) && is_vowel(line[i + 1]))
occurence++;
i++;
}
printf("Number of occurence of any two vowels in succession in the line is : %d\n", occurence);
return 0;
}
An example of running this:
Enter a line
Please do something about the bee hive and then eat some meat
Number of occurence of any two vowels in succession in the line is : 5
(5 because Pl<ea>se do something ab<ou>t the b<ee> hive and then <ea>t some m<ea>t)
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 need to make Check whether a character is a Vowel or Consonant as function and it returns 1 if it's a vowel and returns 0 if it's consonant
That's what I've come up with
#include <stdio.h>
#include <stdlib.h>
int vowel(char c) {
int lowercase, uppercase;
lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
uppercase = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (lowercase || uppercase)
return 1;
else
return 0;
}
int main() {
char c;
printf("Enter an alphabet: ");
scanf("%c", &c);
vowel(c);
}
Input: a
Output: Process returned 0
Your function int vowel(char c) looks OK, though for readability and better compiler optimization opportunities I'd use a switch statement, like this:
int vowel(char c) {
switch(tolower(c)){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return 1;
default:
return 0;
}
}
The second observation is that you seem to ignore the result of the function's execution altogether. I could only assume you were supposed to do something with it, like printing out the result. Something along these lines (instead of the v̶o̶w̶e̶l̶(̶c̶)̶;̶ code):
printf("\nThe character '%c' is %sa vowel\n", c, vowel(c) ? "" : "not ");
return type of vowel function is "int". But the function call is not storing the value. Change the function call as below.
int main() {
char c;
printf("Enter an alphabet: ");
scanf("%c", &c);
int flag = vowel(c);
printf("%d",flag); }
#include <stdio.h>
#include <stdlib.h>
int vowel(char c) {
int lowercase, uppercase;
lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
uppercase = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (lowercase || uppercase)
return 1;
else
return 0;
}
int main() {
char c;
printf("Enter an alphabet: ");
scanf("%c", &c);
int a=vowel(c);
if(a==1)
printf("vowel");
if(a==0)
printf("consonant");
}
Answering your questions one by one
Firstly,
what you have is a void function called vowel that takes in a char parameter.
When you declare a type of the function, what you are basically doing is telling the system what sort of return type you are expecting from this function. Since you are declaring the function void, you are expecting no return type from it hence, it does not matter what you return there.
Secondly,
when you declare the vowel function int, in your main function you have to store the value of the vowel function in a variable.
Thirdly,
the process returned 0 that you see is mainly the execution of your program saying no errors occurred.
What you probably are looking for is something like this I think,
if (lowercase || uppercase)
printf("1");
else
printf("0");
It's awesome to come up with own solutions, I would also suggest following the solutions given above as they are good coding practice.
I've been trying to get this code to work but the loop does not seem to work? I am very new to C and I sort of get confused with the syntax of this language. However my loop is not functioning like how I want it to be. I want the if and else statement to work but no matter what input (right or wrong) it always outputs "thank you".
#include <stdio.h>
#include <stdlib.h>
int confirm()
{
char c;
printf("Confirm (y/n): ");
scanf("%c", &c);
while (scanf("%c", &c))
{
if (c = 'Y' && 'y' && 'N' && 'n')
{
printf("\nthank you");
break;
}
else
{
printf("\nInput not recognised, try again. \n");
printf("\nConfirm (y/n): ");
scanf("%c", &c);
}
}
}
int main(int argc, char* agrv[])
{
confirm();
return 0;
}
it won't ask to enter another output when the output is incorrect. It just keeps ending from the if statement, thus the loop is not running?
Please help.
There's nothing wrong with your loop - it's the if statement that's wrong.
This code compiles, but it does not do what you want it to do:
if (c = 'Y' && 'y' && 'N' && 'n')
= is an assignment; you need == to do a comparison
&& means "AND"; you need ||, which means an "OR"
You combine logical expressions, not constants with && or ||
The condition should be
if (c == 'Y' || c == 'y' || c == 'N' || c == 'n')
Also note that when you read single characters with %c, your program "sees" all characters, including whitespace. This is a problem, because the '\n' left over in the buffer will be passed to your program before Y or N. To fix this, add a space before %c to your format string:
scanf(" %c", &c)
// ^
// |
// Here
Your code also ignores the first character that it reads. I think this is not intentional, so remove the call of scanf before the loop. You should also remove the second scanf from the loop, leaving the only call to scanf in the loop header.
int confirm()
{
char c;
printf("Confirm (y/n): ");
//scanf("%c", &c);// <---------- needless
while (scanf("%c", &c)) //<----while loop will do `scanf("%c",&c)`, so previous line should be remove.
{
if (c == 'Y' || c == 'y' || c == 'N' || c == 'n')// <- &&(AND); ||(OR). Also, be careful that don't be lazy, [c == 'Y' || 'y' || 'N' || 'n'] can't to communicate with computer
{
printf("\nthank you");
break;
}
else
{
printf("\nInput not recognised, try again. \n");
printf("\nConfirm (y/n): ");
scanf("%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')
...