C文字列と関数に関する割り当てが必要です。文字列の最後のn文字を表示する
#include <iostream>
using namespace std;
//other functions
char display_last_nchar(char sent[], int n); // function i'm having trouble with
void main()
{
char sentence[31];
int selection, n;
do {
cout << "Please enter a string: " << endl;
cin.getline(sentence, 31, '\n'); //also, for me here i have to hit enter twice. How can I fix that?
cin.ignore();
cout << "Please make a selection: " << endl;
//other options
cout << "4. Display the last n character of the string " << endl;
cout << "7. Exit" << endl;
cin >> selection;
cin.ignore();
switch (selection)
{
case 4:
cout << "How many characters from the end of the string "
<< "do you want to display? : " << endl;
cin >> n;
cin.ignore();
if (n >= 30)
{
cout << "Error: too many characters" << endl;
break;
}
else
display_last(sentence, n);
cout << sentence << endl;
break;
case 7:
break;
}
}
while (choice != 7);
//other functions
char display_last_nchar(char sent[], int n)
{
for (int i = n; n > 30; i++)
{
sent[i]; //I know this is wrong but this is a guess that i took
}
return sent[n];
}
文字列の最初のn文字を表示する方法を知っています。この機能では、「My hamster has new toy」という文字列を入力して最初の8文字を表示したい場合は、8文字目以降の文字をnullに設定し、「My hams」と表示します。
display_last_ncharが設定されているため、ユーザが入力する数字の前のすべての文字が0になりましたが、そのすべてがnullでした。
文字列の最後のn文字を表示する関数を作成するために必要な手順を教えてください。私はオンラインで見てみましたが、私の本では見たことがありません。
は、あなただけの表示や表示にしたいとオリジナルを変更しますか? – Zeokav
私はユーザーが入力した番号の後に文字を表示したいだけです。したがって、文字列が "こんにちは"で、ユーザーが文字列の最後の3文字を表示したい場合は、 "ere"だけがmainに表示されます。 –