K&R code not working in VS2012 [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is from K&R, it's supposed to count digits, white space, etc. I compile fine but when I run I just get a blank screen and all I can do is type inputs. Is this not working because the C language is outdated?
#include <stdio.h>
main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}

Well to put it simply. This loop will end once you type ctrl + d. If you want for it to end when you press enter, just modify it a bit: change
while ((c = getchar()) != EOF)
to
while ((c = getchar()) != '\n')

The program is written to get input until an end-of-file character. Since you're using DOS, enter Ctrl+Z to terminate input (you have to hit "enter" after the Ctrl+Z because keyboard input will be line-buffered).
On edit: Ctrl+Z is the end-of-file (EOF) character in the Windows/DOS world. In UNIX/Linux environments, it is (usually) Ctrl+D.

Related

Problem using printf() to output the contents of an array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have started working through "The C programming language, 2nd Ed." & already I'm running into problems. This code is from Chapter 1 on arrays,
#include <stdio.h>
int main(void){
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i){
ndigit[i] = 0;
}
while ((c = getchar()) != EOF){
if (c >= '0' && c <= '9')
++ndigit[c - '0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
}
printf("digits =");
for (i = 0; i < 10; ++i){
printf(" %d", ndigit[i]);
}
printf(", white space = %d, other = %d\n", nwhite, nother);
}
It compiles (gcc via Visual Studio Code, Win 10, x64) no errors or warnings. I input the following sequence via the keyboard,
12333 45666 789
and the output I get,
digits = 0^C
Now I was expecting,
digits = 0 1 1 3 1 1 3 1 1 1, white space = 2, other = 0
I then rewrote the for loop that is supposed to print the array to print out i
for (i = 0; i < 10; ++i){
printf(" %d", i);
}
Then got,
digits = 0^C
What's going on here? First off, why isnt the array being printed, secondly why is the final printf() statement not being executed?
EDIT:: Thanks to the comments, I didn't understand the EOF character properly & how the input stream works. If I exit with ctrl+z and then enter the program executes correctly!

Exercise 1-8. Write a program to count blanks, tabs, and newlines [duplicate]

This question already has answers here:
How to enter the value of EOF in the terminal
(4 answers)
Closed 2 years ago.
it's the K and R c programming exercise, I don't know why my program doesnt work, could anyone help, please and thank you.
When I run the program and type a line of words or two and hit 'Enter', it doesnt show anything, it just jumps to the next line and thats it
#include <stdio.h>
int main()
{
int c, line, tab, blank;
line = 0;
tab = 0;
blank = 0;
while ( (c = getchar()) != EOF)
{
if (c == '\n')
++line;
if (c == '\t')
++tab;
if (c == ' ')
++blank;
}
printf("Lines: %d\n Tabs: %d\n Blanks: %d\n", line, tab, blank);
return 0;
}
When do you think will EOF occur, if you are reading from stdin? You need either signal handling or a terminating character.

C programming count number of occurrences? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am working though the C programming book. I have a question in the arrays section (1.6) that says "Let us write a program to count the number of occurrences of each digit, of white space characters (blank, tab, newline) and all other characters.
#include <stdio.h>
/* count digits, white space, others */
int main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; i++)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-0];
else if (c == ' ' || c == '\n' || c == '\t')
nwhite++;
else
nother++;
printf("digits =");
for (i = 0; i < 10; i++);
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}
This is the compiling code. If I type 23, enter, 23, enter then ctrl+d it returns "digits = 1044509184, white space = 2, other = 0". Why? What is digits doing?
if (c >= '0' && c <= '9')
++ndigit[c-0];
This block of code is problematic as it is accessing the array out of bounds (and will cause undefined behaviours).
It should be
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
Notice the difference between 0 and '0'. The latter piece of code works as it is guaranteed that number characters are encoded consecutively.

Word Count Program in C is not counting correctly [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Like the title says im writing a Word Count clone Program in C, but is not counting correctly in one of the cases.
I have .txt file and the program works fine but if i use the following command
linux>./wordcountclone < file.txt
it only counts half the words.
Am i doing anything wrong?
if i use for example
linux>./wordcountclone file.txt
or
linux>./wordcountclone -l -d file.txt
works fine.
Here goes the function im using
void fileO(FILE * name, int car, int word, int lin, int dig){
int c , nl, np, nc, nd, state;
state = OUT;
nl = nc = nd = 0;
np = -1;
c = fgetc(name);
while (c != EOF){
c = fgetc(name);
nc++;
if(nc > INT_MAX)
return;
if (c == '\n')
nl++;
if (c > 47 && c < 58)
nd++;
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
if (state == OUT) {
state = IN;
np++;
}
}
if(lin !=0)
printf("lines %d ", nl);
if(word != 0)
printf("words %d ", np);
if(dig !=0)
printf("digits %d ", nd);
if(car != 0)
printf("chars %d ", nc);
printf("\n");
}
and the .txt file that i am using has the following text
the cat sat on the mat
the dog jumped over the moon
At least 2 problems here:
You always skip the first char doing c = fgetc(name); twice in a row - one before while and then inside the loop.
The state logic is incorrect - if you run this on a file containing only whitespace it'll still count words.
Also, a couple of suggestions:
instead of if (c > 47 && c < 58) you could do if ('0' <= c && c <= '9') or use isdigit()
use isspace() to check for whitespace

Nothing showing up C beginner

I'm following along with a book on C to learn it and I am writing all the code in the book to follow along, the latest one to do with arrays is supposed to say how many white spaces, tabs etc. There are but when I execute it, nothing shows up, it's just blank, as in I can type something then press enter and nothing happens, is it supposed to tell me how many of each thing there is?
I am too new to understand if this program is actually supposed to output anything so I thought I would post it on here and get an opinion, it compiles and runs fine, no errors, but the book sort of refers to it outputting stuff, but nothing happens when I'm running it and typing stuff in, can just keep typing stuff forever.
Here is the code
#include <stdio.h>
int main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[1]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}
The application takes in input some values and then counts the number of digits (0-9) and the white spaces. The key combination to interrupt the cycle is not ENTER but EOF which in Linux is CRTL-D and in WINDOWS is CTRL-Z.
Then, in you application there is a bug :
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[1]);
In order to show the number of digits this should be :
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
Unfortunately, getting interactive input is quite problematic when using scanf(), getchar(), fgets(), etc. That's why most people usually write their own custom functions, often getting a whole line from stdin and then parsing it according to their needs. However, if you want to use ENTER to stop the cycle you can modify the code as follows, but you will lose the possibility to count the number of new lines in the input.
#include <stdio.h>
int main(void)
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar()) != '\n')
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
return 0;
}
This should work as you expected. However, you should consider to write a better input function, there are several interesting solution online.
EDIT
The main() must return int. Not void, not bool, not float. int. Just int, nothing but int, only int. Some compilers accept void main(), but that is non-standard and shouldn't be used.
Check some examples here : http://www.parashift.com/c++-faq-lite/main-returns-int.html
You can provide a EOF using following
WINDOWS:
Press F6 then ENTER
or
Ctrl+Z
LINUX:
Ctrl+D
Change
printf(" %d", ndigit[1]);
to
printf(" %d", ndigit[i]);
Press ctrl+d to give EOF after entering value
Input:
2 4 6 8 //3 white space + 1 new line = 4 white space
[ctrl + d]
Output:
digits = 0 0 1 0 1 0 1 0 1 0, white space = 4, other =0

Resources