Xcode ⌃d EOF Not Working After Paste - c

Behavior is very inconsistent-- very frustrating.
The problem must lie in pasting some input text into Xcode's Debug Area.
Here's how to replicate the problem:
#include <stdio.h>
/* count digits, white space, others */
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);
}
Run this C code in Xcode
Paste in some Lorem Ipsum into Xcode Debugger Area
⌃d will not send EOF signal
Doesn't work even if you send Ctrl-d on its own line.
Note** Works fine (pasting in lipsum and signaling EOF with ⌃d) when you run the compiled executable from Terminal. Also, Ctrl-d doesn't need to be on its own line (just keystroked twice in a row). Still works regardless.

Related

Problem with the K&R - Learning C - Arrays - ASCII

I'm currently learning C from The C Programming Language by Brian W Kernighan and Dennis M. Ritchie. I've gotten to the arrays section, and ran into a problem. When arrays are discussed in this book, they use this program:
`#include <stdio.h>
/* This program count digits, white space, others */
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);}
}`
My question is: why the -'0' in ++ndigit[c-'0'];? What does it mean? I noticed when I get rid of it, the program will not function properly. Edit: I heard it has something to do with ASCII. I have no idea what that is.
If c is between '0' and '9', due to c standard '0' to '9' are encoded consecutively, c-'0' will give out the numerical result of c. Say c is '6', then c-'0'==6.

C program stuck in infinite-loop-like nautre but without printing any variables

I'm trying K&R exercise 1-29 and honestly I'm stumped by what my program is doing. Upon inputting a small text file with gcc, the program just runs forever as if there's an infinite loop. Reading through my loops again I can't see any obvious mistakes, so I tried to print different variables to see what's happening, but nothing's printing to the command line - even the variable i from the very first for loop, not even once. Here's my code:
#include <stdio.h>
#define tabStop 10
#define maxInput 1000
int main(){
int i, c, b;
int m, t = 0;
int index, index2;
char input[maxInput];
int nonBlank;
for (i = 0; i<maxInput-1 && (c=getchar()) != EOF; ++i){
if (c != ' '){
input[i] = c;
}
else {
index = i; /*stores location in character array at start of blanks*/
for (b = 0; c == ' '; ++b, ++i); /*counts the number of blanks (b) until the next non-blank character*/
nonBlank = input[i]; /*saves the first seen non-blank character to be re-added later (as getchar() will have taken it in from input and will now stay at that point of the input)*/
index2 = i; /*stores location in character array at end of blanks*/
if (b >= tabStop){ /*if the tab space fits inside the number of blanks, otherwise there is nothing to be done*/
while (t < b){
t += tabStop;
++m;
}
for (int x = 0, i = index; i != index2 && x <= m; ++i, ++x){ /*loops over the number of tabs to be added starting from the first blank in the array found up until the first non-blank*/
input[i] = '\t';
}
while (i != index2){ /*if i did not reach index2 before x surpassed m, there exist remaining spaces to be filled with blanks*/
input[i] = ' ';
++i;
}
}
input[i] = nonBlank; /*puts the first seen non-blank character into place, as getchar() has already covered it and so it wouldn't otherwise be added like other non-blanks*/
}
}
input[i] = '\0';
printf("%s", input);
return 0;
}
Here's the inputted text file:
hello there world

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

k and r line counting 1.5.3 not working?

I'm having trouble with k&r 1.5.3. Obviously I'm a complete beginner. Below is the code exactly from the book and exactly as I typed it. It compiled fine and runs. It returns characters but just never prints the line count. I'm using ssh into a Ubuntu machine. Can the key on my wife's mac not be interpreted as '\n'?
#include <stdio.h>
/*count lines in input*/
main()
{
int c, n1;
n1 = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++n1;
printf("%d\n", n1);
}
Correct. Mac uses \r as line ending: http://en.wikipedia.org/wiki/Newline
Update your code like this:
#include <stdio.h>
/*count lines in input*/
main()
{
int c, n1;
n1 = 0;
while ((c = getchar()) != EOF)
if (c == '\r') /* use \r for Macs */
++n1;
printf("%d\n", n1);
}
However
When I try to do the same, I have to Ctrl-D to enter an EOF and trigger the program to print the line count.

