Display only the first word in a char array

So I'm currently writing a program to ask the user to enter their full name
then only output their first name. But I can figure out how to do it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
using namespace std;

char name[50];

void TakeData(char name[]){
	cin.get(name, 49);
}

void First(char name[]){
	
	cout << "Your first name is: ";

        if (isspace(*name)){
        cout << name;
        }

	}

}

int main(){
	
	cout << "Enter your full name: "; TakeData(name);

	First(name);

	return 0;
}


For example:
Enter your full name: Jayson J James
Your first name is Jason
Last edited on
NOTE: i wil strongly recommend the c++ string class http://www.cplusplus.com/reference/string/string/, if you are allowed to use it

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;


return 0;
}
Last edited on
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.
Then Make it clear in Your Request . Do you want to read the whole name, Convert case and then output only the first name ?
Last edited on
Here's the actual full code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cstring>
#include <string>
using namespace 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.
Last edited on
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)
I see, thanks for clearing that up.

So I know the string has the find_first_of but is their something equivalent to that in cstring?
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.
Topic archived. No new replies allowed.