0

I'm a newbie programmer. I have an assignment and I have some troubles about that. First of all my assignment is :

"Assume that you scan the input text in Turkish character by character from the keyboard, which you may think of as a default input device, until a ‘CTRL-D’ is pressed. You are supposed to skip punctuation characters as well as blank character. Your program will display the frequencies of letters and digits whenever the scanning process is done. You need to discuss the data structure as well as flowchart solution and hand in them with your code."

I did some research and found that the CTRL + D command corresponds to EOF. I wrote my code but it doesn't work as I want. Here is my code.

#include <stdio.h>
#include "stdlib.h"
#include <locale.h>

int main()
{
    setlocale(LC_ALL, "Turkish");    

    char message[1000] = { ' ' };
    char ch;
    int i = 0;

    printf("Please enter your message: ");

    while (ch = getchar() != EOF )
    {
        message[i] = ch;
        i++;
    }

    for (i; i >= 0; i--)
    {
        printf("%c", message[i]);
    }

    printf("\n\n");

    system("PAUSE");
    return 0;
}

When I run it, I write something and press ctrl + d, but as you can see in the picture, nothing happens. It just writes ^ D. Thank you for your help.

Picture 1

root_roxox
  • 179
  • 1
  • 9
  • 1
    [Control-D does not actually directly cause EOF on Unix machines. It can be used to cause EOF, but this requires there be no characters in the input buffer when it is typed.](https://stackoverflow.com/a/21365313/298225) – Eric Postpischil Apr 20 '20 at 10:37

1 Answers1

1

Ctrl-D corresponds to the integer character literal '\4' in C because A=1, B=2, C=3, D=4.

Your screenshot shows you're programming on Windows, so what you read about Ctrl-D being the same as EOF does not apply (it's a Unix/Linux/Mac thing).

To fix things, you need to read up until '\4' (or just 4; they are equivalent):

while ((ch = getchar()) != '\4')
{
    message[i] = ch;
    i++;
}

Note that you will still need to press your Enter or Return key for the line of text to be accepted, meaning you could type hello world^D what? i can still type? before hitting Enter to submit the text to your program. However, your program will stop reading at the ^D character, resulting in the message hello world being printed.

In the future, you might use something like printf("%d %c\n", message[i], message[i]); in a loop to print a list of the byte values you read.

MemReflect
  • 551
  • 2
  • 10
  • Thank u very much but when I did it, I get a output like this. https://imgur.com/a/Q8eyUIb – root_roxox Apr 20 '20 at 08:14
  • 1
    If you try it with regular ASCII characters, you'll find that it works. That issue with weird characters printing to the console has to do with your system locale and the font that the Windows console uses. You can find [all sorts of questions](https://stackoverflow.com/q/2492077/12483061) on Stack Overflow and other sites about Unicode output in the Windows console. A possible fix: while your program is running, you can also right-click on the title bar and select Properties, then choose the "Lucida Console" font if it is not already selected, which may or may not help with the output issue. – MemReflect Apr 20 '20 at 08:22
  • I did it but I've still this problem.. Can u say me what I should to do for prevent this? – root_roxox Apr 20 '20 at 08:32
  • 1
    Unfortunately, there's little else to try as far as I know. The Windows console is very limited. One other possible fix might be to call `_setmbcp(857)` after your `setlocale` line (857 is the OEM "code page" used for Turkish in the Windows console; don't forget to `#include `). I don't program on Windows, so I'm afraid I can't tell you much else. The main problem is that Windows programs use an ANSI code page by default while console programs use an OEM code page, and `setlocale()` doesn't necessarily work for changing the OEM code page. It's a Windows problem, not a C problem. :( – MemReflect Apr 20 '20 at 08:48