From my understanding of what you are trying to do, you could use the getline method instead of get.
The getline method takes a 3rd parameter that act as a delimiter. see my example below:
#include <iostream>
#include <cctype>
using namespace std;
int main(){
char name[50];
cout << "Enter your full name: ";
cin.getline(name, 45, ' ');
cout << "\t Your First name is " << name << endl;
Actually, I have a few functions before that require me to use get so that I will be able to convert the whole name to uppercase/lowercase, before I actually display just their first name. I wouldn't be able to really use getline in what I'm trying to do.
#include <iostream>
#include <cstring>
#include <string>
usingnamespace std;
char name[50];
void TakeData(char name[]){
cin.get(name, 49);
}
void Length(char name[]){
int length = strlen(name);
cout << "\tYour name's length is "
<< length
<< endl;
}
void LowerCase(char name[]){
strlwr(name);
cout << "\tYour name in lower case: "
<< name
<< endl;
}
void UpperCase(char name[]){
strupr(name);
cout << "\tYour name in upper case: "
<< name
<< endl;
}
void First(char name[]){
cout << "Your first name is: ";
}
int main(){
cout << "Enter your full name: "; TakeData(name);
Length(name);
LowerCase(name);
UpperCase(name);
First(name);
return 0;
}
If I use getline instead of get like you said, it would only read the first name that's inputed and only convert the first name to lower/uppercase, when I need the whole name that was inputed to be converted to lowercase/uppercase, then display only the first name.
If I use getline instead of get like you said, it would only read the first name
Not true. The getline does by default read up to newline. (Hence the name "get line")
The get() does by default read up to newline too. Thus, no difference.
bazetwo's code does read only the first word from the stream, so it does not what it should, but that is not a get vs getline issue; you can do that with either.
You have somehow gotten a C-string. You assume that it contains whitespace delimited words.
If you could find the first whitespace character from the C-string, then characters before it were the first word ...
(unless there is leading whitespace)
std::string has find too. It can search for single character (for example ' '). The find_first_of is a "search with a set, whether any of them matches", i.e. one could accept both space and tabulator as matches.
The standard library does have std::find too. It is generic and works with any array (including C-string).
However, in your case a simple loop would be enough. Print single characters until current character becomes space or the string ends, rather than finding the end of word and printing later.