1

So, I'm going to be up front, this may get my question taken down, but I don't have a lot to go off of so this may be an incomplete question, but I'm completely stuck. I am writing a program in C, to be able to count the occurrence of a character that is provided by the user. I am currently, trying to save the characters with getchar() within a while loop, as I try to display the results it gives me the whole input inside the loop, but when it exits the loop it saves the last char character (i understand why it is doing this, just not how to fix it). NOTE: This code contains extra parts not necessary to answer this question, those are for later on in my code. :) Here's my current code (3/12/18), I'm sorry I can't be more thorough:

#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#define N 26

int main(void) {

    char ch, index;
    int count[N] = {0}, tolower(int ch);

    printf("Enter a sentence (end by '.'): \n");
    while(ch != '.') {
        ch = getchar();
        putchar(ch);  \\print testing to see where the characters are "leaving"
    }
    printf("%c", ch);  \\print testing to see if the characters go past the loop

}
yajiv
  • 2,901
  • 2
  • 15
  • 25
Tcranmer
  • 29
  • 1
  • 10
  • 1
    (1) getchar() doesn't "save" anything. It gets the next character from the input, that's all. If you want to save these somewhere, that's your job. (2) getchar() returns an int, not a char, so you need to use an int variable for it. – Lee Daniel Crocker Mar 12 '18 at 22:23
  • 1
    (3) `tolower()` should be defined by including ``, not by a (bizarre) inline declaration inside `main()`. –  Mar 12 '18 at 22:23
  • (1) Ahhhhhh, that explains it, I was under the assumption that getchar acted like scanf for saving chars (2) Thanks for the input, I'll make sure to fix that (3) Ohhhhhhhh I thought 'tolower()' was given through the string library, thanks for the input. (4) I need to be able to enter a sentence that ends when it encounters the '.', which is saved and ran through a for loop (not written yet), that checks occurrences of each letter given by the user. – Tcranmer Mar 12 '18 at 22:28
  • `scanf` doesn't save chars either – M.M Mar 12 '18 at 22:30
  • I know, I thought it saved like scanf did but for chars – Tcranmer Mar 12 '18 at 22:31

1 Answers1

1

You need to do the loop events in this order:

  1. Read a character
  2. Abort the loop if it was '.' or EOF
  3. Write the character
  4. Go to 1

Also, you need to use int to store the return value of getchar() because otherwise you have no way of checking for EOF (which is not a character).

If you literally translate those steps to code you would get:

int ch;
for (;;)
{
     ch = getchar();
     if ( ch == '.' || ch == EOF )
          break;
     putchar(ch);
}

although it is a common idiom to combine the first two statements into the loop condition:

int ch;
while ( (ch = getchar()) != '.' && ch != EOF )
    putchar(ch);
M.M
  • 138,810
  • 21
  • 208
  • 365
  • it worked in fixing my problem, and as I write the for loop for my next part of my program, it saved the whole string throughout it seems, I appreciate the help :) – Tcranmer Mar 12 '18 at 22:35