This question already has answers here:
How to send EOF via Windows terminal
(3 answers)
Closed 5 years ago.
I am working through K&R's exercises right now and I am at the one where you count the number of blanks, spaces, and tabs using the C language. I have built the following code:
#include <stdio.h>
#include <stdlib.h>
/*Write a program that counts blanks, tabs, and newlines*/
int main()
{
int c, numblanks, numtabs, numnewlines;
numblanks = 0;
numtabs = 0;
numnewlines = 0;
printf("Enter some text and press \"Enter\"\n");
while ((c = getchar()) != EOF) {
if (c == ' ')
++numblanks;
if (c == '\t')
++numtabs;
if (c == '\n')
++numnewlines;
}
printf("The total number of blanks is %i\n", numblanks);
printf("The total number of tabs is %i\n", numtabs);
printf("The total number of new lines is %i\n", numnewlines);
}
I am using Codeblocks and the built-in GCC compiler that is installed with it on a Windows 10 OS. When I run the program, I type some text into the program window that pops up and press "Enter" and nothing happens. I am not sure why. I was wondering if someone could help me get another pair of eyes on my code to see if there is something I am missing. Here is an image of what happens in the program when I run it:
Program window with typed text, but no reaction
I have not tried your solution but sometimes this can happen when the program is finished, the terminal closes. Try to put a getchar at the end of your program.
printf("Enter key to exit");
getchar();
This can help after string input (but sometimes require creating loop with getchar() and checking the result of this function)
Related
This program is returning something which I'm not able to comprehend. Attached is the screenshot of the O/P simple program to find number of spaces, tabs, etc.
What am I missing?
#include <stdio.h>
#include <string.h>
int main() {
int count[] = { 0, 0, 0 }; /* 0 is spaces, 1 is tabs and 2 for newline. */
int string;
printf("Enter the paragraph: \n");
while ((string = getchar()) != EOF) {
if (string == ' ')
count[0]++;
else if (string == '\t')
count[1]++;
else if (string == '\n')
count[2]++;
}
printf("There are %d Spaces.\n", count[0]);
printf("There are %d Tabs.\n", count[1]);
printf("There are %d Newlines.\n", count[2]);
return 0;
}
From the screenshot it appears you typed Ctrl-Z to signal the end of file to your program. While this works in legacy systems such as MS/DOS and the Windows terminals, this key combination has a different meaning on unix systems such as linux: it causes the current process to be suspended by its the running shell parent. The process can be resumed later with the fg command.
To signal the end on file on this system, you should type Ctrl-D instead.
Your program should produce the expected result then. The code seems OK, albeit it is quite confusing to name string an int variable that gets a single byte from getchar(). Such a variable is usually named c.
If you want to send an EOF in order to stop reading you should use CTRL+D.
This question already has answers here:
C/C++ printf() before scanf() issue
(2 answers)
Closed 4 years ago.
I am a java programmer and started learning C recently. I was going through headfirst C and wrote one of the example programs in Eclipse (with CDT). Here is the program
#include <stdlib.h>
#include <stdio.h>
int main()
{
char card_name[3];
puts("Enter the card_name: ");
scanf("%2s",card_name);
int val = 0;
if(card_name[0] == 'K'){
val = 10;
}else if (card_name[0] == 'Q'){
val = 10;
}else if(card_name[0] == 'J'){
val = 10;
}else if(card_name[0] == 'A'){
val = 11;
}else{
val = atoi(card_name);
}
printf("The card value is: %i\n",val);
return 0;
}
When I run it on eclipse, the line containing puts should be executed and then the user should enter in eclipse console and then the scanf line should execute.
But it doesn't happen that way, when I run it, it just expects the user to enter first on the eclipse console, then execute the puts line and finally the scanf line. I couldn't understand this behavior. Can someone help me on this ?
Depending on how the buffering of the output is configured, you might nee to be very explicit about when to actually make the output.
Usually a newline at the end of text is sufficient, i.e. there must be something special in your environment, I suspect the Eclipse console, but that is just guessing.
Especially when attempting to do prompt and reading within one line, it is wise to use a
/* ... */
puts("Enter the card_name: ");
fflush(stdout);
/* ... */
at exactly the point where you need the output you have already send.
In your case the buffering causes this sequence:
put the prompt in output buffer
environment is configured not to automatically output immediatly
(this is where I propose to flush in order to get output)
scanf is executed
user input
your program does not do anythign visible, though executing completly
put the final message into output
ending the program causes final output of anything buffered
I.e. the scanf is not executed before the first puts, it is only the visible effect which gets delayed.
This question already has answers here:
End of File (EOF) in C
(3 answers)
Closed 7 years ago.
When I use this code, I can type into the command line and get back what I typed:
main() {
int c;
while ((c = getchar()) != EOF) {
putchar(c);
}
}
Output:
~/code/c $ ./a.out
one
one
two
two
But when I use this code, it only works when I pipe in data, not when I type data into the command line:
main() {
int nc;
nc = 0;
while (getchar() != EOF) {
nc++;
}
printf("%d\n", nc);
}
When I pipe data in from a text file:
~/code/c $ ./a.out < practice.txt
14
When I try to input data from the command line:
~/code/c $ ./a.out
one
two
three
What's going on?
The while loop never exits since while (getchar() != EOF) is always true. After you're done with the input, press Cntrl+D for Linux or Ctrl+Z for Windows to indicate EOF.
Like everyone mention, you program doesn't stop. The while loop needs a condition to stop. you have to either use CTRL+Z (Win) or CTRL+D(Unix) to stop it, When you pipe it actually sends that character for you. Because EOF is END_OF_FILE. So when you hit the end of your file. the OS sends that character for you.
I've written a small C program where I wanted to display the numeric ASCII value that corresponds to certain key presses.
My code follows. The problem is, after running the program, it accepts input, but doesn't do anything else. It doesn't even reach the first printf statement. I can't figure out what the issue is - is there a problem with mixing the getchar() function with the printf() function in the same program?
#include <stdio.h>
int main() {
const int numKeys = 256;
int keys[numKeys];
int i;
for (i = 0; i < numKeys; i++) {
keys[i] = 0;
}
printf("\n Start pressing some keys!\n\n");
int c;
while ((c = getchar()) != EOF) {
printf(" CAPTURED: %d\n", c);
keys[c]++;
}
printf("\n\n ** RESULTS ** \n\n");
for (i = 0; i < numKeys; i++) {
if (keys[i] != 0) {
printf(" Key with value %d was called %d times.", i, keys[i]);
}
}
}
I should clarify that I have a Windows XP Pro machine, with Cygwin installed. I use Cygwin for my development space, so I wonder if there is something different when running this type of program in that environment.
I found the problem. I think you want to use
while ((c = getchar()) != EOF && c != '\n')
Instead if you want to have it print the results after the person hits enter/return.
problem 1 : getting to printf(" CAPTURED: %d\n", c); without having to press the Enter key
solution : is by using getche() in while loop.
problem 2 : getting to 'printf("\n\n ** RESULTS ** \n\n");' or essentially breaking while loop?
solution : you cannot. you will never get EOF as long as you read from keyboard.
workaround : close stdin or use a escape character other than EOF.
EDIT : workaround2 :
->use getchar() itself. but to print those entered char u need to press Enter key. now on windows ctrl+z gives EOF but this should be the **FIRST** input on the line after you press Enter key. well this is not a good solution.
if you want a "Press key display times pressed scenario. there is just no simple way(AFAIK)"
I believe that the first printf statement gets executed, but due to buffering is not displayed on the screen immediately. Use fflush(stdout) to send the contents of the buffer to the screen. Ie:
printf("\n Start pressing some keys!\n\n");
fflush(stdout);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
EndOfFile in C - EOF
I was trying examples from K&R. I am not able to understand why this code does not exit unless ctrl+c is pressed.
int main ( )
{
int c;
c = getchar();
while(c!=EOF)
{
putchar(c);
c=getchar();
}
}
Any help is appreciated. Thanks!
EDIT: Using Windows (Visual Studio 2010)
In Windows, you generate end of file from the standard input stream by pressing Ctrl+Z. Depending on the buffering behavior, you might also need to press Return.
EOF is End of File. If you read from 'keyboard', you should compare to End of Line symbol which is equal to press Return
int main ( )
{
int c;
c = getchar();
while(c!= '\n')
{
putchar(c);
c=getchar();
}
}
On Windows machine ctrl+c acts as delimiter of character scanning same as EOF
on this loop gets broken otherwise it will keep looking for characters