program that counts blanks, tabs, and newlines

I'm reading The C Programming Language. Here is a question which is saying Write a program to count blanks, tabs, and newlines. Now I can use \n for newlines and \t for tabs, but I am hearing first time about blanks! What it really mean by blanks? For the newlines and tabs, I have compiled following program:
#include <stdio.h>
/* program to count blanks, tabs, and newlines */
main (){
long blanks, tabs, newlines, input;
blanks = 0;
tabs = 0;
newlines = 0;
input = 0;
while ((input = getchar()) != EOF)
if (input == '\n')
++newlines;
else if (input == '\t')
++tabs;
printf("Total newlines: %ld\nTotal Tabs: %ld", newlines, tabs);
}
blanks = spaces (' ')
Though your code is working I strongly suggest adding { } for the body of the while loop.
A blank is simply a space, most of the time. You should probably look into the isblank() function to help with classification.
I'm sure they mean the space character ' '.
See here for the ASCII codes:
http://www.asciitable.com/
also 0x13 is carriage return, may want to look for that? Newlines are not actually that simple depending on how the file is formatted:
http://en.wikipedia.org/wiki/Newline
And like others have said, you may want to consider using functions from
http://www.cplusplus.com/reference/clibrary/cctype/
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Type and Press Enter. CTRL-Z for EOF:\n");
int c;
int b = 0;
int t = 0;
int nl = 0;
while((c = getchar())!=EOF){
putchar(c);
if(c=='\t')
++t;
if(c==' ')
++b;
if(c=='\n')
++nl
}
printf("\n%d and %d\n",b,t,nl);
return 0;
}
You've added a else if which is not required here since we need to know all the 3 values.
please find the code below this will get solution to your complete question.
#include <stdio.h>
/* Count blank, tabs, and new lines */
main ()
{
long c, b, t, l; /* c for characters, b for blanks, t for tabs, l for lines */
b = 0;
t = 0;
l = 0;
while ( ( c = getchar() ) != EOF )
{
if ( c == '\n')
++l;
if ( c == '\t')
++t;
if ( c == ' ')
++b;
}
printf ("\n No of blanks : %d \n", b);
printf ("\n No of tabs : %d \n", t);
printf ("\n No lines in input: %d \n", l);
}
I think blanks are new line followed by new line
#include<stdio.h>
int main()
{
int c,nl,nb,nt;
nl = 0;
nb = 0;
nt = 0;
int flag =1;
while((c = getchar()) != EOF){
if(c == '\n')
{
++nl;
if(flag)
++nb;
flag = 1;
}
else
flag = 0;
if(c == '\t')
++nt;
}
printf("lines : %d, tabs: %d, blanks: %d", nl, nt, nb);
return 0;
}
This answer -- benefited from other users' contributions -- uses only the material taught up to Exercise 1-8. When Ctrl+z is pressed (I used Windows DOS), the program jumps to printf statements, prints answers, then exits to DOS prompt.
#include <stdio.h>
/* Exercise 1-8. Write a program to count blanks, tabs, and newlines. */
int main()
{
int bl, tb, nl, c;
bl = 0;
tb = 0;
nl = 0;
while ((c = getchar()) != EOF) {
if (c == ' ')
++bl;
if (c == '\t');
++tb;
if (c == '\n');
++nl;
}
printf("Total blanks: %d\n", bl);
printf("Total tabs: %d\n", tb);
printf("Total newlines: %d\n", nl);
}
When you hit Enter key 5 times, press Spacebar 2 times, hit Tab key 4 times, then press Ctrl+z, you will get
Total blanks: 2
Total tabs: 4
Total newlines: 5
then DOS prompt.
the solution I think is :
#include <stdio.h>
main() {
int c, nl, nt, nb;
nl = 0;
nt = 0;
nb = 0;
while((c = getchar()) != EOF){
if (c == '\n')
++nl;
if (c == '\t')
++nt;
if (c == ' ')
++nb;
}
printf("Blanks: %d\nNewlines: %d\nTabs: %d\n", nl, nt, nb); }

Resources