How to only print the first word of a string in c++
Asked Answered
M

3

6

How do I set this up to only read the first word the user enters IF they enter to much info?

I do not want to use an if-else statement demanding they enter new info because their info was to much.

I just want it to basically ignore everything after the first word and only print the first word entered. Is this even possible?

const int SIZEB = 10;
char word[SIZEB];
cout << " Provide a word, up to 10 characters, no spaces. > " << endl;
cin.getline(word, SIZEB);
cout << " The word is: " << word << endl;
cout << endl;

UPDATE

It HAS to be a cstring. This is something I am working on for school. I am asking a series of questions and storing the answers as cstring in the first round. Then there is a second round where I store them as string.

Medeah answered 2/2, 2017 at 1:15 Comment(8)
If you are using C++, use std::string instead of char arrays -- otherwise use C. Don't use C++ like it was C.Lim
Just split the string: https://mcmap.net/q/40394/-how-do-i-iterate-over-the-words-of-a-stringMccready
Find the end of the first word, then put a \0 after the word.Subsistent
It has to be a cstring. It is a program where I ask three questions and use cstrings then ask the same and use string. @LimMedeah
@ThomasMatthews How would I know where the end of the first word is?Medeah
Actually, yes, we are. We are using "using namespace std;" in the header. So the extra bumbojumbo isn't needed.Medeah
@KatieStevers: Start from the beginning of the array and find the first character that is not allowed in a word. See std::isalpha.Subsistent
@KatieStevers If you are using namespace std; then you can simply refer to the std::string type as string. There is still absolutely no reason to be using C-style strings. Having said that, if your teacher/professor is teaching the use of using namespace std; and is also requiring that you use C-style strings, it's quite clear that your professor has no ****ing idea how to teach C++. If it is within your means do to so, I would look for another teacher.Lim
S
10

try this:

const int SIZEB = 10;
char word[SIZEB];
cout << " Provide a word, up to 10 characters, no spaces. > " << endl;
cin.getline(word, SIZEB);

std::string input = word;
std::string firstWord = input.substr(0, input.find(" "));

cout << " The word is: " << firstWord << endl;
cout << endl;

You need to do:

#include <string>
Splashy answered 2/2, 2017 at 1:33 Comment(2)
I get this error with this: error: member reference base type 'char [11]' is not a structure or union Looks like it is referring to the line: string firstWord = input.substr(0, input.find(" "));Medeah
the problem is something else... this code above definitely worksSplashy
D
3
std::string word;
std::cout << "Provide a word, up to 10 characters, no spaces.";
std::cin >> word;

std::cout << "The word is: " << word;

If you have to have it less than 10 characters, you can truncate the string as necessary. No reason for C-style strings, arrays etc.

"I have to use a c string." Sigh...

char word[11] = {0}; // keep an extra byte for null termination
cin.getline(word, sizeof(word) - 1);

for(auto& c : word)
{
    // replace spaces will null
    if(c == ' ')
       c = 0;
}

cout << "The word is: " << word << endl;
Duenas answered 2/2, 2017 at 1:46 Comment(0)
S
0

you could also use this method :

std::string str;
std::cin >> str;
std::string word;
int str_size = str.size();
for(int i = 0; i < str_size; i++){
    word.push_back(str[i]);
    if(str[i] == ' ') break;
}
std::cout << "\n" << word << std::endl;
Sillimanite answered 7/2, 2022 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.