Ahoj všichni,
prosím Vás o radu. Nevíte někdo, proč tělo cyklu proběhne dvakrát a vždy v druhém cyklu se tváří tak, jakoby to načetlo i ten enter.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int sw;
printf("\n\tTest kombinace fce getchar s while \n\n");
while (sw != 'q') {
// do {
printf("zadej sw : ");
sw = getchar();
printf("\nsw je: %d\n", sw);
}
// } while (sw != 'q');
system("PAUSE");
return 0;
}
Děkuji mnohokrát.
Fórum › C / C++
Getchar a while

Děkuji za odpovědi,
mephi, prosím tě. Jaké jsou vlastnosti fce getch()? Zkusil jsem to a vypadá to slibně, ale je mi divné, že z žádných "chytrých" knihách o ni není zmínka.
KIIV Přesně tak, tím entrem ale jak jinak to načítání znaku ukončit? a hlavně v jedné knize psali, že se getchar načte jen první znak (tak to mi asi lhali.. :smile10: a to by nebylo poprvé ani naposled). Možná, že se načte jen jeden a právě proto proběhne ten cyklus dvakrát a v tom druhém se načte ten entr. To si ne. Protože mi například v prubehu toho prvního cyklu ještě projde přes switch a tam se ještě neco vykoná, ale nic z klávesy při tom nezadávám. takže určitá pravěpodobnost takovéhoto chování by tu byla. No je to věda. A copak je prosímtě to ASDF? Jestli jen shluk pismen, tak po zadání to vypsalo ASCII všech pěti znaků (tj. i stim entrem).
děkuji a přeji pěkný den.
preferuj ' scanf("%c",&ch); ', tohle volani preskoci vsechny bile znaky (enter, mezera, tab...) na vstupu a prave jeden znak ulozi do promene ch.
co se tyce getch() a getchar() (pokud se nemylim tak se chovaji stejne, ale getchar je standardni a getch je z curses.h) tak ty prectou prave jeden znak ze vstupu a vrati jeho ASCII kod. kdyz napises ASDF\n, tak prave tyhle znaky pujdou na vstup a odtud si je ony funkce postupne budou odebirat. tedy ten enter tam zustane. elegantne se ho muzes zbavit vise zminenym scanf, pripadne to muzes osetrit podminkou typu !='\n';

Přidej příspěvek
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
































Uživatelé prohlížející si toto vlákno
Podobná vlákna
Getchar vs Gets — založil Slimer
Funkce getchar — založil xXx
C getchar EOF — založil Poki
Getchar() a EOF — založil dreamer
Dvakrát getchar() — založil vazbok
Moderátoři diskuze