Whats wrong?

This is a discussion on Whats wrong? within the C Programming forums, part of the General Programming Boards category; Whats wrong with this? I am a newbie plz help! what is wrong??? #include <stdio.h> main() { int c, nl, ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > 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,701
    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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(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, 09: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, 11:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21