Jump to content

program from book doesn't work

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 175 posts
#include <stdio.h>
#include <stdlib.h>

main()
{
double ne;

for ( ne=0; getchar() != EOF; ++ne)
;
printf("%.0f\n", ne);
system("pause");
}

Hi, accorging to the book about C programming this code should print the number of characters in program's input, but it does nothing, can anybody help?

#2
alienkinetics

alienkinetics

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
It works, just not with console input. You need to pipe a file to the app: ie: if you have compiled to "count.exe" then this will return the size of test.txt:

count.exe < test.txt

Although, its a silly way of getting the size of a file.

PS: remove the system() command. it isn't required and somewhat non-portable.

#3
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts

thatsme said:

#include <stdio.h>
#include <stdlib.h>

main()
{
double ne;

for ( ne=0; getchar() != EOF; ++ne)
;
printf("%.0f\n", ne);
system("pause");
}

Hi, accorging to the book about C programming this code should print the number of characters in program's input, but it does nothing, can anybody help?

What book teachs main() (with no type) and system() ?

#4
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts

outsid3r said:

What book teachs main() (with no type) and system() ?

I too, was taught main with no type when I was learning C. I was told it was only neccesary for C++
Posted Image

#5
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
*sigh* I wish it weren't so. The unadorned main is pre-ANSI-C. That was 1989. :(

#6
alienkinetics

alienkinetics

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
Must be one very old book covered in the stains of a thousand hippie coders
Buzz PHP Class Library - Web Components Made Easy!
http://www.buzzphp.com/

#7
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
Most of the new compilers wont work if you put
" main() { printf("Hello world"); } "
You would require to put int main() or void main().

#8
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
Oh and avoid using system, Because it will pause the program, go to the command prompt, search for that command then input it, use getchar() instead