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.