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

A

anurag

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..!!
 
C

Carlo Milanesi

anurag said:
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 <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));
 
V

Vaclav Haisman

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-----
 
J

Juha Nieminen

anurag said:
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.
 
A

anon

anurag said:
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 -
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top