Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in this code , if i want to print the sentence entered by the user then how is it possible?

C++
#include <iostream.h>
#include <conio.h>
#include <fstream.h>

int main()
{
    char p[80];
    cout << "Enter text :";
    cin >> p;
    
    getch();
    return 0;
}
Posted
Updated 19-Aug-11 10:45am
v4

C++
cout<<p;
 
Share this answer
 
Comments
Ashutosh_g 19-Aug-11 2:47am    
It will only print a word of sentence, If i enter "Hello How are you" ,it will print only "Hello"
enhzflep 19-Aug-11 2:57am    
Yes, but no.

It will only _input_ one word. Problm is with use of cin, not with use of cout.

char *buffer = "Hello, how are you";
cout << buffer;

Will indeed print "Hello, how are you"

I don't use cin/cout myself. Perhaps you'll find that there is a flag that ytou set for cin that will cause it to continue taking input until a line-end is found, rather than the first instance of whitespace.
C++
const int bufLen = 1024;
char buffer[bufLen];
cin.getline(buffer, bufLen-1);
cout << buffer << endl;
 
Share this answer
 
Comments
Philippe Mori 19-Aug-11 16:43pm    
I would recommand you to uses new C++ headers and works with string instead.

getline (with string)
Like this,
cout<<p<<endl;
 
Share this answer
 
Comments
Ashutosh_g 19-Aug-11 2:46am    
It will only print a word of sentence, If i enter "Hello How are you" ,it will print only "Hello"
if you want read a line you can try like cin.getline(p, 80);check documentation of getline.
Philippe Mori 19-Aug-11 16:48pm    
If real code, a constant should also be used (or the size computed using sizeof when applicable) so that the actual buffer size is defined at a single place.
C++
# include <iostream>
#include <conio.h>
#include <fstream>

using namespace std;
int main()
{
    
char p[80];
    cout << "Enter text :";
    cin >> p;
   cout << "the ented text is:"<< p << endl;
    
    return 0;
}

its the same code you have used.
watever u did is getting a input in variable p.
If u want tat in the console, just print the same variable thats it!

Thanks!
Arun P.
 
Share this answer
 
v5
Comments
Richard MacCutchan 22-Aug-11 8:29am    
Unfortunately this will not work for any input that contains more than one word; try it.
yes you are right!


if you want ur question to be answered, try this!


C++
#include <conio.h>
#include <fstream>
using namespace std;
int main()
{
    char p[80];
    char ch = 'y';    
while (ch == 'y' || ch == 'Y')
    {
        cout << "Enter text :";
        cin >> p;
        cout << "the ented text is" <<  p ;
        cout << "do you wish to continue y/n" << endl;
        ch = getch();
    }
    return 0;
}</fstream></conio.h>
 
Share this answer
 
v2
Comments
Richard MacCutchan 22-Aug-11 9:07am    
This is still wrong, why not test it properly before posting the code? The user wishes to input some text (which may include spaces) and print it out as entered.
Arun Parthasarathy 23-Aug-11 0:28am    
fine. let the user reply.
rather than u testing and giving ur comments here! cant you give a proper answer if u know?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900