breaking the while [SOLVED] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

breaking the while [SOLVED]

Hi there - simple task: reading 99 letters from stdin using getchar() and concatenate them to a string. then printing the string. if there was a input, that wasnt a letter (a..z, A..Z) exit the program. Code: while(count < 99) { input = getchar(); if((input >= 65 && input <= 90) || (input >= 97 && input <= 122)) { chararray[count] = input; count++; } else { printf("The last one wasnt a letter!"); break; } } Now the prob: without pressing return, the program never breaks the while-loop. but return is identified as a non-letter. even if i enter more than 100 letters and count is already at its limit, the while wont get broken. how to handle this? greetings!

19th Jul 2020, 5:46 PM
Oliver
9 Answers
0
Hey! I got the prob! The Code was working here on Sololearn - because of pressing the submit-button after the input. But i build the code in a real environment, with VSCode ... there happens to following: I type: abcdefhikljmnoplihjklop (then i press enter - here simulated by "submit") and the string shows: abcdefhikl so here - again, the while stops, but my input is not interrupted after the 10th letter, i could type on and on until i press enter... so you can expierience the error only by using a other environment. In sololearn everythings works, but in reality it doesnt. :/
20th Jul 2020, 2:59 PM
Oliver
+ 1
If am understood your question correctly : That, according to me - by using of getchar(), must need to press enter, for submitting the inputs. Then only inputs are read by getchar() (any number of charecter by character). Just telling, your program works like: If input into " input" variable is albhabet and if your input is like " aaaaaaaaxyz(200 chars) works fine... If input is" a(enter) a(enter)...... (200chars),then also works fine. If you mean, loop should stop after just typing 200 characters without pressing enter, then use getch() or getche() functions (instead of getchar()) if it supports in your compiler because those are from deprecated header conio.h. Read charecters immediately without pressing enter.... [Btw, it's not about trust, it is only not understandable to me..]. Hope it helps.. Happy coding..
20th Jul 2020, 2:53 PM
Jayakrishna 🇮🇳
0
Is count defined previously? Try printing count on each loop to check its value and be sure it reaches 99 and keeps going.
19th Jul 2020, 6:25 PM
Notyour Business
0
checked it. count is initialized with 0. count is increased and stops at 99. but the loop isnt left at that point. the point is, the getchar() waits for return... but i thought the loop should have been broken when count reaches the limit.
19th Jul 2020, 6:36 PM
Oliver
0
Its working .. How you initialized variables.. Share your code link ,that helps to rectify problem... edit: Oliver //code (for 10 inputs) #include <stdio.h> int main() { int count=0; char input; char chararray[15]=""; while(count < 10) { input = getchar(); if((input >= 65 && input <= 90) || (input >= 97 && input <= 122)) { chararray[count] = input; count++; } else { printf("The last one wasnt a letter!"); break; } } printf("%s",chararray); return 0; }
19th Jul 2020, 6:40 PM
Jayakrishna 🇮🇳
0
whole code: #include <stdio.h> #include <string.h> int main (){ int count = 0; char input,enter; char chararray[99]; while(count < 99) { printf("Geben Sie einen Buchstaben ein und bestätigen Sie mit Return!\nBuchstabe:"); input = getchar(); enter = getchar(); if((input >= 65 && input <= 90) || (input >= 97 && input <= 122)) { chararray[count] = input; count++; } else { printf("Die letzte Eingabe war kein Buchstabe!"); break; } } printf("\nString: %s \nAnzahl Buchstaben: %d",chararray,count); getchar(); return 0; } did a workaround with a 2nd getchar to capture the return. otherwise it is working,yes ... the array was always filled with 100 letters, but the output string only was shown if i pressed enter. if i pressed enter after 100 letters, everything was fine ... if i did it before, the message was shown up. the prob is: if i was entering aaaaaaaaaaaaaaa(120 times) the result-string was never shown, but the loop was already done at this point. i just came to the point to show the result by pressing return.
19th Jul 2020, 6:55 PM
Oliver
0
No. With that input 60 characters will be read into "input" variable and another will into " enter variable". So your count is 60 only... Not 100. Since you are increasing count in if condition.. Try with 10 or 20 chars instead of big number..
19th Jul 2020, 8:16 PM
Jayakrishna 🇮🇳
0
Why do you have input and enter? I mean, you don't even refer to 'enter' at any point in the whole code besides declaring it and using it to save a second char before checking the value in 'input'.
19th Jul 2020, 8:36 PM
Notyour Business
0
the prob is, without pressing return the while is left, but the string output was never shown. the getchar waits for return, and to capture this non-letter input, i store it in enter. try this code without a second getchar on VsCode or what ever, then you will see it ... i completly debugged it, the count is filled up to 99, and the array is also filled... but without pressing enter the string output wont occure. trust me ...
20th Jul 2020, 2:14 PM
Oliver