title: Display last n characters of a string
body: <p>I need help with an assignment regarding c-strings and functions.</p>
  
  <pre><code>#include &lt;iostream&gt;
  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 &lt;&lt; "Please enter a string: " &lt;&lt; endl;
      cin.getline(sentence, 31, '\n'); //also, for me here i have to hit enter twice. How can I fix that?
      cin.ignore();
      cout &lt;&lt; "Please make a selection: " &lt;&lt; endl;
      //other options
      cout &lt;&lt; "4. Display the last n character of the string " &lt;&lt; endl;
      cout &lt;&lt; "7. Exit" &lt;&lt; endl;
      cin &gt;&gt; selection;
      cin.ignore();
  
      switch (selection)
      {
      case 4:
          cout &lt;&lt; "How many characters from the end of the string "
              &lt;&lt; "do you want to display? : " &lt;&lt; endl;
          cin &gt;&gt; n;
          cin.ignore();
          if (n &gt;= 30)
          {
              cout &lt;&lt; "Error: too many characters" &lt;&lt; endl;
              break;
          }
          else
          display_last(sentence, n);
          cout &lt;&lt; sentence &lt;&lt; endl;
          break;
      case 7:
          break;
      }
  } 
      while (choice != 7);
  
  //other functions
  char display_last_nchar(char sent[], int n)
  {
      for (int i = n; n &gt; 30; i++)
      {
      sent[i]; //I know this is wrong but this is a guess that i took
      }
  return sent[n];
  }
  </code></pre>
  
  <p>I know that how to display the first n characters of a string. For that function, if the user entered a string called "My hamster has a new toy" and they wanted to display the first 8 characters, it would set ever character after the 8th one to null, and thus only display "My hams."</p>
  
  <p>What I tried to do, for the display_last_nchar was set, every character before the user inputted number to 0, but all that did was null the whole string.</p>
  
  <p>Could someone please explain to me, the steps I would need to take in order to create a function that would display the last n characters of the string. I've tried looking online and in my book but it didn't really help me. </p>