#how to compile a program with getchar ?

1 messages · Page 1 of 1 (latest)

dapper linden
#

tried doing ctrl+d but it didnt help, what should i do?

frigid jasperBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

#

@dapper linden

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

dapper linden
#
#include <stdio.h>
#include <stdbool.h>

bool isWhite(char c){
if(c == '\n' || c == ' ' || c == '\t'){
return true;
}else{
return false;
}
//return false;
}

bool isLetter(char c){
   if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){
        return true;
   }else{
        return false;
}
}

bool isDig(char ch){
    if (ch>='0' && ch<='9'){
        return true;
    }else{
        return false;
}
}

int main(){
char ch;
int tavim=0,words=0,lines=0,whitespaces=0,numOfLet=0,numOfDig=0;
int wordy=0;
while ((ch == getchar()) != EOF){
        tavim++;

if(isLetter(ch)){
        numOfLet++;
        wordy=1;
}else if(isDig(ch)){
        numOfDig++;
        wordy=1;
}else if(isWhite(ch)){
        whitespaces++;
if(ch == '\n'){
        lines++;
}
if(wordy==1){
        words++;
}
}
}

printf("total chars: %d", tavim);
printf("total words: %d", words);
printf("total lines: %d", lines);
printf("total numbers: %d", numOfDig);
printf("total Letters: %d", numOfLet);
printf("total whitespaces: %d", whitespaces);
return 0;

}

dapper linden
inner patio
#

how are you counting words? as it is, you'll count every character from the first instance of anything that matches isLetter() or isDig() until the end of the program as a new "word", which i doubt is what you want. the easiest thing to modify your current program to actually count words might be a rising edge detector -- that is, an if statement that is true if and only if wordy was 0 on the previous iteration of the loop, and is now 1 on the current iteration of the loop

#

also, might i suggest <ctype.h>? it has the functions isspace() (an analogue for your isWhite() function that also checks for tabs), isalpha() (a near-direct replacement for your isLetter() function), isdigit(), etc

#

oh, the reason that pressing ctrl+D didn't work is that you're doing ch == getchar(). in this case you want the single equals, not the double equals, and even then you'll still have to press ctrl+D twice

#

not quite sure how you'll manage to detect the first ctrl+D using just getchar(), though

shy nebula
shy nebula
shy nebula
dapper linden
frigid jasperBOT
#

This question is being automatically marked as stale.
If your question has been answered, type !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.

dapper linden
#

!solved