What is wrong with this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is wrong with this code?

Using a do…while() loop, continuously scan for characters (one per line) and print it out afterwards. The loop shall terminate due to either of the following reasons: The inputted character is a vowel The number of inputted characters has already reached 5. It must also be guaranteed that if the number of inputted characters is less than 5, then there must be a vowel from among the inputted characters. (I tried answering this problem with this code here: https://code.sololearn.com/cA7a5A8a19a1/#) But i cant get all test cases correct, only 3/4. What am i doing wrong? Also i cant check what the 4th test case its because its hidden. Help? https://code.sololearn.com/cA7a5A8a19a1/?ref=app

18th Jan 2021, 8:40 AM
Mint
Mint - avatar
9 Answers
+ 2
run loop upto 4 becz count Start from 0 and till upto 4 #include <stdio.h> #include <string.h> #include <ctype.h> int main(){ char letter; int count=0; do{ scanf("%c", &letter); char c = tolower(letter); if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { printf("%c",c); break; } printf("%c",c); count++; } while(count<4); return 0; }
18th Jan 2021, 10:53 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Try to change your (while count<10) to (while count<5)
18th Jan 2021, 8:54 AM
noteve
noteve - avatar
+ 1
《 Nicko12 》 Doesn't work. Its restricting me to only 3 inputs now
18th Jan 2021, 8:59 AM
Mint
Mint - avatar
+ 1
Mint What about while(count < 7) ? This should get 5 inputs.
18th Jan 2021, 11:46 AM
noteve
noteve - avatar
+ 1
If you're giving input with single letter per line seperated by newline . Use scanf("%c\n", letter). And in while loop check use 5 instead of 10 While (count!=5) it will exit after count reaches 5. I also have checked and confirmed here. https://code.sololearn.com/cXP8a1WSKtL8/?ref=app Hope this helps .
19th Jan 2021, 3:41 PM
mahesh hegde
mahesh hegde - avatar
0
♨️♨️ Nope. Its only allowing me 2 character inputs
18th Jan 2021, 11:42 AM
Mint
Mint - avatar
0
《 Nicko12 》 Just tried. Only 4 inputs
18th Jan 2021, 11:48 AM
Mint
Mint - avatar
0
Is there input is accepted like "abcde" or line by line like a b C D E For 1st order use scanf("%c", &letter); and while(count<5); For 2nd order type, use : scanf("%c\n", &letter); and while(count<5); 1st one, reads charecter by charecter only while 2nd one reads new line also and I gores it then reads next charecter. Hope it helps...
18th Jan 2021, 6:31 PM
Jayakrishna 🇮🇳
0
#include <stdio.h> #include <string.h> #include <ctype.h> int main(){ char letter,c; int count=0; do{ scanf("%c", &letter); c = tolower(letter); if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { printf("First Vowel is"); break; } count++; } while(count<5); printf("%c",c); return 0; }
19th Jan 2021, 7:21 PM
Sufian Ahmad
Sufian Ahmad - avatar