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.

Katie Stevers
  • 135
  • 1
  • 1
  • 13
  • 11
    If you are using C++, use `std::string` instead of char arrays -- otherwise use C. Don't use C++ like it was C. – cdhowie Feb 02 '17 at 01:17
  • Just split the string: http://stackoverflow.com/questions/236129/split-a-string-in-c –  Feb 02 '17 at 01:19
  • Find the end of the first word, then put a `\0` after the word. – Thomas Matthews Feb 02 '17 at 01:22
  • 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. @cdhowie – Katie Stevers Feb 02 '17 at 02:37
  • @ThomasMatthews How would I know where the end of the first word is? – Katie Stevers Feb 02 '17 at 02:41
  • Actually, yes, we are. We are using "using namespace std;" in the header. So the extra bumbojumbo isn't needed. – Katie Stevers Feb 02 '17 at 02:53
  • @KatieStevers: Start from the beginning of the array and find the first character that is not allowed in a word. See `std::isalpha`. – Thomas Matthews Feb 02 '17 at 15:10
  • @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. – cdhowie Feb 05 '17 at 02:49

3 Answers3

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>
Wagner Patriota
  • 5,494
  • 26
  • 49
  • 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(" ")); – Katie Stevers Feb 02 '17 at 03:15
  • the problem is something else... this code above definitely works – Wagner Patriota Feb 02 '17 at 03:45
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;
Chad
  • 18,706
  • 4
  • 46
  • 63
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;
Xénon
  • 1
  • 1