I am very new to this and working on a project that requires me to use char getKeyLetter as an input method, but the instructions also state that it won't be possible to get a solution by the method being in char. My task is to make this method gather an single letter from the user and then return it back to the main() as a char.
The char will be used in a statement also gathered by the user. Then use this key letter to remove, replace, and count the letter in the statement. I pretty much have the rest of the code, but getting the KeyLetter has me stumped. Could anyone suggest if i'm supposed to use a string or int or what? I realize I have to convert whatever I use to char before returning by a functional return.
Here is my partially written code for this method. I have no idea how to accomplish this. I know I haven't done my else statements yet, I'm just trying to get the output right first.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// gets inputs
string getKeyLetter()
{
string getKeyLetter;
cout << "Please enter a SINGLE letter to act as key:" << endl;
if (getKeyLetter.length() != 1);
{
cin.get();
}
string getKeyLetter = "Please enter a SINGLE letter to act as key:";
char *keyLetter = newchar[getKeyLetter.length() != 1];
strcpy(keyLetter, getKeyLetter.c_str());
delete[] keyLetter;
return keyLetter;
}
I don't understand what you're trying to accomplish with your code? Can you tell us what you think each line is supposed to do and maybe we can help you fix it.
If you want just one letter, you're heavily complicating it,
1 2 3 4 5 6 7 8 9
string getKeyLetter()
{
string letter = "PLACEHOLDER"; //Initialise variable
while(letter.length() != 1){
cout << "Please enter one letter to act as key: ";
cin >> letter;
}
return letter;
}
honestly, I was lost. I have been trying to find exactly how to accomplish what i want. I was trying to find something to convert the string to a char, but I wasn't truly sure how to accomplish it. That's where lines 10-13 got me confused, as I was trying to implement how the heck to do it, from looking up how to convert a string to char. Then just last night after posting this, I found that when I missed classes, due to personal issues, the prior week's assignment covered do and while loops.
So your code here, so I make sure I'm grasping this, is that the placeholder statement actually creates a type of buffer? I will modify it a bit to create a loop when more than one character, or no character is put in. I'll keep you posted if I have troubles. Thank you for the help.
The assignment stated to return it as a char. Is there a way to do this, or do I convert the string to a char in the main()?
ok so my code is working so far. Thank you so much for your help CodeApprentice. I got the letter and the phrase portions working with the do-while loops. I'll come back here if I have issues with replacing the letter or removing it in the phrase.
Ok, so I have gotten my phrase as well as my letter and returned them, but I get external error on my maskLetter method. I think the problem is that the returned letter is a string and not a char, but I'm not really sure. The compiler doesn't show that there is anything wrong in the actual coding portion of the method, so that's why I assume it's because I have no char. Where am I going astray? Thank you.