[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

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

anurag

12/14/2008 10:33:00 AM

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[i] == ' ')
flag++;

i = 0;

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

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

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

i++;
}

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

while (a[i] != '\0')
{
abbr[j] = tolower(a[i]);
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..!!
4 Answers

Carlo Milanesi

12/14/2008 11:27:00 AM

0

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.i...

Vaclav Haisman

12/14/2008 1:34:00 PM

0

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

Juha Nieminen

12/14/2008 1:44:00 PM

0

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.

anon

12/15/2008 10:03:00 AM

0

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