Thread: Whats wrong?

  1. #1
    Unregistered
    Guest

    Whats wrong?

    Whats wrong with this?
    I am a newbie plz help!
    what is wrong???

    #include <stdio.h>
    main()
    {
    int c, nl, np, nq;
    printf("Press ENTER and then CTRL+C to finish input\n");
    nl = 0;
    np = 0;
    nq = 0;
    while ((c = getchar()) != EOF)
    if(c == '\n')
    ++nl;
    if(c == '\t')
    ++np;
    if(c == ' ')
    ++nq;
    printf("FINAL REPORT:\n");
    printf("Blanks: %d\n", nq);
    printf("Lines: %d\n", nl);
    printf("Tabs: %d\n", np);
    }
    It only sez the correct number of lines and everything else is incorrwctly zero.
    If i do this:
    #include <stdio.h>
    main()
    {
    int c, nl, np, nq;
    printf("Press ENTER and then CTRL+C to finish input\n");
    nl = 0;
    np = 0;
    nq = 0;
    while ((c = getchar()) != EOF)
    if(c == '\t')
    ++np;
    if(c == '\n')
    ++nl;
    if(c == ' ')
    ++nq;
    printf("FINAL REPORT:\n");
    printf("Blanks: %d\n", nq);
    printf("Lines: %d\n", nl);
    printf("Tabs: %d\n", np);
    }
    Thx 4 Help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try putting {} around all the statements you want in your while loop.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    I can't quite figure out what your program is trying to do
    but CTRL+C is a dangerous thing to ask the user to type.

    When the console server detects a CTRL+C it terminates the
    execution of the current program.

    Some debuggers use a "signal" to catch the CTRL+C and continue
    execution but when not in a debugger it's like pulling the plug.

    Post a more complete explaination of what you want to do and I'll
    try to help.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but CTRL+C is a dangerous thing to ask the user to type
    Good point - it should be CTRL+D (Unix/Linux) or CTRL+Z(DOS) to generate an EOF on the input stream

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Not to mention that it's a bad habit. If you are running a program that has just alloc'd 1MB and abort it - guess what?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Unregistered
    Guest
    but if i dont make them press CTRL+C then once the input is over the programm will just skip a line once enter is pressed and wait. if CTRL+C is pressed the output is produced. ill try it the "{"s now but pepper ann is on...

  7. #7
    Unregistered
    Guest
    yeah it work now. thx guyz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code (HELP)
    By n00by in forum C Programming
    Replies: 20
    Last Post: 08-11-2004, 03:15 PM
  2. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  3. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  4. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM