Why are these programs not working?

  • Thread starter dE_logics
  • Start date
  • Tags
    Programs
In summary, the conversation discusses two programs provided in an e-book that are not working properly. The first program counts lines in input while the second program counts digits, white space, and other characters. However, when running the programs, the output is an empty line and the program does not proceed as expected, forming a never-ending loop. The solution is to enter the end-of-stream character, most likely ctrl-D, to terminate the input.
  • #1
dE_logics
742
0
I was wondering why are these programs, as given in an e-book not working --

Code:
 #include <stdio.h>

   /* count lines in input */
   main()
   {
       int c, nl;

       nl = 0;
       while ((c = getchar()) != EOF)
           if (c == '\n')
               ++nl;
       printf("%d\n", nl);
   }
Code:
 #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);
   }

The programs are getting compiled, but on running the binary, I just get an empty line on which I can type...

Expected is that after entering a new line, the program should proceed, but nothing like that is happening...after entering a new line...I just enter a new line and can type in that too. :confused:

This forms a never ending loop.

I'm using gcc to compile the program.
 
Technology news on Phys.org
  • #2
Enter the end-of-stream character to terminate the input. Probably ctrl-D.
 
  • #3
Thanks.
 

1. Why is my code producing errors?

There could be various reasons for your code producing errors. Some common reasons include syntax errors, logical errors, or incorrect data types. It is important to carefully review your code and debug it to identify the specific error and fix it.

2. Why is my program not giving the expected output?

There could be several reasons for your program not giving the expected output. It could be due to incorrect input data, incorrect logic, or an error in your code. It is important to carefully check your code and make sure it is working as intended.

3. Why is my program running slowly?

The speed of a program depends on various factors such as the complexity of the code, the efficiency of the algorithms used, and the hardware specifications of the computer running the program. If your program is running slowly, it could be due to inefficient code or a lack of resources on your computer.

4. Why is my program crashing?

Program crashes can occur due to a variety of reasons such as memory leaks, infinite loops, or trying to access invalid memory locations. It is important to carefully analyze your code and identify the root cause of the crash in order to fix it.

5. Why is my program not working on a different computer?

Some programs may not work on different computers due to differences in hardware specifications, operating systems, or installed libraries. It is important to test your program on different computers to identify any compatibility issues and make necessary changes to ensure it works on all systems.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
735
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
2
Views
932
  • Programming and Computer Science
Replies
6
Views
2K
Back
Top