This is my school's assignments and I'm a beginner in C++.. I'm trying to write a program which prompts strings input from user. Say if user enters "My name is blabla", the program should print the exact same thing without ignoring the characters after the whitespace.

I tried using a buffer loop but I couldn't think of a way of how to terminate it after input has done. i tried, adding cin.eof() at the end of loop doesn't have any effect.(In fact, adding cin.eof() is not a good solution because I still want to store characters as long as the user hits space, not enter)
I want the loop to terminate after the user hits enter but not space, but since both are whitespace so I'm stuck.

Here's my code:

using namespace std;
#include <iostream>
#include <string.h>

int handle_name(char *name);

int main() {

char name[100];
int emplnum = 0;

cout<<"Enter your numbers: "<<endl;

cout<<"Please enter your name: "<<endl;

while (!cin.eof()) {
 cout<<name<<" ";

return 0;

//check if name doesn't contain digits. 
int handle_name(char *name){
int index;
for(index = 0; name[index]!='\0';index++) {
  if (isdigit(name[index])) {
      return 0;

if(name[index] == '\0') return 1;

In line 19, to terminate the buffer loop after user has hit 'enter', I thought of adding this line of code

if((cin>>name)=='\n') break;

but I got error. Any other way to solve this?

Any help will be much appreciated. Thanks.

Edited 6 Years Ago by beatenbob: n/a

You can use EOF by pressing Ctrl-Z in Windows or Ctrl-D in Linux....Here's an example

#include <iostream>
#include <string>

int main()
	std::string word;

	std::cout << "Please enter a string of words...EOF to exit" << std::endl;

	while ( std::cin >> word )
		std::cout << "program recieved->" << word << std::endl;

	return 0;

Edited 6 Years Ago by gerard4143: n/a

This article has been dead for over six months. Start a new discussion instead.