Hello katiestevers,
Good to see you back.
In answer to your new subject line I think you meant character not word, but yes you could use a pointer. But why, when "word[0]" will work just fine. If you really want to use pointer we can figure it out.
First there was nothing wrong with the program in your original post. Just your understanding of what was happening or not happening.
The first section to get one character. The code works fine as long as you type one character and press enter. But should you type two or more characters that becomes a problem. I do not claim to know the exact answer, but when you enter two or more characters the
cin.getline(letter, SIZEA);
on line 28 works correctly to extract a single character, but at the same time the extra character(s) cause the IO operation of cin to fail. Thus by the time you get to the next "cin.getline" cin is still in the fail state and dos not work. Add these if statements between section one and two of your original code and see what prints out when you enter more than one character compared to when you only enter one character.
1 2 3 4 5 6
|
if (!std::cin)
std::cout << "\n cin failed";
if (std::cin.bad())
std::cout << "\n cin bad bit set\n";
if (std::cin.fail())
std::cout << "\n cin fail bit set\n";
|
This is what I did to fix the problem. The addition is in bold:
1 2 3 4 5 6 7 8
|
const int SIZEB = 11;
char word[SIZEB]{ '\0' }; // Should always initialize variables.
std::cout << "\n Provide a word, up to 10 characters, no spaces. > " << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.getline(word, SIZEB);
std::cout << " The word is: " << word << std::endl;
std::cout << std::endl;
|
as it has been said the
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
is also useful. Also notice that both line come before the second "cin.getline(...)" so that the getline will work properly. putting the lines
1 2
|
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
before the second and third
std::cin.getline(...);
will make the first half of the program work.
I think you may find these links useful:
http://www.cplusplus.com/reference/istream/istream/getline/
http://www.cplusplus.com/reference/istream/istream/get/
Now in the second half of the program strings are much different than character arrays. You can think of a string as a variable length character array that can change size as needed. Strings are more versatile than character arrays because you can use "=" to compare two strings whereas character arrays require a function. You also have the use of + and += with strings and I think some others.
Next you need to understand how
cin >> leter;
works. Using this format "leter" will receive whatever you type into the keyboard be it one or 100 characters.The two things that will end input from the input buffer are a white space or enter. You may have seen something like
cin >> variable1 >> variable2
and when you enter from the keyboard it looks like
123 456
with the space separating the two numbers. And what happens is that 123 goes from the input buffer to variable1, but the transfer eats the space and replaces it with "\n" leaving 456\n in the input buffer. Then the transfer to variable2 moves 456 and appends the "\n" leaving the "\n" in the input buffer. At this point any new call to cin will retrieve the "\n" from the buffer. Not rally what you want.
Another quirk of using "cin >>" and "getline(...)" is that you need to use the
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
after you have used "cin >>" and before you use a "getline(...)" so the input buffer is cleared first.
If your intention is to use a pointer with the strings you could, but it is just as easy to use
leter[0]
or
leter.at(0)
and with the second version it will do bounds checking so that you do not make any mistakes.
Using
wordd.resize(10);
is a quick brute force way to drop any characters over ten or thirty, but it dos work. I started to work with
wordd.erase(10,length - 10);
which worked, but still have a couple of tests to do.
Check out these for help or more confusion:
http://www.cplusplus.com/reference/string/string/erase/
http://www.cplusplus.com/reference/string/string/resize/
Hope that helps,
Andy