cout.write....... great problem!!!

Discussion in 'C++' started by anurag, Dec 14, 2008.

  1. anurag

    anurag Guest

    When I use cout.write in my program, I don't get any output. But when
    I use puts(), all is well with the program. Why? Btw, I program on
    Turbo C++. The original program was -

    #include <iostream.h>
    #include <conio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdio.h>

    void displayName(char[]);

    void main()
    {
    char name[40];

    cout << "Enter a name (max 40 characters): " ;
    cin.getline (name, 40);

    displayName(name);

    getch();

    }

    void displayName(char a[])
    {
    char abbr[40];
    int i = 0, j = 0, nameLen, flag = 0, n = 0;

    nameLen = strlen(a);

    for (; i < nameLen ; i++)
    if (a == ' ')
    flag++;

    i = 0;

    if (flag)
    {
    for (; n < flag; n++)
    {
    if (a != ' ')
    {

    abbr[j] = toupper(a);
    abbr[j+1] = ' ';
    j += 2;
    }

    for (; a != ' '; i++);

    i++;
    }

    if (a != '\0')
    {
    abbr[j] = toupper(a);
    i++;
    j++;
    }

    while (a != '\0')
    {
    abbr[j] = tolower(a);
    i++;
    j++;
    }

    abbr[j] = '\0';

    //cout << "The short name is " ;
    puts(abbr); // HERE, cout.write JUST DOESN'T GIVE DAMN
    OUTPUT. :(

    }

    else
    {
    //cout << "The Short name is same as original, i.e. ";
    puts(a); // SAME HERE!!
    }

    }



    PLEASE HELP. THE PROGRAM HAS GIVEN ME NIGHTMARES..!!
     
    anurag, Dec 14, 2008
    #1
    1. Advertising

  2. anurag wrote:
    > When I use cout.write in my program, I don't get any output. But when
    > I use puts(), all is well with the program. Why? Btw, I program on
    > Turbo C++. The original program was -
    >
    > #include <iostream.h>

    Obsolete; no more standard. Use:
    #include <iostream>
    using namespace std;

    > #include <conio.h>

    Not standard.

    > //cout << "The short name is " ;
    > puts(abbr); // HERE, cout.write JUST DOESN'T GIVE DAMN
    > OUTPUT. :(

    It's weird you posted the code that works and not the code that doesn't
    work!
    Try:
    cout.write(abbr, strlen(abbr));

    --
    Carlo Milanesi
    http://digilander.libero.it/carlmila
     
    Carlo Milanesi, Dec 14, 2008
    #2
    1. Advertising

  3. anurag wrote, On 14.12.2008 11:32:
    > When I use cout.write in my program, I don't get any output. But when
    > I use puts(), all is well with the program. Why? Btw, I program on
    > Turbo C++. The original program was -
    >[...]

    The std::cout stream is line buffered. Use either std::cout << std::endl, if
    you want to end the line with new-line character, or std::flush to make sure
    it prints it.

    --
    VH


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (MingW32)

    iFYEAREIAAYFAklFCzsACgkQhQBMvHf/WHl8xwDgzc+gzpvHuweebznLuxzlnSL+
    KG+dRVu2OLxIBgDfd5EtSOHjZmRw9YKU1q3EY7l4iog5/vtMItgIcg==
    =CJyQ
    -----END PGP SIGNATURE-----
     
    Vaclav Haisman, Dec 14, 2008
    #3
  4. anurag wrote:
    > When I use cout.write in my program, I don't get any output.


    cout is buffered by default. Try calling std::cout << std::flush when
    you want to make sure it outputs what you wrote.
     
    Juha Nieminen, Dec 14, 2008
    #4
  5. anurag

    anon Guest

    anurag wrote:
    > When I use cout.write in my program, I don't get any output. But when
    > I use puts(), all is well with the program. Why? Btw, I program on
    > Turbo C++. The original program was -
    >

    <cut>
    >
    > PLEASE HELP. THE PROGRAM HAS GIVEN ME NIGHTMARES..!!



    Please tell - which compiler are you using that doesn't give you these
    errors:

    [xxx@chestnut data_create]$ g++ r2.cpp
    r2.cpp:1:22: error: iostream.h: No such file or directory
    r2.cpp:2:19: error: conio.h: No such file or directory
    r2.cpp:9: error: ‘::main’ must return ‘int’
    r2.cpp: In function ‘int main()’:
    r2.cpp:13: error: ‘cout’ was not declared in this scope
    r2.cpp:14: error: ‘cin’ was not declared in this scope
    r2.cpp:18: error: ‘getch’ was not declared in this scope
     
    anon, Dec 15, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,656
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,417
    Leor Zolman
    Jun 2, 2004
  3. Replies:
    4
    Views:
    321
    Howard
    Sep 15, 2005
  4. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    485
  5. Eric I.
    Replies:
    0
    Views:
    269
    Eric I.
    Oct 5, 2008
Loading...

Share This Page