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;
cin>>emplnum;

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

while (!cin.eof()) {
 cin>>name;
 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;
     break;
  }


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

Recommended Answers

All 2 Replies

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;

	std::cin.clear();
	return 0;
}

Maybe getline() would be helpful...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.