Input does not exit in c, thus output does not work - c

I am just learning c, and I am using Linux terminal. I have written the following simple code, but when I type the inputs, the file does not exit and thus does not count the number of characters. Can anyone help me with it? I have tried other input codes as well. It's the same with all my input related codes. What am I doing wrong? Please help.
main()
{
/* count characters in input */
printf("Type some characters and the program will count the number of characters: ");
int c = getchar();
while(c!=EOF && c!= '\n')
++c;
printf("Number of characters typed: %1d\n", c);
}

So to take notice of the helpful comments;
#include <stdio.h>
int main(){
int c;
int count = 0;
while((c=getchar()) != '\n' && c != EOF)
count++;
printf("%d\n", count);
};
This code works as expected.

Related

Problems with a character counting program in C on online compilers

I'm a newcomer to programming so please forgive me for my stupid questions. I ran the following code on repl.it and JDoodle respectively.
#include<stdio.h>
int main()
{
int nc;
nc = 0;
while(getchar() != EOF)
nc = nc + 1;
printf("%d\n", nc);
}
On JDoodle, the results are always x+1 where x is the number I expected. For example, if I input 123123, the result is 7. Besides, if I leave the input empty, JDoodle tells me that Your Program may have a endless loop. Why these happen?
On repl.it, after I type the input on the right side of the screen and press Enter, there is no response. However the input copying program
int c;
c = getchar();
while(c != EOF){
putchar(c);
c = getchar();
}
works well on repl.it. Why the character counting program cannot work on repl.it? I will appreciate any help with these situations.
the following proposed code:
corrects the problem discussed in the comments to the question.
The differences between the two web pages is of no consequence to the problem seen by the OP.
And now the proposed code:
#include <stdio.h>
int main( void )
{
int nc = 0;
int ch; // the character input from stdin
while( (ch = getchar()) != EOF && '\n' != ch)
nc = nc + 1;
printf("%d\n", nc);
}

Code prints out correctly with a leading space.

I'm new to C programming and I'm having probably a simple issue.
I've looked all over the site looking for an answer to this but my no luck.
So my program runs fine if at the end of the input file there is a space. For example running the program from command line in unix
./program-name < file.txt
if the file.txt is
//with a space whith | | being a space.
The Cat Ran.| |
I get the correct output 1 1 1
if it is
The Cat Ran.(With no space I get 1 1)
//I have to use C for this program.
#include <stdio.h>
int main(void){
int i=0;
char c;
int NOV=0;
while( (c=getchar())!=EOF && c !='\n' && c !=10 ){
if( c=='a'||c=='e'||c=='i'||c=='o'||c=='u' ){
NOV++;
}
if(c==' '){
printf("%d ",NOV);
NOV=0;
}
}
printf("\n");
return 0;
}
Any Help is appreciated.
The trouble is that when you get EOF without a space, you exit the loop, and nothing after the loop prints the number of vowels.
Add if (NOV != 0) printf("%d", NOV); after the loop, perhaps.
Separately, your use of 10 is odd; '\n' is control-J or 10. You might be thinking of control-M, aka carriage return or '\r'.
Also, as Blue Pixy pointed out in a comment, you should always use int c; to receive the return from getchar() or getc() of fgetc(). The functions return an int, which may be any value that fits in an unsigned char or EOF, a negative value. You run into problems if you use char c instead.
Your program should read like this (a lot of useless clutter has been removed) :
int main(void)
{
int c;
int nov = 0;
while( (c = getchar()) != EOF && c != '\n')
{
if( c=='a'||c=='e'||c=='i'||c=='o'||c=='u' )
nov++;
}
printf("%d\n", nov) ;
return 0;
}

Counting input characters in C

I have just started learning C, been reading a C textbook by Keringhan and Ritchie. There was this example in the textbook, counting characters from user input. Here's the code:
#include <stdio.h>
main()
{
long nc;
nc = 0;
while(getchar() != EOF) {
if (getchar() != 'q')
++nc;
else
break;
}
printf("%ld\n", nc);
}
The problem is, when I execute the code, if I input only one character per line, when I input "q" to break, it doesn't do so. I have to type some word per line, only after that it will break the loop. Also, it only counts the half of the characters of the word. I.e. if I input
a
b
russia
it will only print '5' as final result.
Could you please explain to me why is this happening?
This works, but only when you finish off with an Enter. So, this will count the characters until the first "q" appears. That is just how getchar() and getc(stdin) work.
#include <stdio.h>
int main() {
char c = 0;
long count = 0;
short int count_linebreak = 1; // or 0
while((c = getchar()) != EOF) {
if(c != 'q' && (count_linebreak || (!count_linebreak && c != '\n'))) {
++count;
}else if(c == 'q') {
printf("Quit\n");
break;
}
}
printf("Count: %ld\n",count);
return 0;
}
A StackOverflow question about reading stdin before enter
C read stdin buffer before it is submit

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.

Abnormal output using character data type

I wrote a code for practice in C in Turbo c 3.1. My objective was to get the character output. My code is as follows:
#include<stdio.h>
#include<conio.h>
void main(){
char a[10],b;
int i;
clrscr();
for(i=0;i<10;i++)
a[i]='$';
for(i=0;i<10;i++){
b=getchar();
a[i]=b;
}
for(i=0;i<10;i++)
printf("%c",a[i]);
getch();
}
Here, I had to get 10 characters with what I had input but after execution I got only 5 characters. I couldn't get the problem lying here.
Can you please help me?
Thanks in advance.
When you do getchar() , it reads a character.
Therefore when you type something like
a, and then enter,
what you type is actually 2 characters which area and '\n'.
Therefore, you get only 5 characters and 5 '\n' in your output.
Try this
#include<stdio.h>
#include<conio.h>
void main(){
char a[10],b;
int i;
clrscr();
for(i=0;i<10;i++)
a[i]='$';
for(i=0;i<10;i++){
b=getchar();
if(b=='\n'){i--;continue;}
a[i]=b;
}
for(i=0;i<10;i++)
printf("%c",a[i]);
getch();
}
You could do an easy scanf to solve your problem
scanf("%c",&a[i]);
But still, to understand what getchar() does, Every time you call it, it reads the next character of input and returns it to you irrespective of the character. In your case, you type a \n character after you input the number.So it stores first of your numbers with \n after each of them.
You could do this check if you are addicted to getchar()
if(c=getchar()) == '\n')
c=getchar();
so how to make the correction, den?
How about:
int ch;
while (i < 10 && (ch = getchar()) != EOF)
if (ch != '\n')
a[i++] = ch;
Simpler solution maybe to use:
scanf("%c",&a[i]);
instead of :
b = getchar();
a[i] = b;
Probably, you can use getch() or getche() instead of getchar() to read a character.

Resources