Help needed

I am writing a simple program to accept a character string. Suppose, the first and last name of the user in an character array and display a welcome message with the full name. But problem arise in the white space. I don't know how to store a multi word sentence in an array. Please help. The following code snippet dose not work.
#include<iostream.h>
int main()
{
char name[50];
cout<<''Enter your full name:'';
cin>>name;
cout<<endl<<''Hello ''<<name<< ''welcome'';
return 0;
}
Last edited on
Use cin.getline( name, sizeof( name ) ); instead of cin>> name;
to insert strings uses

 
getline(cin,name);
@ar2007

to insert strings uses

getline(cin,name);


There is no such function getline( cin, name ) where name is a character array.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main ()
{
    char name[50];
    std::cout<<"Enter your full name: " ;

    // http://www.cplusplus.com/reference/istream/istream/getline/
    std::cin.getline( name, sizeof(name) ) ;

    std::cout << "\nHello " << name << " welcome\n" ;
}


Consider using std::string and std::getline()
See the example here: http://www.cplusplus.com/reference/string/string/getline/
agree sorry.
@ar2007

agree sorry.


To be more precisely I need to say that there is no function std::getline( std::basic_stream &, std::string && )
Thnx a lot guys, specially to @vlad your code works without a single error.
I believe there is a different iostream.h which is for C which is incompatible with C++. You also need to use std::<function> such as
std::cout // makes your programs more compact.


You may use
using namespace std;

but realize that this will make your programs require much more resources. If you are making small programs this doesn't seem like a big deal but think if you had to compile a giant program. Best to make good habits.

There is probably a more factual/precise discussion on this, please don't take my word for it, find out more at your local library.
Topic archived. No new replies allowed.